2022-03-19 11:46:56 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2017-05-31 07:17:45 +00:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2017-05-31 07:17:45 +00:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-31 07:17:45 +00:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2017-05-31 07:17:45 +00:00
|
|
|
*/
|
2022-01-25 11:53:33 +00:00
|
|
|
|
2022-01-24 21:55:57 +00:00
|
|
|
#include <quazip/quazip.h>
|
|
|
|
#include <quazip/quazipdir.h>
|
|
|
|
#include <quazip/quazipfile.h>
|
2015-04-12 20:50:52 +00:00
|
|
|
#include "MMCZip.h"
|
2015-10-04 23:47:27 +00:00
|
|
|
#include "FileSystem.h"
|
2015-04-12 20:50:52 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
2023-10-28 19:28:35 +00:00
|
|
|
#include <deque>
|
2015-04-12 20:50:52 +00:00
|
|
|
|
2017-05-31 07:17:45 +00:00
|
|
|
// ours
|
2022-01-20 19:40:56 +00:00
|
|
|
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const FilterFunction filter)
|
2015-04-12 20:50:52 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QuaZip modZip(from.filePath());
|
|
|
|
modZip.open(QuaZip::mdUnzip);
|
|
|
|
|
|
|
|
QuaZipFile fileInsideMod(&modZip);
|
|
|
|
QuaZipFile zipOutFile(into);
|
|
|
|
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
|
|
|
|
{
|
|
|
|
QString filename = modZip.getCurrentFileName();
|
|
|
|
if (filter && !filter(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "Skipping file " << filename << " from "
|
|
|
|
<< from.fileName() << " - filtered";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (contained.contains(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "Skipping already contained file " << filename << " from "
|
|
|
|
<< from.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
contained.insert(filename);
|
|
|
|
|
|
|
|
if (!fileInsideMod.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
|
|
|
|
|
|
|
if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to open " << filename << " in the jar";
|
|
|
|
fileInsideMod.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!JlCompress::copyData(fileInsideMod, zipOutFile))
|
|
|
|
{
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
}
|
|
|
|
return true;
|
2015-04-12 20:50:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 12:26:59 +00:00
|
|
|
bool MMCZip::compressDirFiles(QuaZip *zip, QString dir, QFileInfoList files)
|
|
|
|
{
|
|
|
|
QDir directory(dir);
|
|
|
|
if (!directory.exists()) return false;
|
|
|
|
|
|
|
|
for (auto e : files) {
|
|
|
|
auto filePath = directory.relativeFilePath(e.absoluteFilePath());
|
|
|
|
if( !JlCompress::compressFile(zip, e.absoluteFilePath(), filePath)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files)
|
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
|
|
|
|
if(!zip.open(QuaZip::mdCreate)) {
|
|
|
|
QFile::remove(fileCompressed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = compressDirFiles(&zip, dir, files);
|
|
|
|
|
|
|
|
zip.close();
|
|
|
|
if(zip.getZipError()!=0) {
|
|
|
|
QFile::remove(fileCompressed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 22:02:06 +00:00
|
|
|
// ours
|
2022-06-26 17:17:15 +00:00
|
|
|
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
|
|
|
QuaZip zipOut(targetJarPath);
|
|
|
|
if (!zipOut.open(QuaZip::mdCreate))
|
|
|
|
{
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to open the minecraft.jar for modding";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Files already added to the jar.
|
|
|
|
// These files will be skipped.
|
|
|
|
QSet<QString> addedFiles;
|
|
|
|
|
|
|
|
// Modify the jar
|
2022-07-23 17:21:28 +00:00
|
|
|
// This needs to be done in reverse-order to ensure we respect the loading order of components
|
|
|
|
for (auto i = mods.crbegin(); i != mods.crend(); i++)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
2022-07-23 17:21:28 +00:00
|
|
|
const auto* mod = *i;
|
2022-01-24 22:02:06 +00:00
|
|
|
// do not merge disabled mods.
|
2022-06-26 17:17:15 +00:00
|
|
|
if (!mod->enabled())
|
2022-01-24 22:02:06 +00:00
|
|
|
continue;
|
2022-08-09 04:53:50 +00:00
|
|
|
if (mod->type() == ResourceType::ZIPFILE)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
2022-06-26 17:17:15 +00:00
|
|
|
if (!mergeZipFiles(&zipOut, mod->fileinfo(), addedFiles))
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 17:17:15 +00:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 04:53:50 +00:00
|
|
|
else if (mod->type() == ResourceType::SINGLEFILE)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2022-06-26 17:17:15 +00:00
|
|
|
auto filename = mod->fileinfo();
|
2022-01-24 22:02:06 +00:00
|
|
|
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
|
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 17:17:15 +00:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addedFiles.insert(filename.fileName());
|
|
|
|
}
|
2022-08-09 04:53:50 +00:00
|
|
|
else if (mod->type() == ResourceType::FOLDER)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
2022-01-28 12:26:59 +00:00
|
|
|
// untested, but seems to be unused / not possible to reach
|
2022-01-24 22:02:06 +00:00
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2022-06-26 17:17:15 +00:00
|
|
|
auto filename = mod->fileinfo();
|
2022-01-24 22:02:06 +00:00
|
|
|
QString what_to_zip = filename.absoluteFilePath();
|
|
|
|
QDir dir(what_to_zip);
|
|
|
|
dir.cdUp();
|
|
|
|
QString parent_dir = dir.absolutePath();
|
2022-01-28 12:26:59 +00:00
|
|
|
auto files = QFileInfoList();
|
|
|
|
MMCZip::collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
|
|
|
|
|
|
|
|
for (auto e : files) {
|
|
|
|
if (addedFiles.contains(e.filePath()))
|
|
|
|
files.removeAll(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MMCZip::compressDirFiles(&zipOut, parent_dir, files))
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 17:17:15 +00:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qDebug() << "Adding folder " << filename.fileName() << " from "
|
|
|
|
<< filename.absoluteFilePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make sure we do not continue launching when something is missing or undefined...
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 17:17:15 +00:00
|
|
|
qCritical() << "Failed to add unknown mod type" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key){return !key.contains("META-INF");}))
|
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to insert minecraft.jar contents.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompress the jar
|
|
|
|
zipOut.close();
|
|
|
|
if (zipOut.getZipError() != 0)
|
|
|
|
{
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to finalize minecraft.jar!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-31 07:17:45 +00:00
|
|
|
// ours
|
2023-10-28 19:28:35 +00:00
|
|
|
std::pair<QString, QString> MMCZip::findFolderOfFileInZip(QuaZip * zip, QSet<const QString> what, const QString &root)
|
2015-09-13 02:21:26 +00:00
|
|
|
{
|
2023-10-28 19:28:35 +00:00
|
|
|
std::deque<QString> pathsToTraverse;
|
|
|
|
pathsToTraverse.push_back(root);
|
|
|
|
while (!pathsToTraverse.empty())
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
2023-10-28 19:28:35 +00:00
|
|
|
QString currentPath = pathsToTraverse.front();
|
|
|
|
pathsToTraverse.pop_front();
|
|
|
|
QuaZipDir rootDir(zip, currentPath);
|
|
|
|
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Files))
|
|
|
|
{
|
|
|
|
if (what.contains(fileName))
|
|
|
|
return {currentPath, fileName};
|
|
|
|
}
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
2024-01-06 13:44:44 +00:00
|
|
|
pathsToTraverse.push_back(rootDir.path() + fileName);
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-28 19:28:35 +00:00
|
|
|
return {QString(), QString()};
|
2015-09-13 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:17:45 +00:00
|
|
|
// ours
|
2015-09-13 02:21:26 +00:00
|
|
|
bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
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();
|
2015-09-13 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:17:45 +00:00
|
|
|
|
|
|
|
// ours
|
2022-07-20 12:21:44 +00:00
|
|
|
std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
2015-09-13 02:21:26 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QDir directory(target);
|
|
|
|
QStringList extracted;
|
2021-02-09 04:04:23 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
qDebug() << "Extracting subdir" << subdir << "from" << zip->getZipName() << "to" << target;
|
2021-02-09 04:04:23 +00:00
|
|
|
auto numEntries = zip->getEntriesCount();
|
|
|
|
if(numEntries < 0) {
|
|
|
|
qWarning() << "Failed to enumerate files in archive";
|
2022-07-20 12:21:44 +00:00
|
|
|
return std::nullopt;
|
2021-02-09 04:04:23 +00:00
|
|
|
}
|
|
|
|
else if(numEntries == 0) {
|
|
|
|
qDebug() << "Extracting empty archives seems odd...";
|
|
|
|
return extracted;
|
|
|
|
}
|
|
|
|
else if (!zip->goToFirstFile())
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
qWarning() << "Failed to seek to first file in zip";
|
2022-07-20 12:21:44 +00:00
|
|
|
return std::nullopt;
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
2021-02-09 04:04:23 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
QString name = zip->getCurrentFileName();
|
2023-02-09 18:38:34 +00:00
|
|
|
if(!QDir::cleanPath(name).startsWith(subdir))
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-09 18:38:34 +00:00
|
|
|
if (QDir::isAbsolutePath(name) || QDir::cleanPath(name).startsWith(".."))
|
|
|
|
{
|
|
|
|
qDebug() << "extractSubDir: Skipping file that tries to place itself in an absolute location or in a parent directory.";
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-16 13:50:46 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
name.remove(0, subdir.size());
|
2022-05-16 13:50:46 +00:00
|
|
|
auto original_name = name;
|
|
|
|
|
|
|
|
// Fix weird "folders with a single file get squashed" thing
|
|
|
|
QString path;
|
|
|
|
if(name.contains('/') && !name.endsWith('/')){
|
|
|
|
path = name.section('/', 0, -2) + "/";
|
2022-06-20 01:03:02 +00:00
|
|
|
FS::ensureFolderPathExists(FS::PathCombine(target, path));
|
2022-05-16 13:50:46 +00:00
|
|
|
|
|
|
|
name = name.split('/').last();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString absFilePath;
|
2018-07-15 12:51:05 +00:00
|
|
|
if(name.isEmpty())
|
|
|
|
{
|
2022-05-16 13:50:46 +00:00
|
|
|
absFilePath = directory.absoluteFilePath(name) + "/";
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
2022-05-16 13:50:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
absFilePath = directory.absoluteFilePath(path + name);
|
|
|
|
}
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!JlCompress::extractFile(zip, "", absFilePath))
|
|
|
|
{
|
2022-05-16 13:50:46 +00:00
|
|
|
qWarning() << "Failed to extract file" << original_name << "to" << absFilePath;
|
2018-07-15 12:51:05 +00:00
|
|
|
JlCompress::removeFile(extracted);
|
2022-07-20 12:21:44 +00:00
|
|
|
return std::nullopt;
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
2022-05-16 13:50:46 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
extracted.append(absFilePath);
|
2022-05-16 13:50:46 +00:00
|
|
|
QFile::setPermissions(absFilePath, QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser);
|
|
|
|
|
|
|
|
qDebug() << "Extracted file" << name << "to" << absFilePath;
|
2018-07-15 12:51:05 +00:00
|
|
|
} while (zip->goToNextFile());
|
|
|
|
return extracted;
|
2015-09-13 02:21:26 +00:00
|
|
|
}
|
2017-05-24 23:24:27 +00:00
|
|
|
|
2020-08-24 22:13:43 +00:00
|
|
|
// ours
|
|
|
|
bool MMCZip::extractRelFile(QuaZip *zip, const QString &file, const QString &target)
|
|
|
|
{
|
|
|
|
return JlCompress::extractFile(zip, file, target);
|
|
|
|
}
|
|
|
|
|
2017-05-31 07:17:45 +00:00
|
|
|
// ours
|
2022-07-20 12:21:44 +00:00
|
|
|
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
2017-05-24 23:24:27 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
|
|
|
if(fileInfo.size() == 22) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
2022-07-20 12:21:44 +00:00
|
|
|
return std::nullopt;
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
return MMCZip::extractSubDir(&zip, "", dir);
|
2017-05-24 23:24:27 +00:00
|
|
|
}
|
2020-08-24 22:13:43 +00:00
|
|
|
|
|
|
|
// ours
|
2022-07-20 12:21:44 +00:00
|
|
|
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
2020-08-24 22:13:43 +00:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
|
|
|
if(fileInfo.size() == 22) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
2022-07-20 12:21:44 +00:00
|
|
|
return std::nullopt;
|
2020-08-24 22:13:43 +00:00
|
|
|
}
|
|
|
|
return MMCZip::extractSubDir(&zip, subdir, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ours
|
|
|
|
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
|
|
|
if(fileInfo.size() == 22) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
|
|
|
return false;
|
2020-08-24 22:13:43 +00:00
|
|
|
}
|
|
|
|
return MMCZip::extractRelFile(&zip, file, target);
|
|
|
|
}
|
2022-01-28 11:37:22 +00:00
|
|
|
|
|
|
|
bool MMCZip::collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList *files,
|
|
|
|
MMCZip::FilterFunction excludeFilter) {
|
|
|
|
QDir rootDirectory(rootDir);
|
|
|
|
if (!rootDirectory.exists()) return false;
|
|
|
|
|
|
|
|
QDir directory;
|
|
|
|
if (subDir == nullptr)
|
|
|
|
directory = rootDirectory;
|
|
|
|
else
|
|
|
|
directory = QDir(subDir);
|
|
|
|
|
|
|
|
if (!directory.exists()) return false; // shouldn't ever happen
|
|
|
|
|
|
|
|
// recurse directories
|
|
|
|
QFileInfoList entries = directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
|
|
|
|
for (const auto& e: entries) {
|
|
|
|
if (!collectFileListRecursively(rootDir, e.filePath(), files, excludeFilter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect files
|
|
|
|
entries = directory.entryInfoList(QDir::Files);
|
|
|
|
for (const auto& e: entries) {
|
|
|
|
QString relativeFilePath = rootDirectory.relativeFilePath(e.absoluteFilePath());
|
|
|
|
if (excludeFilter && excludeFilter(relativeFilePath)) {
|
|
|
|
qDebug() << "Skipping file " << relativeFilePath;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-02 19:34:55 +00:00
|
|
|
files->append(e); // we want the original paths for MMCZip::compressDirFiles
|
2022-01-28 11:37:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|