diff --git a/oscar/zip.cpp b/oscar/zip.cpp index 3586d824..6a26fc10 100644 --- a/oscar/zip.cpp +++ b/oscar/zip.cpp @@ -57,6 +57,34 @@ bool ZipFile::Add(const QDir & root) } 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); @@ -124,6 +152,32 @@ bool ZipFile::Add(const QString & path, const QString & prefix) 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); +} // ================================================================================================== diff --git a/oscar/zip.h b/oscar/zip.h index 96021004..8e830ae4 100644 --- a/oscar/zip.h +++ b/oscar/zip.h @@ -11,6 +11,36 @@ #include #include +class FileQueue +{ + struct Entry + { + QString path; + QString name; + }; + QList 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 & files() const { return m_files; } + const QString toString() const; +}; + + class ZipFile { public: @@ -20,7 +50,8 @@ public: bool Open(const QString & filepath); bool Add(const QDir & root); 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(); protected: