From 376ad9d161293a3c1db0b026d8f7d922343573ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Tabin?= Date: Sun, 30 Apr 2023 20:27:56 +0200 Subject: [PATCH] Adds ZipArchive::getCompressionMethod and fixes ZipEntry::getCompressionMethod --- src/libzippp.cpp | 54 ++++++++++++++++++++++++++---------------------- src/libzippp.h | 1 + tests/tests.cpp | 9 +++++++- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/libzippp.cpp b/src/libzippp.cpp index a44811f..36edb2d 100644 --- a/src/libzippp.cpp +++ b/src/libzippp.cpp @@ -53,49 +53,49 @@ using namespace std; #define NEW_CHAR_ARRAY(nb) new (std::nothrow) char[(nb)]; static libzippp_uint16 convertCompressionToLibzip(CompressionMethod comp) { - switch(comp) { - case CompressionMethod::STORE: - return ZIP_CM_STORE; + switch(comp) { + case CompressionMethod::STORE: + return ZIP_CM_STORE; #ifdef ZIP_CM_BZIP2 - case CompressionMethod::BZIP2: - return ZIP_CM_BZIP2; + case CompressionMethod::BZIP2: + return ZIP_CM_BZIP2; #endif - case CompressionMethod::DEFLATE: - return ZIP_CM_DEFLATE; + case CompressionMethod::DEFLATE: + return ZIP_CM_DEFLATE; #ifdef ZIP_CM_XZ - case CompressionMethod::XZ: - return ZIP_CM_XZ; + case CompressionMethod::XZ: + return ZIP_CM_XZ; #endif #ifdef ZIP_CM_ZSTD - case CompressionMethod::ZSTD: - return ZIP_CM_ZSTD; + case CompressionMethod::ZSTD: + return ZIP_CM_ZSTD; #endif - default: // CompressionMethod::DEFAULT - return ZIP_CM_DEFAULT; - } + default: + return ZIP_CM_DEFAULT; + } } static CompressionMethod convertCompressionFromLibzip(libzippp_uint16 comp) { - switch(comp) { + switch(comp) { case ZIP_CM_STORE: return CompressionMethod::STORE; #ifdef ZIP_CM_BZIP2 case ZIP_CM_BZIP2: return CompressionMethod::BZIP2; #endif - case ZIP_CM_DEFLATE: - return CompressionMethod::DEFLATE; + case ZIP_CM_DEFLATE: + return CompressionMethod::DEFLATE; #ifdef ZIP_CM_XZ - case ZIP_CM_XZ: - return CompressionMethod::XZ; + case ZIP_CM_XZ: + return CompressionMethod::XZ; #endif #ifdef ZIP_CM_ZSTD - case ZIP_CM_ZSTD: - return CompressionMethod::ZSTD; + case ZIP_CM_ZSTD: + return CompressionMethod::ZSTD; #endif - default: // CompressionMethod::DEFAULT - return CompressionMethod::DEFAULT; - } + default: + return CompressionMethod::DEFAULT; + } } namespace Helper { @@ -498,7 +498,7 @@ bool ZipArchive::setEntryCompressionConfig(ZipEntry& entry, CompressionMethod co bool success = zip_set_file_compression(zipHandle, entry.index, comp_libzip, level)==0; if (success) { - entry.compressionMethod = comp; + entry.compressionMethod = comp_libzip; entry.compressionLevel = level; } return success; @@ -884,6 +884,10 @@ void ZipArchive::setCompressionMethod(CompressionMethod comp) compressionMethod = convertCompressionToLibzip(comp); } +CompressionMethod ZipArchive::getCompressionMethod(void) const { + return convertCompressionFromLibzip(compressionMethod); +} + int ZipArchive::readEntry(const ZipEntry& zipEntry, std::ostream& ofOutput, State state, libzippp_uint64 chunksize) const { if (!ofOutput) { return LIBZIPPP_ERROR_INVALID_PARAMETER; } std::function writeFunc = [&ofOutput](const void* data,libzippp_uint64 size){ ofOutput.write((char*)data, size); return bool(ofOutput); }; diff --git a/src/libzippp.h b/src/libzippp.h index 27207fb..b8d7e2e 100644 --- a/src/libzippp.h +++ b/src/libzippp.h @@ -588,6 +588,7 @@ namespace libzippp { * Defines the compression method to used for the newly created ZipEntry. */ void setCompressionMethod(CompressionMethod comp); + CompressionMethod getCompressionMethod(void) const; /** * Defines the compression level to use. By default this value is zero to use the default behaviour of libzip. diff --git a/tests/tests.cpp b/tests/tests.cpp index b398190..e937a12 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -102,11 +102,15 @@ void test2() { int len = strlen(txtFile); ZipArchive z1("test.zip"); + assert(DEFAULT == z1.getCompressionMethod()); + z1.open(ZipArchive::Write); z1.addData("somedata", txtFile, len); ZipEntry z1e1 = z1.getEntry("somedata"); - z1e1.setCompressionMethod(DEFLATE); + bool setcm = z1e1.setCompressionMethod(DEFLATE); + assert(setcm); + assert(DEFLATE == z1e1.getCompressionMethod()); z1.close(); @@ -137,6 +141,9 @@ void test3() { int len = strlen(txtFile); ZipArchive z1("test.zip"); + z1.setCompressionMethod(DEFLATE); + assert(DEFLATE == z1.getCompressionMethod()); + z1.open(ZipArchive::Write); z1.addData("somedata/in/subfolder/data.txt", txtFile, len); assert(z1.addEntry("somedata/"));