Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm a Perl (and *nix) newbie.

If I have a GZIP file on the server, how can I extract it to another file (or maybe a variable)? (I believe my host has Linux servers.)

Replies are listed 'Best First'.
Re: Extracting a GZIP File
by cjf (Parson) on May 18, 2002 at 23:34 UTC
Re: Extracting a GZIP File
by Dog and Pony (Priest) on May 18, 2002 at 23:35 UTC
    If this is a perl question, you might wanna look at using the module Compress::Zlib, which is available from CPAN.

    If it isn't a perl question, start by typing gzip -h on the command line of your server. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Extracting a GZIP File
by Kanji (Parson) on May 18, 2002 at 23:42 UTC

    You can try using Compress::Zlib (alt.), but that may require installing a private version if it isn't already available.

    Alternately, you can call the Unix command line utilities gzip -cd or gzcat from Perl using system, exec, backticks or the very groovy Shell module...

    use Shell qw/ gzcat /; my $file = "/path/to/gzipped-file" my $contents = gzcat( $file );

        --k.


Re: Extracting a GZIP File
by giulienk (Curate) on May 18, 2002 at 23:38 UTC
    If you got access to gunzip on the server you could do something like
    my $file = 'foo.bar'; #PLEASE Taint Check this if it comes from outsid +e my $contents = `gunzip -c $file`;
    Otherwise if it's available Compress::Zlib you could take a look at the gzopen function.


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Re: Extracting a GZIP File
by JonWoo (Initiate) on Jul 15, 2005 at 13:32 UTC
    Or you just simply use:
    die "Can't open $inputFilename for reading, exiting ..." unless open(READGZIPPEDFILE, "zcat $inputFilename|" );
    I think it's very efficient like this, taking input zcat pipe.