2013-05-17 54 views
2

我需要在包含多個文本文件的zip壓縮文件中搜索字符串。我如何使用perl Archive :: Zip來做到這一點?另外,如果有的話,還有什麼其他模塊?如何使用perl搜索壓縮文件中的文本

我的基本要求是在包含給定字符串的壓縮檔案中找到特定的文本文件。

+0

聽起來像是你在正確的軌道上。你有什麼問題? – squiguy

+0

Archive :: Zip的哪些功能正是它的作用?與CPAN doc我無法指出確切的必需功能。 – pseudocode

+1

也許這很有用:http://cpansearch.perl.org/src/ADAMK/Archive-Zip-1.30/examples/zipGrep.pl。或者這個:http://stackoverflow.com/questions/1249798/how-can-i-grep-for-a-text-pattern-in-a-zipped-text-file – FMc

回答

2

只是一個簡單的例子,如何將所有的文件遍歷在一個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"; 
    }   
} 
相關問題