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

I've been stuck for awhile trying to uncompress .Z text files on the fly. I have a series of files to get off the company's intranet some are straight text files and some have been compressed. The plain text files are simple using LWP and just loading the content into a variable to then playing with it. I've not been able to get the compressed files to uncompress nicely. I've been trying to use the Compress::Zlib module. A snippet of the appropriate code follows:
use Compress::Zlib; use LWP::UserAgent; use HTTP::Request; use strict; ....etc. my $ua = new LWP::UserAgent; $ua->proxy('http', 'http://some.proxy.com:8000/'); my $file = "somefile.Z"; my $page="http://somehost.com/$file"; my $request = new HTTP::Request("GET", $page); my $response = $ua->request($request); if($response->is_success && $response->header('Content-Encoding') = +~ /compress/){ $stuff = Compress::Zlib::uncompress($response->content_ref); } play with data...
Basically $stuff is undef. If I use the inflate process provided in Compress::Zlib I get a status of Z_DATA_ERROR (-3). I don't see why the returned content shouldn't just decompress. I've checked the files and they are properly x-compressed. What am I missing? Does the web client not return the compressed file properly? Help!

Replies are listed 'Best First'.
Re: Help with Uncompressing Data
by epoptai (Curate) on Apr 07, 2001 at 00:33 UTC
    For future reference, when working with files Compress::Zlib can handle,
    this is the syntax you want:
    $stuff = $response->content; $stuff = uncompress($stuff);
Re: Help with Uncompressing Data
by extremely (Priest) on Apr 07, 2001 at 00:16 UTC
    Now it may just be me but from what I can tell the Zlib module works with gzip files, not old Unix Compress.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Help with Uncompressing Data
by kschwab (Vicar) on Apr 07, 2001 at 00:21 UTC
    Perhaps you've hit a bug:

    1.08 - 6 Jan 2000 * uncompress was clobbering its input parameter. Now it doesn't. This bug was spotted by Deven T. Corzine.

    Perhaps clobbering the input ref causes the error ? I guess you could try either passing a regular scalar, or upgrading to version 1.11 of Compress::Zlib, if you haven't already.

Re: Help with Uncompressing Data
by Anonymous Monk on Apr 07, 2001 at 00:16 UTC
    Does the web client not return the compressed file properly?

    If you wrote the data to a file and then used the uncompress program from the command line, you could determine if that is the problem. It's always a good idea to check that your data is good before attempting to debug your code.
      Sorry, Yes the response content does return the correct data. I did write it to a file and it will uncompress nicely wiht either the UNIX uncompress or gunzip commands. Also, my version of Zlib is 1.11 and the c library for zlib is 1.1.3. Replacing the reference with the content doesn't make any difference. I've also tried the memGunzip function from Compress::Zlib and I get the same error message. Thanks for the checking ideas.