Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Process Zip

by bitman (Beadle)
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.

Comment on Process Zip
Re: Process Zip
by marto (Bishop) 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.

        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.

        my $zip = new Archive::Zip; $zip->read('Filename.zip'); # ..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.

Re: Process Zip
by SimonClinch (Chaplain) on Dec 08, 2010 at 21:30 UTC
    Archive::Extract has it

    One world, one people

Re: Process Zip
by pmqs (Monk) 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 = "somefile.zip"; 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 ;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875987]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-25 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls