From f06da0b0cff78d2398714c0964d057676590b5b2 Mon Sep 17 00:00:00 2001 From: alqualos Date: Mon, 15 Feb 2016 18:02:18 +0000 Subject: [PATCH] New JlCompress methods (QIODevice*-based API by Lukasz Kwiecinski) --- quazip/quazip/JlCompress.cpp | 46 ++++++++++++++++++++++++++-- quazip/quazip/JlCompress.h | 39 +++++++++++++++++++++++- quazip/qztest/testjlcompress.cpp | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 3 deletions(-) diff --git a/quazip/quazip/JlCompress.cpp b/quazip/quazip/JlCompress.cpp index c6d78e2..a62af80 100644 --- a/quazip/quazip/JlCompress.cpp +++ b/quazip/quazip/JlCompress.cpp @@ -288,12 +288,17 @@ bool JlCompress::compressDir(QString fileCompressed, QString dir, QString JlCompress::extractFile(QString fileCompressed, QString fileName, QString fileDest) { // Apro lo zip QuaZip zip(fileCompressed); + return extractFile(zip, fileName, fileDest); +} + +QString JlCompress::extractFile(QuaZip &zip, QString fileName, QString fileDest) +{ if(!zip.open(QuaZip::mdUnzip)) { return QString(); } // Estraggo il file - if (fileDest.isEmpty()) + if (fileDest.isEmpty()) fileDest = fileName; if (!extractFile(&zip,fileName,fileDest)) { return QString(); @@ -311,6 +316,11 @@ QString JlCompress::extractFile(QString fileCompressed, QString fileName, QStrin QStringList JlCompress::extractFiles(QString fileCompressed, QStringList files, QString dir) { // Creo lo zip QuaZip zip(fileCompressed); + return extractFiles(zip, files, dir); +} + +QStringList JlCompress::extractFiles(QuaZip &zip, const QStringList &files, const QString &dir) +{ if(!zip.open(QuaZip::mdUnzip)) { return QStringList(); } @@ -339,6 +349,11 @@ QStringList JlCompress::extractFiles(QString fileCompressed, QStringList files, QStringList JlCompress::extractDir(QString fileCompressed, QString dir) { // Apro lo zip QuaZip zip(fileCompressed); + return extractDir(zip, dir); +} + +QStringList JlCompress::extractDir(QuaZip &zip, const QString &dir) +{ if(!zip.open(QuaZip::mdUnzip)) { return QStringList(); } @@ -371,6 +386,11 @@ QStringList JlCompress::extractDir(QString fileCompressed, QString dir) { QStringList JlCompress::getFileList(QString fileCompressed) { // Apro lo zip QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath()); + return getFileList(zip); +} + +QStringList JlCompress::getFileList(QuaZip *zip) +{ if(!zip->open(QuaZip::mdUnzip)) { delete zip; return QStringList(); @@ -395,7 +415,29 @@ QStringList JlCompress::getFileList(QString fileCompressed) { return QStringList(); } delete zip; - return lst; } +QStringList JlCompress::extractDir(QIODevice *ioDevice, QString dir) +{ + QuaZip zip(ioDevice); + return extractDir(zip, dir); +} + +QStringList JlCompress::getFileList(QIODevice *ioDevice) +{ + QuaZip *zip = new QuaZip(ioDevice); + return getFileList(zip); +} + +QString JlCompress::extractFile(QIODevice *ioDevice, QString fileName, QString fileDest) +{ + QuaZip zip(ioDevice); + return extractFile(zip, fileName, fileDest); +} + +QStringList JlCompress::extractFiles(QIODevice *ioDevice, QStringList files, QString dir) +{ + QuaZip zip(ioDevice); + return extractFiles(zip, files, dir); +} diff --git a/quazip/quazip/JlCompress.h b/quazip/quazip/JlCompress.h index 558ef34..49c5086 100644 --- a/quazip/quazip/JlCompress.h +++ b/quazip/quazip/JlCompress.h @@ -3,7 +3,7 @@ /* Copyright (C) 2010 Roberto Pompermaier -Copyright (C) 2005-2014 Sergey A. Tachenov +Copyright (C) 2005-2016 Sergey A. Tachenov This file is part of QuaZIP. @@ -41,6 +41,10 @@ see quazip/(un)zip.h files for details. Basically it's the zlib license. */ class QUAZIP_EXPORT JlCompress { private: + static QStringList extractDir(QuaZip &zip, const QString &dir); + static QStringList getFileList(QuaZip *zip); + static QString extractFile(QuaZip &zip, QString fileName, QString fileDest); + static QStringList extractFiles(QuaZip &zip, const QStringList &files, const QString &dir); /// Compress a single file. /** \param zip Opened zip to compress the file to. @@ -155,6 +159,39 @@ public: are present separately. */ static QStringList getFileList(QString fileCompressed); + /// Extract a single file. + /** + \param ioDevice pointer to device with compressed data. + \param fileName The file to extract. + \param fileDest The destination file, assumed to be identical to + \a file if left empty. + \return The list of the full paths of the files extracted, empty on failure. + */ + static QString extractFile(QIODevice *ioDevice, QString fileName, QString fileDest = QString()); + /// Extract a list of files. + /** + \param ioDevice pointer to device with compressed data. + \param files The file list to extract. + \param dir The directory to put the files to, the current + directory if left empty. + \return The list of the full paths of the files extracted, empty on failure. + */ + static QStringList extractFiles(QIODevice *ioDevice, QStringList files, QString dir = QString()); + /// Extract a whole archive. + /** + \param ioDevice pointer to device with compressed data. + \param dir The directory to extract to, the current directory if + left empty. + \return The list of the full paths of the files extracted, empty on failure. + */ + static QStringList extractDir(QIODevice *ioDevice, QString dir = QString()); + /// Get the file list. + /** + \return The list of the files in the archive, or, more precisely, the + list of the entries, including both files and directories if they + are present separately. + */ + static QStringList getFileList(QIODevice *ioDevice); }; #endif /* JLCOMPRESSFOLDER_H_ */ diff --git a/quazip/qztest/testjlcompress.cpp b/quazip/qztest/testjlcompress.cpp index f4d900e..618116c 100644 --- a/quazip/qztest/testjlcompress.cpp +++ b/quazip/qztest/testjlcompress.cpp @@ -61,6 +61,13 @@ void TestJlCompress::compressFile() QStringList fileList = JlCompress::getFileList(zipName); QCOMPARE(fileList.count(), 1); QVERIFY(fileList[0] == fileName); + // now test the QIODevice* overload of getFileList() + QFile zipFile(zipName); + QVERIFY(zipFile.open(QIODevice::ReadOnly)); + fileList = JlCompress::getFileList(zipName); + QCOMPARE(fileList.count(), 1); + QVERIFY(fileList[0] == fileName); + zipFile.close(); removeTestFiles(QStringList() << fileName); curDir.remove(zipName); } @@ -211,6 +218,15 @@ void TestJlCompress::extractFile() QCOMPARE(destInfo.size(), srcInfo.size()); QCOMPARE(destInfo.permissions(), srcInfo.permissions()); curDir.remove("jlext/jlfile/" + destName); + // now test the QIODevice* overload + QFile zipFile(zipName); + QVERIFY(zipFile.open(QIODevice::ReadOnly)); + QVERIFY(!JlCompress::extractFile(&zipFile, fileToExtract, + "jlext/jlfile/" + destName).isEmpty()); + destInfo = QFileInfo("jlext/jlfile/" + destName); + QCOMPARE(destInfo.size(), srcInfo.size()); + QCOMPARE(destInfo.permissions(), srcInfo.permissions()); + curDir.remove("jlext/jlfile/" + destName); if (!fileToExtract.endsWith("/")) { // If we aren't extracting a directory, we need to check // that extractFile() fails if there is a directory @@ -219,6 +235,7 @@ void TestJlCompress::extractFile() QVERIFY(JlCompress::extractFile(zipName, fileToExtract, "jlext/jlfile/" + destName).isEmpty()); } + zipFile.close(); // Here we either delete the target dir or the dir created in the // test above. curDir.rmpath("jlext/jlfile/" + destName); @@ -262,6 +279,20 @@ void TestJlCompress::extractFiles() curDir.remove("jlext/jlfiles/" + fileName); curDir.rmpath(fileInfo.dir().path()); } + // now test the QIODevice* overload + QFile zipFile(zipName); + QVERIFY(zipFile.open(QIODevice::ReadOnly)); + QVERIFY(!JlCompress::extractFiles(&zipFile, filesToExtract, + "jlext/jlfiles").isEmpty()); + foreach (QString fileName, filesToExtract) { + QFileInfo fileInfo("jlext/jlfiles/" + fileName); + QFileInfo extInfo("tmp/" + fileName); + QCOMPARE(fileInfo.size(), extInfo.size()); + QCOMPARE(fileInfo.permissions(), extInfo.permissions()); + curDir.remove("jlext/jlfiles/" + fileName); + curDir.rmpath(fileInfo.dir().path()); + } + zipFile.close(); curDir.rmpath("jlext/jlfiles"); removeTestFiles(fileNames); curDir.remove(zipName); @@ -309,6 +340,26 @@ void TestJlCompress::extractDir() absolutePath += '/'; QVERIFY(extracted.contains(absolutePath)); } + // now test the QIODevice* overload + QFile zipFile(zipName); + QVERIFY(zipFile.open(QIODevice::ReadOnly)); + QCOMPARE((extracted = JlCompress::extractDir(&zipFile, "jlext/jldir")) + .count(), fileNames.count()); + foreach (QString fileName, fileNames) { + QString fullName = "jlext/jldir/" + fileName; + QFileInfo fileInfo(fullName); + QFileInfo extInfo("tmp/" + fileName); + if (!fileInfo.isDir()) + QCOMPARE(fileInfo.size(), extInfo.size()); + QCOMPARE(fileInfo.permissions(), extInfo.permissions()); + curDir.remove(fullName); + curDir.rmpath(fileInfo.dir().path()); + QString absolutePath = fileInfo.absoluteFilePath(); + if (fileInfo.isDir() && !absolutePath.endsWith('/')) + absolutePath += '/'; + QVERIFY(extracted.contains(absolutePath)); + } + zipFile.close(); curDir.rmpath("jlext/jldir"); removeTestFiles(fileNames); curDir.remove(zipName);