[Israel.pm] Unzip on the fly (with LWP?)
Gaal Yahas
gaal at forum2.org
Tue Jun 8 07:53:26 PDT 2004
On Tue, Jun 08, 2004 at 05:16:57PM +0300, Roman M. Parparov wrote:
> Good afternoon,
>
> What would be the most elegant way to retrieve a zip file from
> a WWW url and unzip it on the fly.
>
> 1) The best result would be getting the unzipped data WITHOUT
> writing any file, let' say, into an array of strings separated by
> newline.
Surprisingly, this is (a bit) obscure.
Lifting code from the section "Low-level member data reading" in the
Archive::Zip doc, I get this. Untested:
use LWP::Simple();
use Archive::Zip;
my $url = XXX;
my $name = XXX; # name of zip file
my $member_name = XXX; # name of file inside zip
# Get the data from the network.
my $web_content = LWP::Simple::get($url) or die "HTTP GET: $!";
# Open a zip object.
my $fh = IO::String->new($web_content);
my $zip = Archive::Zip->new();
my $status = $zip->readFromFileHandle($fh, $name);
# Read from the zip.
my $extracted_content;
my ($member, $status, $bufferRef);
$member = $zip->memberNamed($member_name);
$member->desiredCompressionMethod(COMPRESSION_STORED);
$status = $member->rewindData();
die "error $status" unless $status == AZ_OK;
while (! $member->readIsDone()) {
($bufferRef, $status) = $member->readChunk();
die "error $status"
if $status != AZ_OK && $status != AZ_STREAM_END;
$extracted_content .= $$bufferRef; # [*]
}
$member->endRead();
# $extracted_content now holds your complete data. You may of course
# process it as a stream instead by altering the line above marked
# with an asterix [*].
--
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
More information about the Perl
mailing list