For most uses, you'll probably be satisfied with the solutions above, where you decompress the .gz file into memory and "operates" on the uncompressed content as you wish.

But if you need to deal with really big files or if the operations you'll need to apply prefer the content in a file (eg. when you need to pass it to external programs), you can do it with a combination of Archive::Extract and File::Temp. The following code is such an example:

use File::Temp (); use Archive::Extract (); for my $gz ( @ARGV ) { my $tmp = File::Temp->new(); my $ae = Archive::Extract->new( archive => $gz ); my $ok = $ae->extract( to => $tmp->filename ); die $ae->error unless $ok; # do what you want to the uncompressed content in the # file named $tmp->filename ... # at the end of the scope, $tmp vanishes }

This code has the bonus of working with other compressed types, like .bz2, .Z and even .lzma with the latest release of Archive::Extract. Even more, because this module may also extract archives with multiple files like .tar.gz, .zip, etc.


In reply to Re: unpacking .gz from perl by ferreira
in thread unpacking .gz from perl by karpatov

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.