Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Process Zip

by bitman (Scribe)
on Dec 08, 2010 at 10:46 UTC ( #875987=perlquestion: print w/replies, xml ) Need Help??
bitman has asked for the wisdom of the Perl Monks concerning the following question:

Any idea if you can walk thru a zip processing each file? The examples with Archive::Zip I've seen always specify a filename. I just want to save unzipping the whole 40000+ files into a directory then deleting them.

Replies are listed 'Best First'.
Re: Process Zip
by marto (Archbishop) on Dec 08, 2010 at 11:01 UTC

    Your question isn't clear to me. Are you asking if there is a way to access a file within a zip archive without uncompressing the entire archive? Archive::Zip::MemberRead may be what you are looking for if that's the case.

      I want to process ALL files, one at a time, I don't know the file names.
        my $zip = new Archive::Zip; $zip->read(''); # ..obviously you have this already. my @memberNames = $zip->memberNames; # @memberNames is now a list of the files in the zip Archive.

        Now you iterate over that array like any other array.
        Is that what you wanted?

        After this, of course, you can extract the files by name (using the ->extractMember method) and delete the temporary file when you're done with it.

        Archive::Extract may prove intersting, though you will have to extract all of the files, process and delete them.

        An alternative would be to investigate somehow extracting them to memory rather than to file. You'd need to do some research on this.

Re: Process Zip
by pmqs (Pilgrim) on Dec 09, 2010 at 13:52 UTC
    This will walk a zip file, on member at a time, without saving anything to disk at all.
    use IO::Uncompress::Unzip qw($UnzipError); my $zipfile = ""; my $u = new IO::Uncompress::Unzip $zipfile or die "Cannot open $zipfile: $UnzipError"; my $status; for ($status = 1; ! $u->eof(); $status = $u->nextStream()) { my $name = $u->getHeaderInfo()->{Name}; warn "Processing member $name\n" ; my $buff; while (($status = $u->read($buff)) > 0) { # Do something here } last if $status < 0; } die "Error processing $zipfile: $!\n" if $status < 0 ;
Re: Process Zip
by SimonClinch (Deacon) on Dec 08, 2010 at 21:30 UTC
    Archive::Extract has it

    One world, one people

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875987]
Approved by marto
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-04-25 23:58 GMT
Find Nodes?
    Voting Booth?