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.
$ man ls > ls.man $ man xterm > xterm.man $ man cp > cp.man $ bzip2 -v *.man cp.man: 2.496:1, 3.206 bits/byte, 59.93% saved, 5690 in, 2280 o +ut. ls.man: 2.644:1, 3.026 bits/byte, 62.18% saved, 8093 in, 3061 o +ut. xterm.man: 5.062:1, 1.580 bits/byte, 80.24% saved, 264279 in, 5221 +0 out. $ zip -0m mans *.man.bz2 adding: cp.man.bz2 (stored 0%) adding: ls.man.bz2 (stored 0%) adding: xterm.man.bz2 (stored 0%) $
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
In reply to Re: reading zipped bzipped files!
by johngg
in thread reading zipped bzipped files!
by mike_gerard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |