diff options
author | Petr Mrázek <peterix@gmail.com> | 2015-09-13 04:21:26 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2015-09-13 04:21:26 +0200 |
commit | 8ef07ec6346a4f9c078184c9497e7c12f5b8c33d (patch) | |
tree | ddb81e3591825bd1cd5e080e1fcc4aede553d911 /logic/MMCZip.cpp | |
parent | 2315f463a8e7713fc62027bb9540a22b240a0f78 (diff) | |
download | MultiMC-8ef07ec6346a4f9c078184c9497e7c12f5b8c33d.tar MultiMC-8ef07ec6346a4f9c078184c9497e7c12f5b8c33d.tar.gz MultiMC-8ef07ec6346a4f9c078184c9497e7c12f5b8c33d.tar.lz MultiMC-8ef07ec6346a4f9c078184c9497e7c12f5b8c33d.tar.xz MultiMC-8ef07ec6346a4f9c078184c9497e7c12f5b8c33d.zip |
GH-1227 allow structured world zip import and drag and drop out of MultiMC
Diffstat (limited to 'logic/MMCZip.cpp')
-rw-r--r-- | logic/MMCZip.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/logic/MMCZip.cpp b/logic/MMCZip.cpp index 75f49ced..62904fa5 100644 --- a/logic/MMCZip.cpp +++ b/logic/MMCZip.cpp @@ -26,6 +26,7 @@ see quazip/(un)MMCZip.h files for details. Basically it's the zlib license. #include <pathutils.h> #include <quazip.h> #include <JlCompress.h> +#include <quazipdir.h> #include "MMCZip.h" #include <QDebug> @@ -338,3 +339,153 @@ bool MMCZip::compressDir(QString zipFile, QString dir, QString prefix, const Sep return true; } +QString MMCZip::findFileInZip(QuaZip * zip, const QString & what, const QString &root) +{ + QuaZipDir rootDir(zip, root); + for(auto fileName: rootDir.entryList(QDir::Files)) + { + if(fileName == what) + return root; + } + for(auto fileName: rootDir.entryList(QDir::Dirs)) + { + QString result = findFileInZip(zip, what, root + fileName); + if(!result.isEmpty()) + { + return result; + } + } + return QString(); +} + +bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root) +{ + QuaZipDir rootDir(zip, root); + for(auto fileName: rootDir.entryList(QDir::Files)) + { + if(fileName == what) + { + result.append(root); + return true; + } + } + for(auto fileName: rootDir.entryList(QDir::Dirs)) + { + findFilesInZip(zip, what, result, root + fileName); + } + return !result.isEmpty(); +} + +bool removeFile(QStringList listFile) +{ + bool ret = true; + for (int i = 0; i < listFile.count(); i++) + { + ret &= QFile::remove(listFile.at(i)); + } + return ret; +} + +bool MMCZip::extractFile(QuaZip *zip, const QString &fileName, const QString &fileDest) +{ + if(!zip) + return false; + + if (zip->getMode() != QuaZip::mdUnzip) + return false; + + if (!fileName.isEmpty()) + zip->setCurrentFile(fileName); + + QuaZipFile inFile(zip); + if (!inFile.open(QIODevice::ReadOnly) || inFile.getZipError() != UNZ_OK) + return false; + + // Controllo esistenza cartella file risultato + QDir curDir; + if (fileDest.endsWith('/')) + { + if (!curDir.mkpath(fileDest)) + { + return false; + } + } + else + { + if (!curDir.mkpath(QFileInfo(fileDest).absolutePath())) + { + return false; + } + } + + QuaZipFileInfo64 info; + if (!zip->getCurrentFileInfo(&info)) + return false; + + QFile::Permissions srcPerm = info.getPermissions(); + if (fileDest.endsWith('/') && QFileInfo(fileDest).isDir()) + { + if (srcPerm != 0) + { + QFile(fileDest).setPermissions(srcPerm); + } + return true; + } + + QFile outFile; + outFile.setFileName(fileDest); + if (!outFile.open(QIODevice::WriteOnly)) + return false; + + if (!copyData(inFile, outFile) || inFile.getZipError() != UNZ_OK) + { + outFile.close(); + removeFile(QStringList(fileDest)); + return false; + } + outFile.close(); + + inFile.close(); + if (inFile.getZipError() != UNZ_OK) + { + removeFile(QStringList(fileDest)); + return false; + } + + if (srcPerm != 0) + { + outFile.setPermissions(srcPerm); + } + return true; +} + +QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target) +{ + QDir directory(target); + QStringList extracted; + if (!zip->goToFirstFile()) + { + return QStringList(); + } + do + { + QString name = zip->getCurrentFileName(); + if(!name.startsWith(subdir)) + { + continue; + } + name.remove(0, subdir.size()); + QString absFilePath = directory.absoluteFilePath(name); + if(name.isEmpty()) + { + absFilePath += "/"; + } + if (!extractFile(zip, "", absFilePath)) + { + removeFile(extracted); + return QStringList(); + } + extracted.append(absFilePath); + } while (zip->goToNextFile()); + return extracted; +} |