Repository navigation
Expand file tree
/
Copy pathtool.cpp
More file actions
352 lines (298 loc) · 11.4 KB
/
Copy pathtool.cpp
File metadata and controls
352 lines (298 loc) · 11.4 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// ============================================================
// tool.cpp
// جميع عمليات الاستخراج وعرض المحتويات
// ============================================================
#include "tool.h"
#include "debughelper.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QApplication>
#include <QDebug>
#include <quazip5/JlCompress.h>
#include <quazip5/quazip.h>
#include <quazip5/quazipfile.h>
#include <quazip5/quazipfileinfo.h>
// ============================================================
// الثوابت (Constants)
// ============================================================
namespace {
const QString ERROR_FILE_NOT_FOUND = "File not found: ";
const QString ERROR_EXTRACT_FAILED = "Extraction failed: The file is corrupt or unsupported.";
const QString ERROR_LIST_FAILED = "Failed to display content: the file is corrupted or unsupported.";
const QString ERROR_CREATE_DIR = "Failed to create the destination folder: ";
bool isZipDirectoryEntry(const QuaZipFileInfo64 &info)
{
if (info.name.endsWith('/')) {
return true;
}
const quint32 dosAttributes = info.externalAttr & 0xFF;
if ((dosAttributes & 0x10) != 0) {
return true;
}
const quint32 unixMode = info.externalAttr >> 16;
return (unixMode & 0xF000) == 0x4000;
}
QString zipOutputPath(const QString &rootPath, const QString &entryName, bool &valid)
{
valid = false;
const QString cleanRoot = QDir::cleanPath(rootPath);
const QString candidate = QDir::cleanPath(cleanRoot + QLatin1Char('/') + entryName);
if (candidate != cleanRoot && !candidate.startsWith(cleanRoot + QLatin1Char('/'))) {
return QString();
}
valid = true;
return candidate;
}
}
// ============================================================
// دوال مساعدة (Helper Functions)
// ============================================================
static bool fileExists(const QString &filePath, QString &errorMessage)
{
if (!QFileInfo(filePath).exists()) {
errorMessage = ERROR_FILE_NOT_FOUND + filePath;
return false;
}
return true;
}
static bool createOutputDir(const QString &outputPath, QString &errorMessage)
{
if (!QDir().mkpath(outputPath)) {
errorMessage = ERROR_CREATE_DIR + outputPath;
return false;
}
return true;
}
// ============================================================
// عرض محتويات الملف المضغوط
// ============================================================
QStringList getArchiveContents(const QString &filePath, QString &errorMessage)
{
debugFunctionStart("getArchiveContents");
errorMessage.clear();
// 1. التحقق من وجود الملف
if (!fileExists(filePath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("getArchiveContents");
return QStringList();
}
// 2. قراءة المحتويات
QString nativePath = QDir::toNativeSeparators(filePath);
debugVariable("Native Path", nativePath);
QStringList files = JlCompress::getFileList(nativePath);
if (files.isEmpty()) {
errorMessage = "The file is empty, corrupt, or unsupported.";
debugError(errorMessage);
debugFunctionEnd("getArchiveContents");
return QStringList();
}
debugFileList("Files in archive", files);
debugFunctionEnd("getArchiveContents");
return files;
}
// ============================================================
// استخراج الملفات (بدون تقدم)
// ============================================================
bool extractArchive(const QString &filePath, const QString &outputPath, QString &errorMessage)
{
debugFunctionStart("extractArchive");
debugVariable("filePath", filePath);
debugVariable("outputPath", outputPath);
errorMessage.clear();
// 1. تحديد مجلد الوجهة النهائي
QString finalOutputPath = outputPath;
if (finalOutputPath.isEmpty()) {
finalOutputPath = QFileInfo(filePath).absolutePath() + "/extracted";
}
debugVariable("finalOutputPath", finalOutputPath);
// 2. التحقق من وجود الملف
if (!fileExists(filePath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractArchive");
return false;
}
// 3. إنشاء مجلد الوجهة
if (!createOutputDir(finalOutputPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractArchive");
return false;
}
// 4. تحويل المسارات
QString nativePath = QDir::toNativeSeparators(filePath);
QString nativeOutput = QDir::toNativeSeparators(finalOutputPath);
debugVariable("nativePath", nativePath);
debugVariable("nativeOutput", nativeOutput);
// 5. محاولة extractDir أولاً
QStringList extractedFiles = JlCompress::extractDir(nativePath, nativeOutput);
if (!extractedFiles.isEmpty()) {
debugQuaZipResult("extractDir", true, QString::number(extractedFiles.size()) + " files");
debugFunctionEnd("extractArchive");
return true;
}
// 6. إذا فشل extractDir، استخدم extractFile لكل ملف
debugError("extractDir فشل، نحاول extractFile");
QStringList allFiles = JlCompress::getFileList(nativePath);
if (allFiles.isEmpty()) {
errorMessage = "The file is corrupt or unsupported.";
debugError(errorMessage);
debugFunctionEnd("extractArchive");
return false;
}
bool anyFileExtracted = false;
for (const QString &singleFile : allFiles) {
QApplication::processEvents();
QString result = JlCompress::extractFile(nativePath, singleFile, nativeOutput);
if (!result.isEmpty()) {
anyFileExtracted = true;
debugExtractFile(singleFile, true);
} else {
debugExtractFile(singleFile, false);
}
}
if (!anyFileExtracted) {
errorMessage = "Failed to extract any file from the archive.";
debugError(errorMessage);
debugFunctionEnd("extractArchive");
return false;
}
debugQuaZipResult("extractFile", true, "Some files extracted");
debugFunctionEnd("extractArchive");
return true;
}
// ============================================================
// استخراج الملفات مع تقدم تدريجي (Progress)
// ============================================================
bool extractArchiveWithProgress(const QString &filePath, const QString &outputPath, QString &errorMessage,
ExtractProgressCallback progressCallback)
{
debugFunctionStart("extractArchiveWithProgress");
errorMessage.clear();
if (!fileExists(filePath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
QString finalOutputPath = outputPath;
if (finalOutputPath.isEmpty()) {
finalOutputPath = QFileInfo(filePath).absolutePath() + "/extracted";
}
if (!createOutputDir(finalOutputPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
const QString nativePath = QDir::toNativeSeparators(filePath);
const QString nativeOutput = QDir::toNativeSeparators(finalOutputPath);
const QString rootPath = QDir(nativeOutput).absolutePath();
QuaZip zip(nativePath);
if (!zip.open(QuaZip::mdUnzip)) {
errorMessage = ERROR_EXTRACT_FAILED;
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
const QList<QuaZipFileInfo64> entries = zip.getFileInfoList64();
if (entries.isEmpty()) {
errorMessage = "The file is empty, corrupt, or unsupported.";
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
int totalFiles = 0;
for (const QuaZipFileInfo64 &entry : entries) {
if (!isZipDirectoryEntry(entry)) {
++totalFiles;
}
}
int currentFile = 0;
int extractedCount = 0;
bool anySuccess = false;
bool more = zip.goToFirstFile();
while (more) {
QuaZipFileInfo64 entry;
if (!zip.getCurrentFileInfo(&entry)) {
errorMessage = ERROR_EXTRACT_FAILED;
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
bool validPath = false;
const QString outputFilePath = zipOutputPath(rootPath, entry.name, validPath);
if (!validPath) {
if (!isZipDirectoryEntry(entry)) {
++currentFile;
if (progressCallback) {
progressCallback(currentFile, totalFiles, entry.name);
}
}
qWarning() << "Skipping unsafe ZIP path:" << entry.name;
more = zip.goToNextFile();
continue;
}
if (isZipDirectoryEntry(entry)) {
if (QDir().mkpath(outputFilePath)) {
anySuccess = true;
} else {
qWarning() << "Failed to create ZIP directory:" << outputFilePath;
}
more = zip.goToNextFile();
continue;
}
++currentFile;
bool extracted = false;
const QFileInfo outputInfo(outputFilePath);
if (QDir().mkpath(outputInfo.absolutePath())) {
QuaZipFile zipFile(&zip);
if (zipFile.open(QIODevice::ReadOnly)) {
QFile outFile(outputFilePath);
if (outFile.open(QIODevice::WriteOnly)) {
extracted = true;
while (!zipFile.atEnd()) {
const QByteArray chunk = zipFile.read(64 * 1024);
if (chunk.isEmpty()) {
if (zipFile.getZipError() != 0) {
extracted = false;
}
break;
}
if (outFile.write(chunk) != chunk.size()) {
extracted = false;
break;
}
}
outFile.close();
}
zipFile.close();
} else {
qWarning() << "Failed to open ZIP entry:" << entry.name << zipFile.errorString();
}
}
if (extracted) {
const QFile::Permissions permissions = entry.getPermissions();
if (permissions != QFileDevice::Permissions()) {
QFile::setPermissions(outputFilePath, permissions);
}
++extractedCount;
anySuccess = true;
debugExtractFile(entry.name, true);
} else {
QFile::remove(outputFilePath);
debugExtractFile(entry.name, false);
}
if (progressCallback) {
progressCallback(currentFile, totalFiles, entry.name);
}
more = zip.goToNextFile();
}
if (!anySuccess) {
errorMessage = "Failed to extract any file.";
debugError(errorMessage);
debugFunctionEnd("extractArchiveWithProgress");
return false;
}
debugQuaZipResult("extractArchiveWithProgress", true,
QString::number(extractedCount) + " files extracted");
debugFunctionEnd("extractArchiveWithProgress");
return true;
}