Skip to content

Commit aab2f0b

Browse files
cen1lazyone233
andauthored
Feature/strategy options (#206)
* Adds compression strategy (level, method) to JlCompress --------- Co-authored-by: ran <[email protected]>
1 parent 89bef11 commit aab2f0b

File tree

4 files changed

+296
-115
lines changed

4 files changed

+296
-115
lines changed

quazip/JlCompress.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ bool JlCompress::compressFile(QuaZip* zip, QString fileName, QString fileDest, c
5555

5656
QuaZipFile outFile(zip);
5757
if (options.getDateTime().isNull()) {
58-
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName))) return false;
58+
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName), nullptr, 0, options.getCompressionMethod(), options.getCompressionLevel())) return false;
5959
}
6060
else {
61-
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName, options.getDateTime()))) return false;
61+
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName, options.getDateTime()), nullptr, 0, options.getCompressionMethod(), options.getCompressionLevel())) return false;
6262
}
6363

6464
QFileInfo input(fileName);
@@ -106,13 +106,13 @@ bool JlCompress::compressSubDir(QuaZip* zip, QString dir, QString origDir, bool
106106
QuaZipFile dirZipFile(zip);
107107
std::unique_ptr<QuaZipNewInfo> qzni;
108108
if (options.getDateTime().isNull()) {
109-
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir);
109+
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir);
110110
}
111111
else {
112-
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir, options.getDateTime());
112+
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir, options.getDateTime());
113113
}
114114
if (!dirZipFile.open(QIODevice::WriteOnly, *qzni, nullptr, 0, 0)) {
115-
return false;
115+
return false;
116116
}
117117
dirZipFile.close();
118118
}

quazip/JlCompress.h

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,71 @@ class QUAZIP_EXPORT JlCompress {
4444
public:
4545
class Options {
4646
public:
47-
explicit Options(const QDateTime& dateTime = QDateTime())
48-
: m_dateTime(dateTime) {}
47+
/**
48+
* The enum values refer to the comments in the open function of the quazipfile.h file.
49+
*
50+
* The value is represented by two hexadecimal characters,
51+
* the left character indicating the compression method,
52+
* and the right character indicating the compression level.
53+
*
54+
* method == 0 indicates that the file is not compressed but rather stored as is.
55+
* method == 8(Z_DEFLATED) indicates that zlib compression is used.
56+
*
57+
* A higher value of level indicates a smaller size of the compressed file,
58+
* although it also implies more time consumed during the compression process.
59+
*/
60+
enum CompressionStrategy
61+
{
62+
/// Storage without compression
63+
Storage = 0x00, // Z_NO_COMPRESSION 0
64+
/// The fastest compression speed
65+
Fastest = 0x81, // Z_BEST_SPEED 1
66+
/// Relatively fast compression speed
67+
Faster = 0x83,
68+
/// Standard compression speed and ratio
69+
Standard = 0x86,
70+
/// Better compression ratio
71+
Better = 0x87,
72+
/// The best compression ratio
73+
Best = 0x89, // Z_BEST_COMPRESSION 9
74+
/// The default compression strategy, according to the open function of quazipfile.h,
75+
/// the value of method is Z_DEFLATED, and the value of level is Z_DEFAULT_COMPRESSION -1 (equals lvl 6)
76+
Default = 0xff
77+
};
4978

50-
QDateTime getDateTime() const {
51-
return m_dateTime;
52-
}
79+
public:
80+
explicit Options(const QDateTime& dateTime = QDateTime(), const CompressionStrategy& strategy = Default)
81+
: m_dateTime(dateTime), m_compressionStrategy(strategy) {}
82+
83+
QDateTime getDateTime() const {
84+
return m_dateTime;
85+
}
86+
87+
void setDateTime(const QDateTime &dateTime) {
88+
m_dateTime = dateTime;
89+
}
90+
91+
CompressionStrategy getCompressionStrategy() const {
92+
return m_compressionStrategy;
93+
}
94+
95+
int getCompressionMethod() const {
96+
return m_compressionStrategy != Default ? m_compressionStrategy >> 4 : Z_DEFLATED;
97+
}
98+
99+
int getCompressionLevel() const {
100+
return m_compressionStrategy != Default ? m_compressionStrategy & 0x0f : Z_DEFAULT_COMPRESSION;
101+
}
53102

54-
void setDateTime(const QDateTime &dateTime) {
55-
m_dateTime = dateTime;
56-
}
103+
void setCompressionStrategy(const CompressionStrategy &strategy) {
104+
m_compressionStrategy = strategy;
105+
}
57106

58107
private:
59-
// If set, used as last modified on file inside the archive.
60-
// If compressing a directory, used for all files.
61-
QDateTime m_dateTime;
108+
// If set, used as last modified on file inside the archive.
109+
// If compressing a directory, used for all files.
110+
QDateTime m_dateTime;
111+
CompressionStrategy m_compressionStrategy;
62112
};
63113

64114
static bool copyData(QIODevice &inFile, QIODevice &outFile);
@@ -287,7 +337,7 @@ class QUAZIP_EXPORT JlCompress {
287337
list of the entries, including both files and directories if they
288338
are present separately.
289339
*/
290-
static QStringList getFileList(QIODevice *ioDevice);
340+
static QStringList getFileList(QIODevice *ioDevice);
291341
};
292342

293343
#endif /* JLCOMPRESSFOLDER_H_ */

qztest/qztest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,40 @@ bool createTestFiles(const QStringList &fileNames, int size, const QString &dir)
5656
testDir.path().toUtf8().constData());
5757
return false;
5858
}
59+
//qDebug() << "Created path " << testDir.path();
60+
QFile dirFile(testDir.path());
61+
if (!dirFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner |
62+
QFileDevice::ReadGroup | QFileDevice::ExeGroup |
63+
QFileDevice::ReadOther | QFileDevice::ExeOther)) {
64+
qWarning("Couldn't set permissions for %s",
65+
testDir.path().toUtf8().constData());
66+
return false;
67+
}
5968
}
6069
if (fileName.endsWith('/')) {
6170
if (!curDir.mkpath(filePath)) {
6271
qWarning("Couldn't mkpath %s",
6372
fileName.toUtf8().constData());
6473
return false;
6574
}
75+
//qDebug() << "Created path " << filePath;
76+
QFile dirFile(filePath);
77+
if (!dirFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner |
78+
QFileDevice::ReadGroup | QFileDevice::ExeGroup |
79+
QFileDevice::ReadOther | QFileDevice::ExeOther)) {
80+
qWarning("Couldn't set permissions for %s",
81+
filePath.toUtf8().constData());
82+
return false;
83+
}
6684
} else {
6785
QFile testFile(filePath);
6886
if (!testFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
6987
qWarning("Couldn't create %s",
7088
fileName.toUtf8().constData());
7189
return false;
7290
}
91+
testFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner |
92+
QFileDevice::ReadGroup | QFileDevice::ReadOther);
7393
if (size == -1) {
7494
QTextStream testStream(&testFile);
7595
testStream << "This is a test file named " << fileName << quazip_endl;

0 commit comments

Comments
 (0)