Skip to content

Commit

Permalink
Feature/strategy options (#206)
Browse files Browse the repository at this point in the history
* Adds compression strategy (level, method) to JlCompress
---------

Co-authored-by: ran <1353599815@qq.com>
  • Loading branch information
cen1 and lazyone233 authored Oct 5, 2024
1 parent 89bef11 commit aab2f0b
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 115 deletions.
10 changes: 5 additions & 5 deletions quazip/JlCompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ bool JlCompress::compressFile(QuaZip* zip, QString fileName, QString fileDest, c

QuaZipFile outFile(zip);
if (options.getDateTime().isNull()) {
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName))) return false;
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName), nullptr, 0, options.getCompressionMethod(), options.getCompressionLevel())) return false;
}
else {
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName, options.getDateTime()))) return false;
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, fileName, options.getDateTime()), nullptr, 0, options.getCompressionMethod(), options.getCompressionLevel())) return false;
}

QFileInfo input(fileName);
Expand Down Expand Up @@ -106,13 +106,13 @@ bool JlCompress::compressSubDir(QuaZip* zip, QString dir, QString origDir, bool
QuaZipFile dirZipFile(zip);
std::unique_ptr<QuaZipNewInfo> qzni;
if (options.getDateTime().isNull()) {
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir);
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir);
}
else {
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir, options.getDateTime());
qzni = std::make_unique<QuaZipNewInfo>(origDirectory.relativeFilePath(dir) + QLatin1String("/"), dir, options.getDateTime());
}
if (!dirZipFile.open(QIODevice::WriteOnly, *qzni, nullptr, 0, 0)) {
return false;
return false;
}
dirZipFile.close();
}
Expand Down
74 changes: 62 additions & 12 deletions quazip/JlCompress.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,71 @@ class QUAZIP_EXPORT JlCompress {
public:
class Options {
public:
explicit Options(const QDateTime& dateTime = QDateTime())
: m_dateTime(dateTime) {}
/**
* The enum values refer to the comments in the open function of the quazipfile.h file.
*
* The value is represented by two hexadecimal characters,
* the left character indicating the compression method,
* and the right character indicating the compression level.
*
* method == 0 indicates that the file is not compressed but rather stored as is.
* method == 8(Z_DEFLATED) indicates that zlib compression is used.
*
* A higher value of level indicates a smaller size of the compressed file,
* although it also implies more time consumed during the compression process.
*/
enum CompressionStrategy
{
/// Storage without compression
Storage = 0x00, // Z_NO_COMPRESSION 0
/// The fastest compression speed
Fastest = 0x81, // Z_BEST_SPEED 1
/// Relatively fast compression speed
Faster = 0x83,
/// Standard compression speed and ratio
Standard = 0x86,
/// Better compression ratio
Better = 0x87,
/// The best compression ratio
Best = 0x89, // Z_BEST_COMPRESSION 9
/// The default compression strategy, according to the open function of quazipfile.h,
/// the value of method is Z_DEFLATED, and the value of level is Z_DEFAULT_COMPRESSION -1 (equals lvl 6)
Default = 0xff
};

QDateTime getDateTime() const {
return m_dateTime;
}
public:
explicit Options(const QDateTime& dateTime = QDateTime(), const CompressionStrategy& strategy = Default)
: m_dateTime(dateTime), m_compressionStrategy(strategy) {}

QDateTime getDateTime() const {
return m_dateTime;
}

void setDateTime(const QDateTime &dateTime) {
m_dateTime = dateTime;
}

CompressionStrategy getCompressionStrategy() const {
return m_compressionStrategy;
}

int getCompressionMethod() const {
return m_compressionStrategy != Default ? m_compressionStrategy >> 4 : Z_DEFLATED;
}

int getCompressionLevel() const {
return m_compressionStrategy != Default ? m_compressionStrategy & 0x0f : Z_DEFAULT_COMPRESSION;
}

void setDateTime(const QDateTime &dateTime) {
m_dateTime = dateTime;
}
void setCompressionStrategy(const CompressionStrategy &strategy) {
m_compressionStrategy = strategy;
}

private:
// If set, used as last modified on file inside the archive.
// If compressing a directory, used for all files.
QDateTime m_dateTime;
// If set, used as last modified on file inside the archive.
// If compressing a directory, used for all files.
QDateTime m_dateTime;
CompressionStrategy m_compressionStrategy;
};

static bool copyData(QIODevice &inFile, QIODevice &outFile);
Expand Down Expand Up @@ -287,7 +337,7 @@ class QUAZIP_EXPORT JlCompress {
list of the entries, including both files and directories if they
are present separately.
*/
static QStringList getFileList(QIODevice *ioDevice);
static QStringList getFileList(QIODevice *ioDevice);
};

#endif /* JLCOMPRESSFOLDER_H_ */
20 changes: 20 additions & 0 deletions qztest/qztest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,40 @@ bool createTestFiles(const QStringList &fileNames, int size, const QString &dir)
testDir.path().toUtf8().constData());
return false;
}
//qDebug() << "Created path " << testDir.path();
QFile dirFile(testDir.path());
if (!dirFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner |
QFileDevice::ReadGroup | QFileDevice::ExeGroup |
QFileDevice::ReadOther | QFileDevice::ExeOther)) {
qWarning("Couldn't set permissions for %s",
testDir.path().toUtf8().constData());
return false;
}
}
if (fileName.endsWith('/')) {
if (!curDir.mkpath(filePath)) {
qWarning("Couldn't mkpath %s",
fileName.toUtf8().constData());
return false;
}
//qDebug() << "Created path " << filePath;
QFile dirFile(filePath);
if (!dirFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner |
QFileDevice::ReadGroup | QFileDevice::ExeGroup |
QFileDevice::ReadOther | QFileDevice::ExeOther)) {
qWarning("Couldn't set permissions for %s",
filePath.toUtf8().constData());
return false;
}
} else {
QFile testFile(filePath);
if (!testFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning("Couldn't create %s",
fileName.toUtf8().constData());
return false;
}
testFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner |
QFileDevice::ReadGroup | QFileDevice::ReadOther);
if (size == -1) {
QTextStream testStream(&testFile);
testStream << "This is a test file named " << fileName << quazip_endl;
Expand Down
Loading

0 comments on commit aab2f0b

Please sign in to comment.