mirror of
https://gitlab.com/pholy/OSCAR-code.git
synced 2025-04-05 02:30:44 +00:00
Add zip creation of OSCAR data folder.
Also fix the zip progress bar when compressing more than 2GB data, and prevent the zip file from ever trying to include itself.
This commit is contained in:
parent
5dfbc1fbe4
commit
78d3155ecd
@ -2672,9 +2672,50 @@ void MainWindow::on_actionCreate_OSCAR_Data_zip_triggered()
|
||||
}
|
||||
|
||||
qDebug() << "Create zip of OSCAR data folder:" << filename;
|
||||
// TODO: implement creation of .zip
|
||||
// TODO: write debug log to .txt file in data folder (or zip)
|
||||
// NOTE: make sure not to include .zip in itself if the user chooses to write the zip in the data folder itself!
|
||||
|
||||
QDir oscarData(GetAppData());
|
||||
QFile debugLog(oscarData.canonicalPath() + QDir::separator() + "debuglog.txt");
|
||||
|
||||
ZipFile z;
|
||||
bool ok = z.Open(filename);
|
||||
if (ok) {
|
||||
ProgressDialog * prog = new ProgressDialog(this);
|
||||
prog->setMessage(tr("Calculating size..."));
|
||||
prog->setWindowModality(Qt::ApplicationModal);
|
||||
prog->open();
|
||||
|
||||
// Build the list of files and exclude any existing debug log.
|
||||
FileQueue files;
|
||||
files.AddDirectory(oscarData.canonicalPath(), oscarData.dirName());
|
||||
files.Remove(debugLog.fileName());
|
||||
|
||||
prog->setMessage(tr("Creating archive..."));
|
||||
|
||||
// Create the zip.
|
||||
ok = z.AddFiles(files, prog);
|
||||
if (ok && z.aborted() == false) {
|
||||
// Update the debug log and add it last.
|
||||
ok = debugLog.open(QIODevice::WriteOnly);
|
||||
if (ok) {
|
||||
debugLog.write(ui->logText->toPlainText().toLocal8Bit().data());
|
||||
debugLog.close();
|
||||
QString debugLogName = oscarData.dirName() + QDir::separator() + QFileInfo(debugLog).fileName();
|
||||
ok = z.AddFile(debugLog.fileName(), debugLogName);
|
||||
if (!ok) {
|
||||
qWarning() << "Unable to add debug log to archive!";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
z.Close();
|
||||
} else {
|
||||
qWarning() << "Unable to open" << filename;
|
||||
}
|
||||
if (!ok) {
|
||||
QMessageBox::warning(nullptr, STR_MessageBox_Error,
|
||||
QObject::tr("Unable to create archive!"),
|
||||
QMessageBox::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
#include "translation.h"
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include <QCoreApplication>
|
||||
#include "SleepLib/progressdialog.h"
|
||||
|
||||
static const quint64 PROGRESS_SCALE = 1024; // QProgressBar only holds an int, so report progress in KiB.
|
||||
|
||||
// Static functions to abstract the details of miniz from the primary logic.
|
||||
static void* zip_init();
|
||||
@ -67,10 +68,13 @@ bool ZipFile::AddDirectory(const QString & path, const QString & prefix, Progres
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool ZipFile::AddFiles(const FileQueue & queue, ProgressDialog* progress)
|
||||
bool ZipFile::AddFiles(FileQueue & queue, ProgressDialog* progress)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
// Exclude the zip file that's being created (if it happens to be in the list).
|
||||
queue.Remove(QFileInfo(m_file).canonicalFilePath());
|
||||
|
||||
qDebug().noquote() << "Adding" << queue.toString();
|
||||
m_abort = false;
|
||||
m_progress = 0;
|
||||
@ -85,8 +89,8 @@ bool ZipFile::AddFiles(const FileQueue & queue, ProgressDialog* progress)
|
||||
}
|
||||
|
||||
// Always emit, since the caller may have configured and connected a progress dialog manually.
|
||||
emit setProgressValue(m_progress);
|
||||
emit setProgressMax(queue.byteCount() + queue.dirCount());
|
||||
emit setProgressValue(m_progress/PROGRESS_SCALE);
|
||||
emit setProgressMax((queue.byteCount() + queue.dirCount())/PROGRESS_SCALE);
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
for (auto & entry : queue.files()) {
|
||||
@ -149,7 +153,7 @@ bool ZipFile::AddFile(const QString & path, const QString & name)
|
||||
|
||||
bool ok = zip_add(m_ctx, archive_name, data, fi.lastModified());
|
||||
|
||||
emit setProgressValue(m_progress);
|
||||
emit setProgressValue(m_progress/PROGRESS_SCALE);
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
return ok;
|
||||
@ -201,27 +205,51 @@ bool FileQueue::AddDirectory(const QString & path, const QString & prefix)
|
||||
bool FileQueue::AddFile(const QString & path, const QString & prefix)
|
||||
{
|
||||
QFileInfo fi(path);
|
||||
QString canonicalPath = fi.canonicalFilePath();
|
||||
QString archive_name = prefix;
|
||||
quint64 size;
|
||||
|
||||
if (archive_name.isEmpty()) archive_name = fi.fileName();
|
||||
|
||||
if (fi.isDir()) {
|
||||
m_dir_count++;
|
||||
size = 0;
|
||||
} else if (fi.exists()) {
|
||||
m_file_count++;
|
||||
size = fi.size();
|
||||
m_byte_count += fi.size();
|
||||
} else {
|
||||
qWarning() << "file doesn't exist" << path;
|
||||
qWarning() << "file doesn't exist" << canonicalPath;
|
||||
return false;
|
||||
}
|
||||
m_byte_count += size;
|
||||
Entry entry = { path, archive_name };
|
||||
Entry entry = { canonicalPath, archive_name };
|
||||
m_files.append(entry);
|
||||
QCoreApplication::processEvents();
|
||||
return true;
|
||||
}
|
||||
|
||||
int FileQueue::Remove(const QString & path)
|
||||
{
|
||||
QFileInfo fi(path);
|
||||
QString canonicalPath = fi.canonicalFilePath();
|
||||
int removed = 0;
|
||||
|
||||
QMutableListIterator<Entry> i(m_files);
|
||||
while (i.hasNext()) {
|
||||
Entry & entry = i.next();
|
||||
if (entry.path == canonicalPath) {
|
||||
if (fi.isDir()) {
|
||||
m_dir_count--;
|
||||
} else {
|
||||
m_file_count--;
|
||||
m_byte_count -= fi.size();
|
||||
}
|
||||
i.remove();
|
||||
removed++;
|
||||
qDebug().noquote() << "skipping file:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -25,7 +25,7 @@ public:
|
||||
bool Open(const QString & filepath);
|
||||
bool AddDirectory(const QString & path, ProgressDialog* progress=nullptr); // add a directory and recurse
|
||||
bool AddDirectory(const QString & path, const QString & archive_name, ProgressDialog* progress=nullptr); // add a directory and recurse
|
||||
bool AddFiles(const class FileQueue & queue, ProgressDialog* progress=nullptr); // add a fixed list of files
|
||||
bool AddFiles(class FileQueue & queue, ProgressDialog* progress=nullptr); // add a fixed list of files
|
||||
bool AddFile(const QString & path, const QString & archive_name); // add a single file
|
||||
void Close();
|
||||
|
||||
@ -42,7 +42,7 @@ protected:
|
||||
void* m_ctx;
|
||||
QFile m_file;
|
||||
bool m_abort;
|
||||
int m_progress;
|
||||
quint64 m_progress;
|
||||
};
|
||||
|
||||
|
||||
@ -62,6 +62,9 @@ public:
|
||||
FileQueue() : m_dir_count(0), m_file_count(0), m_byte_count(0) {}
|
||||
~FileQueue() = default;
|
||||
|
||||
//!brief Remove a file from the queue, return the number of instances removed.
|
||||
int Remove(const QString & path);
|
||||
|
||||
//!brief Recursively add a directory and its contents to the queue along with the prefix to be used in an archive.
|
||||
bool AddDirectory(const QString & path, const QString & prefix="");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user