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

I have some zip files that contain .gz files in them and each one is an xml file, is there a way to extract the xml's, here's what I got so far: I'm able to print the gzip names inside the zip, but not sure how to go about extracting them. Thanks!

#!/home/cnda/master/bin/perl/bin/perl use strict; use warnings; use IO::Compress::Zip; use File::Spec::Functions qw(splitpath); use IO::File; use IO::Uncompress::Unzip qw($UnzipError); use File::Path qw(mkpath); $path = $ARGV[0]; @list = `find $path -name '*zip' -type f`; foreach(@list){ my $zipFile = IO::Uncompress::Unzip->new($_); while (my $status = $zipFile->nextStream()) { my $zipEntryHeader = $zipFile->getHeaderInfo(); my $pathAndFilename = $zipEntryHeader->{Name}; print "$pathAndFilename\n"; }

Replies are listed 'Best First'.
Re: .gz inside a zip
by huck (Prior) on Sep 11, 2017 at 02:10 UTC

    You were missing a my and a right bracket too

    #!/home/cnda/master/bin/perl/bin/perl use strict; use warnings; use IO::Compress::Zip; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; my @list=('E:/synctest/y2017.zip'); foreach my $path (@list){ my $zip = Archive::Zip->new(); unless ( $zip->read( $path ) == AZ_OK ) { die 'read error';} my @membersfn=$zip->memberNames(); for my $pathAndFilename (@membersfn) { print "$pathAndFilename\n"; my $member = $zip->memberNamed( $pathAndFilename ); if ($member) { $member->desiredCompressionMethod( COMPRESSION_STORED ); my $status = $member->rewindData(); die "error $status" unless $status == AZ_OK; my $membdata=''; while (!$member->readIsDone()){ my ( $bufferRef, $status ) = $member->readChunk(4*1024); die "error $status" if $status != AZ_OK && $status != AZ_STREAM_END; $membdata.=$$bufferRef; } # ! done my $flatdata; gunzip \$membdata=>\$flatdata; unless (defined $flatdata) {die "IO::Uncompress::Gunzip+un +zip failed: $GunzipError\n"; } print $flatdata."\n"; } # member }# pathAndFilename } # path

Re: .gz inside a zip
by karlgoethebier (Abbot) on Sep 11, 2017 at 09:31 UTC
    "I have some zip files..."

    You can avoid it to shell out:

    #!/usr/bin/env perl # $Id: 1199058.pl,v 1.2 2017/09/11 09:10:43 karl Exp karl $ # http://perlmonks.org/?node_id=1199058 use strict; use warnings; use feature qw(say); use Path::Iterator::Rule; my $rule = Path::Iterator::Rule->new->file->name("*.zip"); my $path = q(.); my $next = $rule->iter($path); while ( defined( my $archive = $next->() ) ) { say $archive; # more stuff.. } __END__

    See also Path::Tiny, Path::Iterator::Rule and IO::All.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help