http://qs321.pair.com?node_id=95367

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

I am writing a program that will connect to a FTP server using LWP and d/l a zip compressed file. I am able to fetch the file, save it to disk, uncompress the file using Archive::Zip, and parse the resulting data.

The question I put before you is: How can I skip the save file to disk step and unzip the file directly from the UA's output? I understand that I can get a reference to $ua->content, but I haven't figured out how to pass that to a Archive::Zip object, which appears to only work on files and FHs.

This is the code I have right now, assume all objects were properly started before this snippit:

foreach my $file ( @filelist ) { my $FILE_URL = qq ( $URL/$file ); my $request = HTTP::Request->new(GET => $FILE_URL ); my $res = $ua->request($request, $file); print $res->status_line, "\n"; my $zip = Archive::Zip->new(); $zip->read ($file); my $member = $zip->memberNamed( 'data.dat' ); my ( $string, $status ) = $member->contents(); die "error $status" if $status != AZ_OK; print $string; }
This works, but saves the file to disk on line 4. I want to be able to take $ua->contents and use it for the zip file on line 8, $zip->read ($file).

Any help would be nice.

--SparkeyG
japh

Replies are listed 'Best First'.
Re: LWP::UserAgent interaction w/ Archive::Zip
by traveler (Parson) on Jul 10, 2001 at 21:36 UTC
Re: LWP::UserAgent interaction w/ Archive::Zip
by SparkeyG (Curate) on Jul 10, 2001 at 22:43 UTC
    Okay, the IO::Scalar pointer has helped, now I am getting a unblessed reference error when I attempt to pass it to  $zip->read()
    Here is a revised code and the error:

    my $SH = IO::Scalar->new(); $SH->open (\$res->content()); # my $SH = tie *ZIP, 'IO::Scalar', \$res->content(); my $zip = Archive::Zip->new(); die ("Error reading Zip File : $!\n") if $zip->read ( \$SH ) != A +Z_OK; # die ("Error reading Zip File : $!\n") if $zip->read ( \*ZIP ) != + AZ_OK;

    The error, w/ use diagnostics:
    Can't call method "isa" on unblessed reference at /opt/perl/lib/site_perl/5.6.1/Archive/Zip.pm line 624 (#1)

    And if anyone cares, the offending code from Archive::Zip:
    if ( ref( $fd ) ) { if ( $fd->isa( 'IO::Handle' ) or $fd->isa( 'GLOB' ) ) { $status = $handle->fdopen( $fd, @_ ); } else { $handle = $fd; } }

    --SparkeyG
    A humble monk over his head.

      What you do is what I've done in the 0.12 version of Archive::Zip that I'm going to upload to CPAN Real Soon Now.

      You can fix _newFileHandle, but you'll run into problems (I think) when it goes to look at the file name. I've made a number of changes since 0.11; some of them have to do with reading from IO::Scalars. If you want to try out the latest version, you can try an almost-finished one at ftp://ftp.bike-nomad.com/public/Archive-Zip-0.12.tar.gz. I'd be interested to get your feedback.