Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

unpacking .gz from perl

by karpatov (Beadle)
on Feb 16, 2008 at 19:12 UTC ( #668333=perlquestion: print w/replies, xml ) Need Help??
karpatov has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I would like to now if there is a simple way how to unpack a gz file from within the perl script, operate on unpacked file and then to delete (and continue with another gz....).
Tx for replies karpatov

Replies are listed 'Best First'.
Re: unpacking .gz from perl
by ferreira (Chaplain) on Feb 16, 2008 at 20:01 UTC

    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.

      did this code work for u.When i tried its showing an error like "It looks like you don't have a C compiler on your PATH, so you will not be able to compile C or XS extension modules. You can install GCC from the MinGW package using the Perl Package Manager by running: ppm install MinGW"

      I tried installing using the above command.But installation is getting failed

        When i tried its showing an error like

        It is not only an error message, it is instructions to overcome the error!!!!!!

Re: unpacking .gz from perl
by Joost (Canon) on Feb 16, 2008 at 19:18 UTC
Re: unpacking .gz from perl
by johngg (Abbot) on Feb 16, 2008 at 19:18 UTC
    I've only used it on .zip files, not .gz files, but have a look at Compress::Zlib as it seems from the documentation that it could be what you need.



    Update: I'm not sure what I was on when I wrote that nonsense about .zip files. I have, of course, used Compress::Zlib on .gz files, it was Archive::Zip that I used on .zip files.

Re: unpacking .gz from perl
by graff (Chancellor) on Feb 16, 2008 at 19:55 UTC
    You can open the file like this, if you have PerlIO::gzip installed:
    use strict; use PerlIO::gzip; ( @ARGV == 1 and -f $ARGV[0] and $ARGV[0] =~ /\.gz$/ ) or die "Usage: $0 file.gz\n"; open( IN, "<:gzip", $ARGV[0] ) or die "open failed on $ARGV[0]: $!\n"; while (<IN>) { # $_ contains one line of uncompressed text data per iteration do_something($_); }

    UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://668333]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-03-20 16:32 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (254 votes). Check out past polls.