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

Yo! Monks!

I am attempting to decompress a .gz file and read the decompressed file into a filehandle for examination. My confusion here may have as much to do with my resident confusion about object syntax as it might with compress::zlib or anything else.

I have tried doing this:
use Compress::Zlib; my $gz = deflateInit() || warn "Could not create zlib deflation stream +: $!\n"; ($out,$status) = $gz->deflate("$system_config{sendmail_log_dir}$_") || + warn "Cannot deflate and open $system_config{sendmail_log_dir}$_: $! +\n"; open (LOGFILE, $out) || warn "Cannot open deflated logfile for reading +: $!\n";
And i have tried this:
use Compress::Zlib; my $gz = deflateInit() || warn "Could not create zlib deflation stream +: $!\n"; open (LOGFILE, $gz->deflate("$system_config{sendmail_log_dir}$_")) || +warn "Cannot open deflated logfile for reading: $!\n";
But neither seems to read the file at all successfully. So, what is my (perl) problem here?
_____________________
mjn

Replies are listed 'Best First'.
Re: Using compress::zlib output as a filehandle
by iburrell (Chaplain) on Mar 21, 2003 at 17:15 UTC
    You want to use the gzopen() function to open a .gz file. Compress::Zlib has two interfaces, one to do the deflate compression and one to read gzip files. You are using the deflate functions and trying to pass deflate() the name of the file. The only problem with the gzip interface is that it uses its own function names instead of creating a standard handle object. PerlIO in Perl 5.8 supposedly has a builtin filter to read gzip files.
    my $gz = gzopen($file); my $line; while ($gz->gzread($line)) { } $gz->gzclose();
Re: Using compress::zlib output as a filehandle
by mjn (Acolyte) on Mar 21, 2003 at 17:41 UTC
    Now I have adjusted my code to reflect those suggestions but I am still getting errors and some look to be out of my control. The documentation for compress::zlib suggests that gzreadline and not gzread is the thing to use...Here's what I have:
    use Compress::Zlib; my $gz = gzopen("$system_config{sendmail_log_dir}$_") || warn "Could n +ot open $system_config{sendmail_log_dir}$_ for reading: $!\n"; my $line; while($gz->gzreadline($line)) { push (@logfile_contents,$line); } $gz->gzclose();
    Here are my errors:
    Use of uninitialized value in subroutine entry at /usr/lib/perl5/site_perl/5.6.1/i386-linux/Compress/Zlib.pm line 118.

    Could not open /home/mjn/perl/logscanner/maillog.2.gz for reading: Bad file descriptor

    Can't call method "gzreadline" without a package or object reference at ./logscanner.pl line 36.

    Input would be great! Thanks.
    _____________________
    mjn
      You need to read the documentation more carefully. The function gzopen takes two arguments.

      Updated: See the zlib manual for details of mode. For reading you want "rb".