Upgraded ExportToZipTask
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
ec41252535
commit
0bf70d677e
@ -424,18 +424,33 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q
|
|||||||
|
|
||||||
void ExportToZipTask::executeTask()
|
void ExportToZipTask::executeTask()
|
||||||
{
|
{
|
||||||
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() {
|
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() { exportZip(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExportToZipTask::exportZip()
|
||||||
|
{
|
||||||
setStatus("Adding files...");
|
setStatus("Adding files...");
|
||||||
setProgress(0, m_files.length());
|
setProgress(0, m_files.length());
|
||||||
if (!m_dir.exists()) {
|
if (!m_dir.exists()) {
|
||||||
emitFailed(tr("Folder doesn't exist"));
|
emitFailed(tr("Folder doesn't exist"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!m_output->isOpen() && !m_output->open(QuaZip::mdCreate)) {
|
if (!m_output.isOpen() && !m_output.open(QuaZip::mdCreate)) {
|
||||||
emitFailed(tr("Could not create file"));
|
emitFailed(tr("Could not create file"));
|
||||||
return;
|
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) {
|
for (const QFileInfo& file : m_files) {
|
||||||
if (!isRunning())
|
if (!isRunning())
|
||||||
return;
|
return;
|
||||||
@ -444,25 +459,25 @@ void ExportToZipTask::executeTask()
|
|||||||
auto relative = m_dir.relativeFilePath(absolute);
|
auto relative = m_dir.relativeFilePath(absolute);
|
||||||
setStatus("Compresing: " + relative);
|
setStatus("Compresing: " + relative);
|
||||||
setProgress(m_progress + 1, m_progressTotal);
|
setProgress(m_progress + 1, m_progressTotal);
|
||||||
if (m_followSymlinks) {
|
if (m_follow_symlinks) {
|
||||||
if (file.isSymLink())
|
if (file.isSymLink())
|
||||||
absolute = file.symLinkTarget();
|
absolute = file.symLinkTarget();
|
||||||
else
|
else
|
||||||
absolute = file.canonicalFilePath();
|
absolute = file.canonicalFilePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!JlCompress::compressFile(m_output.get(), absolute, m_destinationPrefix + relative)) {
|
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));
|
emitFailed(tr("Could not read and compress %1").arg(relative));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_output->close();
|
m_output.close();
|
||||||
if (m_output->getZipError() != 0) {
|
if (m_output.getZipError() != 0) {
|
||||||
emitFailed(tr("A zip error occurred"));
|
emitFailed(tr("A zip error occurred"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emitSucceeded();
|
emitSucceeded();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace MMCZip
|
} // namespace MMCZip
|
@ -40,6 +40,7 @@
|
|||||||
#include <quazip/JlCompress.h>
|
#include <quazip/JlCompress.h>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QHash>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -146,28 +147,30 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q
|
|||||||
|
|
||||||
class ExportToZipTask : public Task {
|
class ExportToZipTask : public Task {
|
||||||
public:
|
public:
|
||||||
ExportToZipTask(std::shared_ptr<QuaZip> output,
|
ExportToZipTask(QString outputPath, QDir dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false)
|
||||||
QDir dir,
|
: m_output(outputPath), m_dir(dir), m_files(files), m_destination_prefix(destinationPrefix), m_follow_symlinks(followSymlinks)
|
||||||
QFileInfoList files,
|
|
||||||
QString destinationPrefix = "",
|
|
||||||
bool followSymlinks = false)
|
|
||||||
: m_output(output), m_dir(dir), m_files(files), m_destinationPrefix(destinationPrefix), m_followSymlinks(followSymlinks)
|
|
||||||
{
|
{
|
||||||
setAbortable(true);
|
setAbortable(true);
|
||||||
};
|
};
|
||||||
ExportToZipTask(QString outputPath, QString dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false)
|
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;
|
virtual ~ExportToZipTask() = default;
|
||||||
|
|
||||||
|
void setExcludeFiles(QStringList excludeFiles) { m_exclude_files = excludeFiles; }
|
||||||
|
void addExtraFile(QString fileName, QByteArray data) { m_extra_files.emplace(fileName, data); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void executeTask() override;
|
virtual void executeTask() override;
|
||||||
|
void exportZip();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<QuaZip> m_output;
|
QuaZip m_output;
|
||||||
QDir m_dir;
|
QDir m_dir;
|
||||||
QFileInfoList m_files;
|
QFileInfoList m_files;
|
||||||
QString m_destinationPrefix;
|
QString m_destination_prefix;
|
||||||
bool m_followSymlinks;
|
bool m_follow_symlinks;
|
||||||
|
QStringList m_exclude_files;
|
||||||
|
QHash<QString, QByteArray> m_extra_files;
|
||||||
};
|
};
|
||||||
} // namespace MMCZip
|
} // namespace MMCZip
|
||||||
|
Loading…
Reference in New Issue
Block a user