Skip to content

Commit

Permalink
Merge pull request #195 from ctabin/fix-compression-method
Browse files Browse the repository at this point in the history
Adds ZipArchive::getCompressionMethod and fixes ZipEntry::getCompressionMethod
  • Loading branch information
ctabin authored Apr 30, 2023
2 parents 8b7a1fc + 376ad9d commit a576d82
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
54 changes: 29 additions & 25 deletions src/libzippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<bool(const void*,libzippp_uint64)> writeFunc = [&ofOutput](const void* data,libzippp_uint64 size){ ofOutput.write((char*)data, size); return bool(ofOutput); };
Expand Down
1 change: 1 addition & 0 deletions src/libzippp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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/"));
Expand Down

0 comments on commit a576d82

Please sign in to comment.