Separate directory walking from zip creation.

This is necessary for presenting a progress bar during zip creation.
This commit is contained in:
sawinglogz 2020-01-22 10:56:08 -05:00
parent 1c4c7871da
commit 607002ffa1
2 changed files with 86 additions and 1 deletions

View File

@ -57,6 +57,34 @@ bool ZipFile::Add(const QDir & root)
} }
bool ZipFile::Add(const QDir & inDir, const QString & prefix) bool ZipFile::Add(const QDir & inDir, const QString & prefix)
{
bool ok;
FileQueue queue;
queue.Add(inDir, prefix);
ok = Add(queue);
return ok;
}
bool ZipFile::Add(const FileQueue & queue)
{
bool ok;
// TODO: add progress bar
qDebug() << "Adding" << queue.toString();
for (auto & entry : queue.files()) {
ok = Add(entry.path, entry.name);
if (!ok) {
break;
}
}
return ok;
}
// ==================================================================================================
bool FileQueue::Add(const QDir & inDir, const QString & prefix)
{ {
QDir dir(inDir); QDir dir(inDir);
@ -124,6 +152,32 @@ bool ZipFile::Add(const QString & path, const QString & prefix)
return ok; return ok;
} }
bool FileQueue::Add(const QString & path, const QString & prefix)
{
QFileInfo fi(path);
QString archive_name = prefix;
quint64 size;
if (fi.isDir()) {
m_dir_count++;
size = 0;
} else if (fi.exists()) {
m_file_count++;
size = fi.size();
} else {
qWarning() << "file doesn't exist" << path;
return false;
}
m_byte_count += size;
Entry entry = { path, archive_name };
m_files.append(entry);
return true;
}
const QString FileQueue::toString() const
{
return QString("%1 directories, %2 files, %3 bytes").arg(m_dir_count).arg(m_file_count).arg(m_byte_count);
}
// ================================================================================================== // ==================================================================================================

View File

@ -11,6 +11,36 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
class FileQueue
{
struct Entry
{
QString path;
QString name;
};
QList<Entry> m_files;
int m_dir_count;
int m_file_count;
quint64 m_byte_count;
public:
FileQueue() : m_dir_count(0), m_file_count(0), m_byte_count(0) {}
~FileQueue() = default;
//!brief Recursively add a directory and its contents to the queue along with the prefix to be used in an archive.
bool Add(const QDir & dir, const QString & prefix="");
//!brief Add a file to the queue along with the name to be used in an archive.
bool Add(const QString & path, const QString & archive_name="");
inline int dirCount() const { return m_dir_count; }
inline int fileCount() const { return m_file_count; }
inline quint64 byteCount() const { return m_byte_count; }
const QList<Entry> & files() const { return m_files; }
const QString toString() const;
};
class ZipFile class ZipFile
{ {
public: public:
@ -20,7 +50,8 @@ public:
bool Open(const QString & filepath); bool Open(const QString & filepath);
bool Add(const QDir & root); bool Add(const QDir & root);
bool Add(const QDir & dir, const QString & archive_name); // add a directory and recurse bool Add(const QDir & dir, const QString & archive_name); // add a directory and recurse
bool Add(const QString & path, const QString & archive_name); // add a file and recurse bool Add(const QString & path, const QString & archive_name); // add a single file
bool Add(const FileQueue & queue); // add a fixed list of files
void Close(); void Close();
protected: protected: