msalerno has asked for the wisdom of the Perl Monks concerning the following question:

I need to zip a bunch of files, and due to the fact that I have a few extra CPU cycles to spare, I want to use maximum compression. The issue I am running into is that when I specify the maximum compression within the script, it does not seem to be honored. Regardless of what I try, the default desiredCompressionLevel is always used.

According to Archive/Zip.pm:

use constant COMPRESSION_DEFLATED => 8; # file is Deflat +ed use constant COMPRESSION_LEVEL_DEFAULT => -1; use constant COMPRESSION_LEVEL_FASTEST => 1; use constant COMPRESSION_LEVEL_BEST_COMPRESSION => 9;
Here's an example script:
#!/bin/perl -w use strict; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); #$VERSION = '1.30'; die "usage: $0 zipfile.zip file [...]\n" if (scalar(@ARGV) < 2); my $zipName = shift(@ARGV); my $zip = Archive::Zip->new(); foreach my $memberName (map { glob } @ARGV){ my $member = $zip->addFile( $memberName ) or warn "Can't add file $memberName\n"; #$member->desiredCompressionMethod( COMPRESSION_DEFLATED ); $member->desiredCompressionLevel( 9 ); } my $status = $zip->writeToFileNamed($zipName); my $zipread = Archive::Zip->new(); $zipread->read( $zipName ); foreach my $member ($zipread->members()){ print $member->fileName()."\t", $member->compressionMethod()."\t", $member->desiredCompressionMethod()."\n"; } exit $status;
Expected output:
filename.log 8 9

Actual output:
filename.log 8 8

Thanks for the help

Replies are listed 'Best First'.
Re: Archive::Zip not honoring desiredCompressionMethod
by spazm (Monk) on Jul 08, 2009 at 18:36 UTC
    msalerno,

    excellent question.

    You are checking the compressionMethod, which is returning 8. This is good, as Archive::Zip only supports one compression method.

    If we check the compressionLevel, we should get the desired information. When I check it on the file I'm adding, I see default (-1) and then I set it and see what I set (9). How do we check this in the output stream??

    Checking the compressed file size in the output stream shows that the compressionLevel is getting picked up and having an effect.

    a modified version of your code, I now pass in the ziplevel as the first arg:

    #!/bin/perl -w use strict; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); #$VERSION = '1.30'; die "usage: $0 ziplevel zipfile.zip file [...]\n" if (scalar(@ARGV) < 3); my $zipCompressionLevel = shift(@ARGV); my $zipName = shift(@ARGV); my $zip = Archive::Zip->new(); foreach my $memberName (map { glob } @ARGV){ my $member = $zip->addFile( $memberName ) or warn "Can't add file $memberName\n"; $member->desiredCompressionMethod( COMPRESSION_DEFLATED ); my $pre = $member->desiredCompressionLevel(); my $mid = $member->desiredCompressionLevel( $zipCompressionLevel ) +; my $end = $member->desiredCompressionLevel( ); print "Compression level:\n\tbefore: $pre, middle: $mid, after: $e +nd\n"; #print "compressed_size: " . $member->compressedSize() . "\n"; #print "uncompressed_size: " . $member->uncompressedSize() . "\n"; ### these are the same, since the file isn't actually compressed u +ntil writing. } my $status = $zip->writeToFileNamed($zipName); my $zipread = Archive::Zip->new(); $zipread->read( $zipName ); foreach my $member ($zipread->members()){ print $member->fileName()."\t", $member->compressionMethod()."\t", $member->desiredCompressionMethod()."\t", $member->desiredCompressionLevel(). "\n"; print " compressed_size: " . $member->compressedSize() . "\n"; print " uncompressed_size: " . $member->uncompressedSize() . "\n" +; } exit $status
    Output:
    $ perl ziptest.pl 9 zip.zip ziptest.pl Comptession level: before: -1, middle: -1, after: 9 ziptest.pl 8 8 -1 compressed_size: 576 uncompressed_size: 1449 $ perl ziptest.pl 1 zip.zip ziptest.pl Comptession level: before: -1, middle: -1, after: 1 ziptest.pl 8 8 -1 compressed_size: 608 uncompressed_size: 1449 $ perl ziptest.pl 0 zip.zip ziptest.pl Comptession level: before: -1, middle: -1, after: 0 ziptest.pl 8 8 -1 compressed_size: 1449 uncompressed_size: 1449