digestBen
digestBen

Reputation: 77

How to zip a file in perl using IO::Compress::Zip

does anyone knows how to use the Level option in IO::Compress::Zip?

I have the problem that I am trying to zip a DB Backup file. But after Zip the file is smaller then the original file and cant be used to importing the file on another server. The file is corupt ore something like this... I am using perl to zip the file like this...

my $zipfile = zip['MYFILE'] => $zipFile, Zip64 => 1, Method => ZIP_CM_STORE
        or die "Zip failed: $ZipError\n";

But no Success. For example orig file size is 13.910.216KB and when Zipped its only 13.909.298KB.

I dont know why but i think i need to set the Level option to Z_NO_COMPRESSION. How to do that?

Thanks

Upvotes: 1

Views: 1172

Answers (2)

pmqs
pmqs

Reputation: 3705

Based on the conversation thread I tried to reproduce the use-case.

First create a file similar in size to yours

$ truncate -s 13910K test
$ ls -lh test
-rw-rw-r-- 1 xxx yyy 14M Apr 13 12:00 test

Add it to zip file using IO::Compress::Zip

$ perl -MIO::Compress::Zip=:all -e 'zip "test" => "test.zip", Method => ZIP_CM_STORE'

Check the sizes & CRC

$ crc32 test
49769d91

$ ls -l test
-rw-rw-r-- 1 xxx yyy 14243840 Apr 13 12:00 test

$ unzip -lv test.zip 
Archive:  test.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
14243840  Stored 14243840   0% 2020-04-13 11:48 49769d91  test
--------          -------  ---                            -------
14243840         14243840   0%                            1 file

All is as expected. The sizes & CRCs match.

Could you try this on your system please?

Upvotes: 0

pmqs
pmqs

Reputation: 3705

As has already been mentioned, by specifying the method ZIP_CM_STORE you are telling IO::Compress::Zip not to compress the file at all.

If you don't specify a Method at all, the code will use ZIP_CM_DEFLATE (which is the standard compression used in practically all zip files)

my $zipfile = zip['MYFILE'] => $zipFile, Zip64 => 1
        or die "Zip failed: $ZipError\n";

If you want to change the compression level, use the Level option. By default it will uses Z_DEFAULT_COMPRESSION for the compression level. If you want the best compression, use Z_BEST_COMPRESSION

my $zipfile = zip['MYFILE'] => $zipFile, Zip64 => 1, Level => Z_BEST_COMPRESSION
        or die "Zip failed: $ZipError\n";

Upvotes: 2

Related Questions