-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractZipTask.cpp
More file actions
135 lines (117 loc) · 5 KB
/
ExtractZipTask.cpp
File metadata and controls
135 lines (117 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2025 Trial97 <alexandru.tripon97@gmail.com>
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ExtractZipTask.h"
#include <QtConcurrent>
#include "FileSystem.h"
#include "archive/ArchiveReader.h"
#include "archive/ArchiveWriter.h"
namespace MMCZip {
void ExtractZipTask::executeTask()
{
m_zipFuture = QtConcurrent::run(QThreadPool::globalInstance(), [this]() { return extractZip(); });
connect(&m_zipWatcher, &QFutureWatcher<ZipResult>::finished, this, &ExtractZipTask::finish);
m_zipWatcher.setFuture(m_zipFuture);
}
auto ExtractZipTask::extractZip() -> ZipResult
{
auto target = m_outputDir.absolutePath();
auto target_top_dir = QUrl::fromLocalFile(target);
QStringList extracted;
qDebug() << "Extracting subdir" << m_subdirectory << "from" << m_input.getZipName() << "to" << target;
if (!m_input.collectFiles()) {
return ZipResult(tr("Failed to enumerate files in archive"));
}
if (m_input.getFiles().isEmpty()) {
logWarning(tr("Extracting empty archives seems odd..."));
return ZipResult();
}
auto extPtr = ArchiveWriter::createDiskWriter();
auto ext = extPtr.get();
setStatus("Extracting files...");
setProgress(0, m_input.getFiles().count());
ZipResult result;
auto fileName = m_input.getZipName();
if (!m_input.parse([this, &result, &target, &target_top_dir, ext, &extracted](ArchiveReader::File* f) {
if (m_zipFuture.isCanceled())
return false;
setProgress(m_progress + 1, m_progressTotal);
QString file_name = f->filename();
if (!file_name.startsWith(m_subdirectory)) {
f->skip();
return true;
}
auto relative_file_name = QDir::fromNativeSeparators(file_name.mid(m_subdirectory.size()));
auto original_name = relative_file_name;
setStatus("Unpacking: " + relative_file_name);
// Fix subdirs/files ending with a / getting transformed into absolute paths
if (relative_file_name.startsWith('/'))
relative_file_name = relative_file_name.mid(1);
// Fix weird "folders with a single file get squashed" thing
QString sub_path;
if (relative_file_name.contains('/') && !relative_file_name.endsWith('/')) {
sub_path = relative_file_name.section('/', 0, -2) + '/';
FS::ensureFolderPathExists(FS::PathCombine(target, sub_path));
relative_file_name = relative_file_name.split('/').last();
}
QString target_file_path;
if (relative_file_name.isEmpty()) {
target_file_path = target + '/';
} else {
target_file_path = FS::PathCombine(target_top_dir.toLocalFile(), sub_path, relative_file_name);
if (relative_file_name.endsWith('/') && !target_file_path.endsWith('/'))
target_file_path += '/';
}
if (!target_top_dir.isParentOf(QUrl::fromLocalFile(target_file_path))) {
result = ZipResult(tr("Extracting %1 was cancelled, because it was effectively outside of the target path %2")
.arg(relative_file_name, target));
return false;
}
if (!f->writeFile(ext, target_file_path)) {
result = ZipResult(tr("Failed to extract file %1 to %2").arg(original_name, target_file_path));
return false;
}
extracted.append(target_file_path);
qDebug() << "Extracted file" << relative_file_name << "to" << target_file_path;
return true;
})) {
FS::removeFiles(extracted);
return result.has_value() ? result : ZipResult(tr("Failed to parse file %1").arg(fileName));
}
return ZipResult();
}
void ExtractZipTask::finish()
{
if (m_zipFuture.isCanceled()) {
emitAborted();
} else if (auto result = m_zipFuture.result(); result.has_value()) {
emitFailed(result.value());
} else {
emitSucceeded();
}
}
bool ExtractZipTask::abort()
{
if (m_zipFuture.isRunning()) {
m_zipFuture.cancel();
// NOTE: Here we don't do `emitAborted()` because it will be done when `m_build_zip_future` actually cancels, which may not occur
// immediately.
return true;
}
return false;
}
} // namespace MMCZip