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

Hi Monks!

I am trying to extract the file only from a zip file.
This zip file once unzipped has directories, e.g. info/home/houses.txt
What I need is to only grab the "houses.txt" file from the zip file.
The sample code I am posting unzips the file alright. but I can not get the file only "houses.txt" from the zip file.
Any suggestions?

#!/usr/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use Data::Dumper; my $zipFile = "myzip.zip"; my $archiveDir = '/fileDirectory' ; my $zip = Archive::Zip->new(); my $status = $zip->read("$archiveDir/$zipFile"); my @members = $zip->memberNames(); die "Read of $zip failed\n" if $status != AZ_OK; foreach my $member (@members) { $zip->extractMember($member, "/zips/$member"); }

Thanks for looking at it!

Replies are listed 'Best First'.
Re: Unzipping and extracting file only with Archive::Zip
by Mr. Muskrat (Canon) on Nov 28, 2017 at 19:18 UTC

    The code you posted should extract all members from the archive. Can you show us the code where you attempted to extract only houses.txt so that we can help you fix it?

      I tried using this from the module thinking that it would remove the path where the file is and extract the file only, but no luck.
      $zip->extractMemberWithoutPaths($member, "/zips/$member");
        use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use Data::Dumper; my $zipFile = "myzip.zip"; my $archiveDir = '/fileDirectory'; my $zip = Archive::Zip->new(); my $status = $zip->read("$archiveDir/$zipFile"); die "Read of $zip failed\n" if $status != AZ_OK; $zip->extractMemberWithoutPaths('info/home/houses.txt', '/zips/houses. +txt'); #$zip->extractMember('info/home/houses.txt', '/zips/houses.txt'); # ei +ther works for me