Search code examples
perlrecursionphp-ziparchive

Recursive descent in zip files?


I am trying to recursively scan a bunch of zip files and I am using, of course, archive::zip. I would like to avoid expanding the archive's content in a temporary folder. I would like to be able to use something like (nearly-pseudo code):

sub CALLMYSELFAGAIN .....

my @members = $currentZipFile->members();
while(my $member = pop @members){                       
    if ($member->isTextFile()){
        push @content, $member->contents();
    }elsif(isZipFile($member->fileName())){
        CALLMYSELFAGAIN($member);
    }

The problem is, $member->can("memberNames")) returns false, so $member is NOT an archive::zip in the sense that I could not open it again as a zip file. Or am I wrong?

Any hint?


Solution

  • You can do this:

    elsif (isZipFile($member->filename)) {
        my $contents = $currentZipFile->contents($member);
        open my $fh, '<', \$contents; # In-memory filehandle
        my $child_zip = Archive::Zip->new;
        $child_zip->readFromFileHandle($fh);
        CALLMYSELFAGAIN($child_zip);
    }
    

    but expect that to be very memory intensive, especially if you go more than one level deep.