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

For reasons that I cannot control, I have to read an overall zipped file which itself contains compressed *.bz2 files (which I normally uncompress with bunzip2). I can get the names of the zipped files with

my $name = $zip->getHeaderInfo()->{Name};

but I cannot work out how, when I know that the zipped file names are *.bz2, to read the files via Uncompress::Bunzip2 Any help gratefully received.

Replies are listed 'Best First'.
Re: reading zipped bzipped files!
by Corion (Patriarch) on Dec 21, 2012 at 18:47 UTC

    If $zip is an Archive::Zip object, you can get at the unzipped content with the ->contents method. You then pass whatever you get from that method to UnCompress::Bzip2.

Re: reading zipped bzipped files!
by johngg (Canon) on Dec 22, 2012 at 00:26 UTC

    I wondered how this might be achieved so decided to have a go. I prepared a ZIP archive containing three bzip2'ed files (man page outputs) stored without further compression.

    The following script constructs an Archive::Zip object to access the ZIP file and gets a list of member files. Then for each member it creates a member object and uses that to obtain the content. A reference to this content, by way of an on-the-fly subroutine, is used as the argument to the IO::Uncompress::Bunzip2 constructor which can then be read line by line. I just print the first five lines of each member file to demonstrate that the method works. I have not incorporated any error checking, this is left as an exercise for the reader.

    use strict; use warnings; use 5.014; use Archive::Zip; use IO::Uncompress::Bunzip2; my $zipFile = q{mans.zip}; my $zip = Archive::Zip->new( $zipFile ); my @members = $zip->memberNames(); foreach my $member ( @members ) { say qq{Member: $member}; my $memberFH = $zip->memberNamed( $member ); my $bzFH = IO::Uncompress::Bunzip2->new( sub { \ $_[ 0 ] }->( $memberFH->contents() ) ); my $lineCt = 0; while ( my $line = $bzFH->getline() ) { last if $lineCt ++ > 5; print $line; } }

    The output.

    Member: cp.man.bz2 CP(1) User Commands + CP(1) NAME cp - copy files and directories Member: ls.man.bz2 LS(1) User Commands + LS(1) NAME ls - list directory contents Member: xterm.man.bz2 XTERM(1) X Window System + XTERM(1) NAME xterm - terminal emulator for X

    I hope this is useful.

    Cheers,

    JohnGG

      Here is a variation on a theme that prints the forst 5 lines of each member in the zip file. The difference with this one is that the complete bzip2 file doesn't need to be read into memory.
      use strict; use warnings; use IO::Uncompress::Unzip qw($UnzipError); use IO::Uncompress::Bunzip2; my $zipFile = q{mans.zip}; my $zip = IO::Uncompress::Unzip->new( $zipFile ) or die "Cannot open $zipFile: $UnzipError"; my $status; for ($status = 1; $status > 0; $status = $zip->nextStream()) { my $name = $zip->getHeaderInfo()->{Name}; warn "Processing member $name\n" ; my $bzFH = IO::Uncompress::Bunzip2->new($zip); my $lineCt = 0; while ( my $line = <$bzFH> ) { last if $lineCt ++ > 5; print $line; } }

        Excellent ++

        I was trying to come up with a way which avoided reading the whole bzipped file but I am not familiar yet with these IO::Compress/Uncompress::* modules as most of my work is on servers running 5.8.x or older and I had only used Archive::Zip before. I will have to study.

        Thank you for showing me this method :-)

        Cheers,

        JohnGG

      Thanks to both of you. I was almost there with my code, but was wrong on the code line

       while ($line = <$zbz2>)

      Of course, my actual code is a bit more complicated because of other little gotchas, but it now works. Have a great Christmas. Mike Gerard

Re: reading zipped bzipped files!
by Anonymous Monk on Dec 21, 2012 at 18:41 UTC
    What is $zip?