Upgraded ExportToZipTask

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-17 16:24:43 +03:00
parent ec41252535
commit 0bf70d677e
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 68 additions and 50 deletions

View File

@ -424,45 +424,60 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q
void ExportToZipTask::executeTask()
{
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() {
setStatus("Adding files...");
setProgress(0, m_files.length());
if (!m_dir.exists()) {
emitFailed(tr("Folder doesn't exist"));
return;
}
if (!m_output->isOpen() && !m_output->open(QuaZip::mdCreate)) {
emitFailed(tr("Could not create file"));
return;
}
for (const QFileInfo& file : m_files) {
if (!isRunning())
return;
auto absolute = file.absoluteFilePath();
auto relative = m_dir.relativeFilePath(absolute);
setStatus("Compresing: " + relative);
setProgress(m_progress + 1, m_progressTotal);
if (m_followSymlinks) {
if (file.isSymLink())
absolute = file.symLinkTarget();
else
absolute = file.canonicalFilePath();
}
if (!JlCompress::compressFile(m_output.get(), absolute, m_destinationPrefix + relative)) {
emitFailed(tr("Could not read and compress %1").arg(relative));
return;
}
}
m_output->close();
if (m_output->getZipError() != 0) {
emitFailed(tr("A zip error occurred"));
return;
}
emitSucceeded();
});
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() { exportZip(); });
}
void ExportToZipTask::exportZip()
{
setStatus("Adding files...");
setProgress(0, m_files.length());
if (!m_dir.exists()) {
emitFailed(tr("Folder doesn't exist"));
return;
}
if (!m_output.isOpen() && !m_output.open(QuaZip::mdCreate)) {
emitFailed(tr("Could not create file"));
return;
}
for (auto fileName : m_extra_files.keys()) {
if (!isRunning())
return;
QuaZipFile indexFile(&m_output);
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName))) {
emitFailed(tr("Could not create:") + fileName);
return;
}
indexFile.write(m_extra_files[fileName]);
}
for (const QFileInfo& file : m_files) {
if (!isRunning())
return;
auto absolute = file.absoluteFilePath();
auto relative = m_dir.relativeFilePath(absolute);
setStatus("Compresing: " + relative);
setProgress(m_progress + 1, m_progressTotal);
if (m_follow_symlinks) {
if (file.isSymLink())
absolute = file.symLinkTarget();
else
absolute = file.canonicalFilePath();
}
if (!m_exclude_files.contains(relative) && !JlCompress::compressFile(&m_output, absolute, m_destination_prefix + relative)) {
emitFailed(tr("Could not read and compress %1").arg(relative));
return;
}
}
m_output.close();
if (m_output.getZipError() != 0) {
emitFailed(tr("A zip error occurred"));
return;
}
emitSucceeded();
}
} // namespace MMCZip

View File

@ -40,6 +40,7 @@
#include <quazip/JlCompress.h>
#include <QDir>
#include <QFileInfo>
#include <QHash>
#include <QSet>
#include <QString>
#include <functional>
@ -146,28 +147,30 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q
class ExportToZipTask : public Task {
public:
ExportToZipTask(std::shared_ptr<QuaZip> output,
QDir dir,
QFileInfoList files,
QString destinationPrefix = "",
bool followSymlinks = false)
: m_output(output), m_dir(dir), m_files(files), m_destinationPrefix(destinationPrefix), m_followSymlinks(followSymlinks)
ExportToZipTask(QString outputPath, QDir dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false)
: m_output(outputPath), m_dir(dir), m_files(files), m_destination_prefix(destinationPrefix), m_follow_symlinks(followSymlinks)
{
setAbortable(true);
};
ExportToZipTask(QString outputPath, QString dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false)
: ExportToZipTask(std::make_shared<QuaZip>(outputPath), QDir(dir), files, destinationPrefix, followSymlinks){};
: ExportToZipTask(outputPath, QDir(dir), files, destinationPrefix, followSymlinks){};
virtual ~ExportToZipTask() = default;
void setExcludeFiles(QStringList excludeFiles) { m_exclude_files = excludeFiles; }
void addExtraFile(QString fileName, QByteArray data) { m_extra_files.emplace(fileName, data); }
protected:
virtual void executeTask() override;
void exportZip();
private:
std::shared_ptr<QuaZip> m_output;
QuaZip m_output;
QDir m_dir;
QFileInfoList m_files;
QString m_destinationPrefix;
bool m_followSymlinks;
QString m_destination_prefix;
bool m_follow_symlinks;
QStringList m_exclude_files;
QHash<QString, QByteArray> m_extra_files;
};
} // namespace MMCZip