Hi,
Can anybody please answer my question? How can i read data from a compressed file without unzipping it? Unlike one of the monks had pointed out, Archive::Zip does not seem to be able to read data from a zip file without having to unzip it first. Please help.
| [reply] |
use IO::Uncompress::Gunzip qw( $GunzipError );
my $file = "somefile";
my $gz = new IO::Uncompress::Gunzip $file
or die "Cannot open $file: $GunzipError\n" ;
while (<$gz>)
{
# whatever...
}
| [reply] [d/l] |
use autodie 2.06;
open my($fh), '<:raw', $file;
while( my $read = read $fh, my($seg),1024 ){
print "i have read $read out of 1024\n";
print "length is ", length $seg,"\n";
}
close $fh;
| [reply] [d/l] |
OK, real world example: I give you a sealed envelope with some pages of text, unreadable through the envelope. Your job is to count the number of vowels on the pages, without opening, damaging or otherwise manipulating the envelope.
This is exactly the same problem. You can not process the contents (pages) of a file (envelope) without opening it. It does not matter what kind of file you have: ZIP, gzip, text, HTML, graphics, drawings, XML, photos, ...
Despite the name, gzip (GNU zip) and ZIP (originating from the old PKZIP utility) are completely different things. A ZIP program (like the original PKZIP, WinZIP, InfoZIP, 7ZIP, ...) compresses individual files and creates an archive of them, whereas gzip only compresses files. Even the compression algorithms are different.
gzip files can be decompressed as a stream, i.e. you read a the first few bytes of a gzip file, stuff them into a newly initialised gzip compressor, and get back a few more bytes of content. You stuff some more bytes from the gzip file into the same gzip compressor, and get back the next chunk of bytes. Repeat until EOF. This way, you need only a little bit of memory or temporary disk space, even when processing huge gzip files. bzip2 (again with a completely different algorithm) shares this ability. ZIP archives don't, because (1) a ZIP archive contains more than just a single file and (2) the table of contents is at the END of the ZIP archive.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
Regarding the compression used by zip and gzip - while it is true that zip can use a multitude of compression formats, the most common out in the wild is deflate, which is exactly what gzip uses.
Also, although zip isn't designed for being streamed, it is still possible to stream from a zip file. So say you have a zip file "my.zip" and you want to stream the member "abc.txt", this will do it
use IO::Uncompress::Unzip qw( $UnzipError);
my $unzip = new IO::Uncompress::Unzip "my.zip",
Name => "abc.txt"
or die "Cannot open my.zip[abc.txt]: $UnzipError\n";
while <($unzip>)
{
...
}
| [reply] [d/l] |