David M. Karr
David M. Karr

Reputation: 15215

how to use perl Archive::Zip to recursively walk archive files?

I have a small perl script that I use to search archives for members matching a name. I'd like to enhance this so that if it finds any members in the archive that are also archives (zip, jar, etc) it will then recursively scan those, looking for the original desired pattern.

I've looked through the "Archive::Zip" documentation, and I thought I saw how to do this. I noticed the "fh()" and "readFromFileHandle()" methods. However, in my testing, it appears that the "fh()" call on an archive member returns the file handle for the containing archive, not the member. Perhaps I'm doing it wrong, but I would appreciate an example of how to do this.

Upvotes: 3

Views: 1678

Answers (1)

Borodin
Borodin

Reputation: 126722

You can't read the contents of any sort of archive member (whether it is text, picture, or another archive) without extracting it from the archive file.

Once you have identified a member that you want to view, you must call extractMember (or, more likely, extractMemberWithoutPaths if the file is to be temporary) to extract it to a disk file. Then you can create a new Archive::Zip object and read the new file while keeping the old one open.

You will presumably want to unlink the archive file once you have catalogued its contents.


Edit

I hadn't come across the Archive::Zip::MemberRead module before. It appears you were on the right track with readFromFileHandle. I would guess that it should work like this, but it would be awkward for me to test it at present.

my $zip = Archive::Zip->new;
$zip->read('myfile.zip');

my $zipfh  = Archive::Zip::MemberRead->new($zip, 'archive/path/to/member.zip');

my $newzip = Archive::Zip->new;
$newzip->readFromFileHandle($zipfh)

Upvotes: 1

Related Questions