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

There's a way to open a *.gz file using open(), pipes and "gzip -d", but I'm either not remembering/finding it or hallucinating... Anyone recall this shortcut? (for bonus points, I'm sure a quick tutorial on open and pipes in general would also be appreciated by many)

Replies are listed 'Best First'.
Re: Opening zip'd files
by lhoward (Vicar) on Apr 28, 2000 at 02:30 UTC
    Check out the Compress::Zlib perl module. It lets you read and write .gz files directly w/o the need for a pipe (or spawning a separate gzip process).

    $outlog=gzopen("somefile.gz","a"); $outlog->gzwrite("some data\n"); $outlog->gzclose();

    The code for reading is very similar.


    Les Howard
    www.lesandchris.com
    Author of Net::Syslog and Number::Spell

Re: Opening zip'd files
by btrott (Parson) on Apr 27, 2000 at 23:47 UTC
    This seemed to work for me:
    open GZ, "|gzip -dc foo.gz" or die "Can't open pipe: $!"; my $buf; print $buf while read GZ, $buf, 1024; close GZ or die "Can't close pipe: $!";
    You don't have to worry about checking for SIGPIPE cause you only get that when writing to a pipe.
RE: Opening zip'd files
by Anonymous Monk on Apr 28, 2000 at 11:44 UTC
    I think:
    open(Z, "gunzip -c $zfile|");
    you can also use "zcat" (or "gzcat" on some systems) instead of "gunzip -c".