Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Question on IO::UnCompress::GunZip...

by Khen1950fx (Canon)
on Jan 18, 2010 at 07:07 UTC ( [id://817937]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Question on IO::UnCompress::GunZip...
in thread Question on IO::UnCompress::GunZip...

Thank you, rementis.
  • Comment on Re^3: Question on IO::UnCompress::GunZip...

Replies are listed 'Best First'.
Re^4: Question on IO::UnCompress::GunZip...
by Anonymous Monk on Jan 22, 2010 at 09:36 UTC
    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.

      Is this a rewording of your question - how do a read from a compressed file without having to uncompress it completely first, then read through the temporary uncompressed file?

      If that is what you are asking then try this (assuming the compressed file contains text)

      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... }
      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;

      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". ;-)
        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>) { ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://817937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found