Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

finding file in a zip file

by Anonymous Monk
on Jan 31, 2005 at 18:48 UTC ( [id://426689]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

If I want to find a particular file in a zip file, I am doing the following, but it does not seem to detect the file even though the file exists.

my $file = "test.txt"; $zip1 = Archive::Zip->new( $myZipFile ); @files = $zip1->memberNames(); #this does not return any result..even though there are two files in t +he archive called test.txt @files = $zip1->membersMatching( '.*$file' ); print "this is the number of members matching $file : $#files \n\n"; $file->extractToFileNamed($fileToExtract) == AZ_OK or die "Unable to extract data file $! .";

What seems to be the error? Is there another way of finding a file I am looking for in a Zip and extracting it to a temp file for reading? Thanks

Replies are listed 'Best First'.
Re: finding file in a zip file
by Paladin (Vicar) on Jan 31, 2005 at 19:17 UTC

    You have your .*$file in single quotes, which means the variable isn't being interpolated, so it's looking for a file that matches zero or more of anything, followed by the dollar sign, followed by the word file. Try changing those to double quotes, and that part will work fine.

    As for the rest... The extractToFileNamed() method works on the return values of membersMatching, and not on strings. You probably want something like:

    foreach my $archived_file (@files) { $archived_file->extractToFileNamed('some_file_name') or die "Unable to extract data file $! ."; }

    Also, $#files gives you the index of the last element, not the number of elements in the array. For that you want my $numfiles = @files

Re: finding file in a zip file
by samizdat (Vicar) on Jan 31, 2005 at 19:12 UTC
    Is it possible -- since you mention _two_ test.txt files -- that you need to generate, or strip, the directory path?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://426689]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found