Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Archive::Zip changing compression on the fly

by cormanaz (Chaplain)
on May 22, 2010 at 15:13 UTC ( #841181=perlquestion: print w/replies, xml ) Need Help??
cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Howdy bros. I'm trying to set up some code using Archive::Zip to zip up a directory. I'm a little confused from the docs about how the compression level works. The directory in question has some zip files already in it, and I don't want to take the time to compress those since it won't do any good, but everything else I want to compress. So I've got this so far:
use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use File::Find qw(find); my $startdir = 'foo'; our $zip = Archive::Zip->new(); my $dir_member = $zip->addDirectory( '04campaigndashboard/' ); $dir_member->desiredCompressionLevel( 9 ); find(\&wanted,$startdir); sub wanted { ($fn = $File::Find::name) =~ s/$startdir//; if ($fn =~ /.zip/) { ???? } else { my $file_member = $zip->addFile( $fn ); } return; }
What I'm confused about is what goes in the zip case. The docs say you can change the compression for any element with the desiredCompressionLevel method, but you have to know the member name first. But isn't the file member compressed when you add it? If so this would not help me avoid the cycles to compress it only to uncompress it with the desiredCompressionLevel method, in fact it would make things worse.

Or is the compression done when the archive is written to disk?

UPDATE: I was just doing some testing and found tat the addFile method seems to not work when the argument is a zip file--it returns undef. So maybe you can't nest zip files with this mod at all>


Replies are listed 'Best First'.
Re: Archive::Zip changing compression on the fly
by zwon (Abbot) on May 22, 2010 at 15:42 UTC

    It looks like

    should do what you want.

    PS Also, in wanted you aren't checking if you have found file or directory, and that may be the reason why addFile returns undef. Add something like

    return unless -f $File::Find::name;
    in the beginning of wanted
      Doh! Why didn't I think of that?

      Unfortunately even that construction runs in the the problem that the mod seemingly won't add a zip file with addFile. Seems for that you have to use $zip->addMember.

        There's also a problem with File::Find. Inside wanted it changes directory to the directory where the file is, so you should pass to addFile either absolute file name, or basename, but you're passing relative. It seems though, that Archive::Zip actually opens file later, so basenames will not work either. I recommend you not to invoke addFile from the wanted, but only build list of found files, and add them to the zip later.

        PS Like this:

        use Path::Class qw(file); my @files; find( \&wanted, $startdir ); sub wanted { my $file = file($File::Find::name); return unless -f $file->basename; push @files, $File::Find::name; } for (@files) { if (/\.zip/) { $zip->addFile($_)->desiredCompressionLevel(0); } else { $zip->addFile($_); } } $zip->writeToFileNamed('');

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://841181]
Approved by wfsp
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2017-06-25 19:19 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (569 votes). Check out past polls.