-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "QGCZip.h" | ||
#include "QGCLoggingCategory.h" | ||
|
||
#include <QtCore/QDir> | ||
#include <QtCore/private/qzipreader_p.h> | ||
#include <QtCore/private/qzipwriter_p.h> | ||
|
||
QGC_LOGGING_CATEGORY(QGCZipLog, "qgc.utilities.compression.qgczip") | ||
|
||
namespace QGCZip { | ||
|
||
bool zipDirectory(const QString &directoryPath, const QString &zipFilePath) | ||
{ | ||
QDir dir(directoryPath); | ||
if (!dir.exists()) { | ||
qCDebug(QGCZipLog) << "Directory does not exist:" << directoryPath; | ||
return false; | ||
} | ||
|
||
QFile zipFile(zipFilePath); | ||
if (!zipFile.open(QIODevice::WriteOnly)) { | ||
qCDebug(QGCZipLog) << "Could not open zip file for writing:" << zipFilePath; | ||
return false; | ||
} | ||
|
||
QZipWriter zipWriter(&zipFile); | ||
zipWriter.setCompressionPolicy(QZipWriter::AutoCompress); | ||
|
||
QStringList fileList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::AllDirs, QDir::DirsFirst); | ||
|
||
for (const QString &fileName : fileList) { | ||
QString filePath = directoryPath + "/" + fileName; | ||
|
||
if (QFileInfo(filePath).isDir()) { | ||
zipWriter.addDirectory(fileName); | ||
} else { | ||
QFile file(filePath); | ||
if (file.open(QIODevice::ReadOnly)) { | ||
zipWriter.addFile(fileName, file.readAll()); | ||
file.close(); | ||
} else { | ||
qCDebug(QGCZipLog) << "Could not open file for reading:" << filePath; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
zipWriter.close(); | ||
return true; | ||
} | ||
|
||
bool unzipFile(const QString &zipFilePath, const QString &outputDirectoryPath) | ||
{ | ||
QDir outputDir(outputDirectoryPath); | ||
if (!outputDir.exists()) { | ||
if (!outputDir.mkpath(outputDirectoryPath)) { | ||
qCDebug(QGCZipLog) << "Failed to create output directory:" << outputDirectoryPath; | ||
return false; | ||
} | ||
} | ||
|
||
QFile zipFile(zipFilePath); | ||
if (!zipFile.open(QIODevice::ReadOnly)) { | ||
qCDebug(QGCZipLog) << "Could not open zip file:" << zipFilePath; | ||
return false; | ||
} | ||
|
||
QZipReader zipReader(&zipFile); | ||
if (!zipReader.isReadable()) { | ||
qCDebug(QGCZipLog) << "Could not read zip file:" << zipFilePath; | ||
return false; | ||
} | ||
|
||
const QList<QZipReader::FileInfo> allFiles = zipReader.fileInfoList(); | ||
|
||
for (const QZipReader::FileInfo &fileInfo : allFiles) { | ||
QString filePath = outputDirectoryPath + "/" + fileInfo.filePath; | ||
|
||
if (fileInfo.isDir) { | ||
QDir().mkpath(filePath); | ||
} else if (fileInfo.isFile) { | ||
QFile outFile(filePath); | ||
if (outFile.open(QIODevice::WriteOnly)) { | ||
outFile.write(zipReader.fileData(fileInfo.filePath)); | ||
outFile.close(); | ||
} else { | ||
qDebug() << "Could not open file for writing:" << filePath; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
zipReader.close(); | ||
return true; | ||
} | ||
|
||
} // namespace QGCZip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <QtCore/QLoggingCategory> | ||
|
||
Q_DECLARE_LOGGING_CATEGORY(QGCZipLog) | ||
|
||
namespace QGCZip { | ||
/// Method to zip files in a given directory | ||
bool zipDirectory(const QString &directoryPath, const QString &zipFilePath); | ||
|
||
/// Method to unzip files to a given directory | ||
bool unzipFile(const QString &zipFilePath, const QString &outputDirectoryPath); | ||
} // namespace QGCZip |
Oops, something went wrong.