2

I need to search for a string in a zip archive containing multiple text files. How can I use perl Archive::Zip to do that? Also, what other module can it, if any?

My basic requirement is to find a particular text file in zipped archive which contains a given string.

pseudocode
  • 341
  • 1
  • 6
  • 15
  • Sounds like you are on the right track. What problems are you having? – squiguy May 17 '13 at 00:56
  • which function of Archive::Zip exactly does it? with CPAN doc I am not able to pin point the exact required function. – pseudocode May 17 '13 at 01:11
  • 1
    Maybe this is useful: http://cpansearch.perl.org/src/ADAMK/Archive-Zip-1.30/examples/zipGrep.pl. Or this: http://stackoverflow.com/questions/1249798/how-can-i-grep-for-a-text-pattern-in-a-zipped-text-file – FMc May 17 '13 at 02:27

1 Answers1

2

Just a simple example how to iterate over all files in a zip,

use strict;
use Archive::Zip;
my ($path, $filename) = ('c:\\temp\\', 'many_filez.zip'); #try / instead of \\?
my $zip = Archive::Zip->new($path.$filename);
unless ($zip) {
    die dump "cannot open '$path' + '$filename'" 
}
#find only css-files
my @members = $zip->membersMatching('.*\.css');
foreach my $member (@members) {
    my $file_content = $zip->contents($member );
    if ($file_content =~ /text to look for/ ) {
        print "\n $member->{fileName} did have a match";
    }        
}
FtLie
  • 773
  • 4
  • 14