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

Currently have a subroutine that works that gathers filesize information of zip files. I'm looking to turn this into a more pure perl solution for speed purproses (I need to do this over millions of files).

I looked in Archive::Zip and Archive::Unzip and wasn't really finding anything. For tarballs I was able to do this with Archive::Tar->list_archive for tarballs.

Current working code

sub CollectZip { my ($location) = shift; my @zipListData = `unzip -l \'$location\'`; chomp @zipListData; ### Get rid of the first 3 rows and last 2 rows shift(@zipListData); shift(@zipListData); shift(@zipListData); pop(@zipListData); pop(@zipListData); my %filesData; foreach my $zipData (@zipListData) { $zipData =~ tr/ //s; my @zipData = split(/ /, $zipData); my ($zipFilesize) = $zipData[1]; my ($zipFilename) = $zipData[4]; $filesData{$zipFilename} = $zipFilesize; } print Dumper \%filesData; return %filesData; }

Replies are listed 'Best First'.
Re: Unzip -l replacement
by marinersk (Priest) on Mar 10, 2015 at 19:49 UTC

    I'd use uncompressedSize:

    #!/usr/bin/perl use strict; use Archive::Zip; foreach my $zipfnm (@ARGV) { if (-e $zipfnm) { # ZIP file exists. Proceed. my $zipobj = Archive::Zip->new($zipfnm); # ZIP obje +ct pointer my @mbrhan = $zipobj->members(); foreach my $mbrhan (@mbrhan) { # Next member found in ZIP file my $mbrfnm = $mbrhan->fileName(); my $mbrfsz = $mbrhan->uncompressedSize(); print "$mbrfnm is $mbrfsz bytes\n"; } } } exit; __END__

    D:\PerlMonks>7z l -tzip test.zip 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 Listing archive: test.zip -- Path = test.zip Type = zip Physical Size = 330 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ----------------- +------- 2015-03-10 15:40:58 ....A 22 22 test.dat 2015-03-10 15:41:09 ....A 28 28 test2.dat ------------------- ----- ------------ ------------ ----------------- +------- 50 50 2 files, 0 folder +s D:\PerlMonks>zipgetsize.pl test.zip test.dat is 22 bytes test2.dat is 28 bytes D:\PerlMonks>

      This worked wonderfully. Thank you so much.

        You are more than welcome. Very glad it got you what you needed.