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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
#include "quazipdir.h"
#include <QSet>
#include <QSharedData>
class QuaZipDirPrivate: public QSharedData {
friend class QuaZipDir;
private:
QuaZipDirPrivate(QuaZip *zip, const QString &dir = QString()):
zip(zip), dir(dir), caseSensitivity(QuaZip::csDefault),
filter(QDir::NoFilter), sorting(QDir::NoSort) {}
QuaZip *zip;
QString dir;
QuaZip::CaseSensitivity caseSensitivity;
QDir::Filters filter;
QStringList nameFilters;
QDir::SortFlags sorting;
template<typename TFileInfoList>
bool entryInfoList(QStringList nameFilters, QDir::Filters filter,
QDir::SortFlags sort, TFileInfoList &result) const;
inline QString simplePath() const {return QDir::cleanPath(dir);}
};
QuaZipDir::QuaZipDir(const QuaZipDir &that):
d(that.d)
{
}
QuaZipDir::QuaZipDir(QuaZip *zip, const QString &dir):
d(new QuaZipDirPrivate(zip, dir))
{
if (d->dir.startsWith('/'))
d->dir = d->dir.mid(1);
}
QuaZipDir::~QuaZipDir()
{
}
bool QuaZipDir::operator==(const QuaZipDir &that)
{
return d->zip == that.d->zip && d->dir == that.d->dir;
}
QuaZipDir& QuaZipDir::operator=(const QuaZipDir &that)
{
this->d = that.d;
return *this;
}
QString QuaZipDir::operator[](int pos) const
{
return entryList().at(pos);
}
QuaZip::CaseSensitivity QuaZipDir::caseSensitivity() const
{
return d->caseSensitivity;
}
bool QuaZipDir::cd(const QString &directoryName)
{
if (directoryName == "/") {
d->dir = "";
return true;
}
QString dirName = directoryName;
if (dirName.endsWith('/'))
dirName.chop(1);
if (dirName.contains('/')) {
QuaZipDir dir(*this);
if (dirName.startsWith('/')) {
#ifdef QUAZIP_QUAZIPDIR_DEBUG
qDebug("QuaZipDir::cd(%s): going to /",
dirName.toUtf8().constData());
#endif
if (!dir.cd("/"))
return false;
}
QStringList path = dirName.split('/', QString::SkipEmptyParts);
for (QStringList::const_iterator i = path.constBegin();
i != path.end();
++i) {
const QString &step = *i;
#ifdef QUAZIP_QUAZIPDIR_DEBUG
qDebug("QuaZipDir::cd(%s): going to %s",
dirName.toUtf8().constData(),
step.toUtf8().constData());
#endif
if (!dir.cd(step))
return false;
}
d->dir = dir.path();
return true;
} else { // no '/'
if (dirName == ".") {
return true;
} else if (dirName == "..") {
if (isRoot()) {
return false;
} else {
int slashPos = d->dir.lastIndexOf('/');
if (slashPos == -1) {
d->dir = "";
} else {
d->dir = d->dir.left(slashPos);
}
return true;
}
} else { // a simple subdirectory
if (exists(dirName)) {
if (isRoot())
d->dir = dirName;
else
d->dir += "/" + dirName;
return true;
} else {
return false;
}
}
}
}
bool QuaZipDir::cdUp()
{
return cd("..");
}
uint QuaZipDir::count() const
{
return entryList().count();
}
QString QuaZipDir::dirName() const
{
return QDir(d->dir).dirName();
}
QuaZipFileInfo QuaZipDir_getFileInfo(QuaZip *zip, bool *ok,
const QString &relativeName,
bool isReal)
{
QuaZipFileInfo info;
if (isReal) {
*ok = zip->getCurrentFileInfo(&info);
} else {
*ok = true;
info.compressedSize = 0;
info.crc = 0;
info.diskNumberStart = 0;
info.externalAttr = 0;
info.flags = 0;
info.internalAttr = 0;
info.method = 0;
info.uncompressedSize = 0;
info.versionCreated = info.versionNeeded = 0;
}
info.name = relativeName;
return info;
}
template<typename TFileInfoList>
void QuaZipDir_convertInfoList(const QList<QuaZipFileInfo> &from, TFileInfoList &to);
template<>
void QuaZipDir_convertInfoList(const QList<QuaZipFileInfo> &from, QList<QuaZipFileInfo> &to)
{
to = from;
}
template<>
void QuaZipDir_convertInfoList(const QList<QuaZipFileInfo> &from, QStringList &to)
{
to.clear();
for (QList<QuaZipFileInfo>::const_iterator i = from.constBegin();
i != from.constEnd();
++i) {
to.append(i->name);
}
}
// utility class to restore the current file
class QuaZipDirRestoreCurrent {
public:
inline QuaZipDirRestoreCurrent(QuaZip *zip):
zip(zip), currentFile(zip->getCurrentFileName()) {}
inline ~QuaZipDirRestoreCurrent()
{
zip->setCurrentFile(currentFile);
}
private:
QuaZip *zip;
QString currentFile;
};
class QuaZipDirComparator
{
private:
QDir::SortFlags sort;
static QString getExtension(const QString &name);
int compareStrings(const QString &string1, const QString &string2);
public:
inline QuaZipDirComparator(QDir::SortFlags sort): sort(sort) {}
bool operator()(const QuaZipFileInfo &info1, const QuaZipFileInfo &info2);
};
QString QuaZipDirComparator::getExtension(const QString &name)
{
if (name.endsWith('.') || name.indexOf('.', 1) == -1) {
return "";
} else {
return name.mid(name.lastIndexOf('.') + 1);
}
}
int QuaZipDirComparator::compareStrings(const QString &string1,
const QString &string2)
{
if (sort & QDir::LocaleAware) {
if (sort & QDir::IgnoreCase) {
return string1.toLower().localeAwareCompare(string2.toLower());
} else {
return string1.localeAwareCompare(string2);
}
} else {
return string1.compare(string2, (sort & QDir::IgnoreCase)
? Qt::CaseInsensitive : Qt::CaseSensitive);
}
}
bool QuaZipDirComparator::operator()(const QuaZipFileInfo &info1,
const QuaZipFileInfo &info2)
{
QDir::SortFlags order = sort
& (QDir::Name | QDir::Time | QDir::Size | QDir::Type);
if ((sort & QDir::DirsFirst) == QDir::DirsFirst
|| (sort & QDir::DirsLast) == QDir::DirsLast) {
if (info1.name.endsWith('/') && !info2.name.endsWith('/'))
return (sort & QDir::DirsFirst) == QDir::DirsFirst;
else if (!info1.name.endsWith('/') && info2.name.endsWith('/'))
return (sort & QDir::DirsLast) == QDir::DirsLast;
}
bool result;
int extDiff;
switch (order) {
case QDir::Name:
result = compareStrings(info1.name, info2.name) < 0;
break;
case QDir::Type:
extDiff = compareStrings(getExtension(info1.name),
getExtension(info2.name));
if (extDiff == 0) {
result = compareStrings(info1.name, info2.name) < 0;
} else {
result = extDiff < 0;
}
break;
case QDir::Size:
if (info1.uncompressedSize == info2.uncompressedSize) {
result = compareStrings(info1.name, info2.name) < 0;
} else {
result = info1.uncompressedSize < info2.uncompressedSize;
}
break;
case QDir::Time:
if (info1.dateTime == info2.dateTime) {
result = compareStrings(info1.name, info2.name) < 0;
} else {
result = info1.dateTime < info2.dateTime;
}
break;
default:
qWarning("QuaZipDirComparator(): Invalid sort mode 0x%2X",
static_cast<unsigned>(sort));
return false;
}
return (sort & QDir::Reversed) ? !result : result;
}
template<typename TFileInfoList>
bool QuaZipDirPrivate::entryInfoList(QStringList nameFilters,
QDir::Filters filter, QDir::SortFlags sort, TFileInfoList &result) const
{
QString basePath = simplePath();
if (!basePath.isEmpty())
basePath += "/";
int baseLength = basePath.length();
result.clear();
QuaZipDirRestoreCurrent saveCurrent(zip);
if (!zip->goToFirstFile()) {
return zip->getZipError() == UNZ_OK;
}
QDir::Filters fltr = filter;
if (fltr == QDir::NoFilter)
fltr = this->filter;
if (fltr == QDir::NoFilter)
fltr = QDir::AllEntries;
QStringList nmfltr = nameFilters;
if (nmfltr.isEmpty())
nmfltr = this->nameFilters;
QSet<QString> dirsFound;
QList<QuaZipFileInfo> list;
do {
QString name = zip->getCurrentFileName();
if (!name.startsWith(basePath))
continue;
QString relativeName = name.mid(baseLength);
bool isDir = false;
bool isReal = true;
if (relativeName.contains('/')) {
int indexOfSlash = relativeName.indexOf('/');
// something like "subdir/"
isReal = indexOfSlash == relativeName.length() - 1;
relativeName = relativeName.left(indexOfSlash + 1);
if (dirsFound.contains(relativeName))
continue;
isDir = true;
}
dirsFound.insert(relativeName);
if ((fltr & QDir::Dirs) == 0 && isDir)
continue;
if ((fltr & QDir::Files) == 0 && !isDir)
continue;
if (!nmfltr.isEmpty() && QDir::match(nmfltr, relativeName))
continue;
bool ok;
QuaZipFileInfo info = QuaZipDir_getFileInfo(zip, &ok, relativeName,
isReal);
if (!ok) {
return false;
}
list.append(info);
} while (zip->goToNextFile());
QDir::SortFlags srt = sort;
if (srt == QDir::NoSort)
srt = sorting;
#ifdef QUAZIP_QUAZIPDIR_DEBUG
qDebug("QuaZipDirPrivate::entryInfoList(): before sort:");
foreach (QuaZipFileInfo info, list) {
qDebug("%s\t%s", info.name.toUtf8().constData(),
info.dateTime.toString(Qt::ISODate).toUtf8().constData());
}
#endif
if (srt != QDir::NoSort && (srt & QDir::Unsorted) != QDir::Unsorted) {
if (QuaZip::convertCaseSensitivity(caseSensitivity)
== Qt::CaseInsensitive)
srt |= QDir::IgnoreCase;
QuaZipDirComparator lessThan(srt);
qSort(list.begin(), list.end(), lessThan);
}
QuaZipDir_convertInfoList(list, result);
return true;
}
QList<QuaZipFileInfo> QuaZipDir::entryInfoList(const QStringList &nameFilters,
QDir::Filters filters, QDir::SortFlags sort) const
{
QList<QuaZipFileInfo> result;
if (d->entryInfoList(nameFilters, filters, sort, result))
return result;
else
return QList<QuaZipFileInfo>();
}
QList<QuaZipFileInfo> QuaZipDir::entryInfoList(QDir::Filters filters,
QDir::SortFlags sort) const
{
return entryInfoList(QStringList(), filters, sort);
}
QStringList QuaZipDir::entryList(const QStringList &nameFilters,
QDir::Filters filters, QDir::SortFlags sort) const
{
QStringList result;
if (d->entryInfoList(nameFilters, filters, sort, result))
return result;
else
return QStringList();
}
QStringList QuaZipDir::entryList(QDir::Filters filters,
QDir::SortFlags sort) const
{
return entryList(QStringList(), filters, sort);
}
bool QuaZipDir::exists(const QString &filePath) const
{
if (filePath == "/")
return true;
QString fileName = filePath;
if (fileName.endsWith('/'))
fileName.chop(1);
if (fileName.contains('/')) {
QFileInfo fileInfo(fileName);
#ifdef QUAZIP_QUAZIPDIR_DEBUG
qDebug("QuaZipDir::exists(): fileName=%s, fileInfo.fileName()=%s, "
"fileInfo.path()=%s", fileName.toUtf8().constData(),
fileInfo.fileName().toUtf8().constData(),
fileInfo.path().toUtf8().constData());
#endif
QuaZipDir dir(*this);
return dir.cd(fileInfo.path()) && dir.exists(fileInfo.fileName());
} else {
if (fileName == "..") {
return !isRoot();
} else if (fileName == ".") {
return true;
} else {
QStringList entries = entryList(QDir::AllEntries, QDir::NoSort);
#ifdef QUAZIP_QUAZIPDIR_DEBUG
qDebug("QuaZipDir::exists(): looking for %s",
fileName.toUtf8().constData());
for (QStringList::const_iterator i = entries.constBegin();
i != entries.constEnd();
++i) {
qDebug("QuaZipDir::exists(): entry: %s",
i->toUtf8().constData());
}
#endif
Qt::CaseSensitivity cs = QuaZip::convertCaseSensitivity(
d->caseSensitivity);
if (filePath.endsWith('/')) {
return entries.contains(filePath, cs);
} else {
return entries.contains(fileName, cs)
|| entries.contains(fileName + "/", cs);
}
}
}
}
bool QuaZipDir::exists() const
{
QDir thisDir(d->dir);
return QuaZipDir(d->zip, thisDir.filePath("..")).exists(thisDir.dirName());
}
QString QuaZipDir::filePath(const QString &fileName) const
{
return QDir(d->dir).filePath(fileName);
}
QDir::Filters QuaZipDir::filter()
{
return d->filter;
}
bool QuaZipDir::isRoot() const
{
return d->simplePath().isEmpty();
}
QStringList QuaZipDir::nameFilters() const
{
return d->nameFilters;
}
QString QuaZipDir::path() const
{
return d->dir;
}
QString QuaZipDir::relativeFilePath(const QString &fileName) const
{
return QDir(d->dir).relativeFilePath(fileName);
}
void QuaZipDir::setCaseSensitivity(QuaZip::CaseSensitivity caseSensitivity)
{
d->caseSensitivity = caseSensitivity;
}
void QuaZipDir::setFilter(QDir::Filters filters)
{
d->filter = filters;
}
void QuaZipDir::setNameFilters(const QStringList &nameFilters)
{
d->nameFilters = nameFilters;
}
void QuaZipDir::setPath(const QString &path)
{
QString newDir = path;
if (newDir == "/") {
d->dir = "";
} else {
if (newDir.endsWith('/'))
newDir.chop(1);
if (newDir.startsWith('/'))
newDir = newDir.mid(1);
d->dir = newDir;
}
}
void QuaZipDir::setSorting(QDir::SortFlags sort)
{
d->sorting = sort;
}
QDir::SortFlags QuaZipDir::sorting() const
{
return d->sorting;
}
|