[Israel.pm] Unzip on the fly (with LWP?)

Roman M. Parparov romm at empire.tau.ac.il
Wed Jun 9 03:32:30 PDT 2004


On Tue, Jun 08, 2004 at 05:53:26PM +0300, Gaal Yahas wrote:
> 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:
> 
Thank you for your time.
Unfortunately it doesn't work.
After a thorough check I found the following references in the manuals:

Archive::Zip :
       readFromFileHandle( $fileHandle, $filename )
           Read zipfile headers from an already-opened file handle, appending
           new members. Does not close the file handle.  Returns "AZ_OK" or
           error code. Note that this requires a seekable file handle; reading
           from a stream is not yet supported.

Archive::Zip FAQ:

Reading from streams
       Q: How do I read from a stream (like for the Info-Zip "funzip" pro-
       gram)?

       A:   This isn't currently supported, though writing to a stream is.

BUT, using IO::Scalar does work.
Here is the code that worked for me:

use LWP::UserAgent;
use IO::Scalar;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Archive::Zip::MemberRead

	my %param;
	my @content = ();
	my $url = 'http://my.url/file';
	my $ua = LWP::UserAgent->new(); #LWP::UserAgent because of proxy
	if ($param{-proxy}) {
	    $ua->proxy(['http'],$param{-proxy});
	}
	my $webcontent = $ua->get($url)->content();
	my $zipname = "filename.zip";
	my $fh = IO::Scalar->new(\$webcontent);
	my $zip = Archive::Zip->new();
	my $status = $zip->readFromFileHandle($fh);
	my @membername = $zip->memberNames();
	my $membername;
	my $line;
	for $membername (@membername) {
	    my $fh2 =  new Archive::Zip::MemberRead($zip, $membername);
	    while (defined($line = $fh2->getline())) {
		push(@content,$line);
	    }
	}

Of course the while{} cycle is useful mostly for ASCII files.
Thus we actually achieve the same effect as if a local file was Tied
to @content;

> -- 
> Gaal Yahas <gaal at forum2.org>
> http://gaal.livejournal.com/
>  
>  +++++++++++++++++++++++++++++++++++++++++++
>  This Mail Was Scanned By Mail-seCure System
>  at the Tel-Aviv University CC.

-- 
Roman M. Parparov - NASA EOSDIS project node at TAU technical manager.
Email: romm at empire.tau.ac.il		   http://www.nasa.proj.ac.il/
Phone/Fax: +972-(0)3-6405205 (work),         +972-(0)51-34-18-34 (home)
----------------------------------------------------------------------
The economy depends about as much on economists as the weather does on
weather forecasters.
                -- Jean-Paul Kauffmann



More information about the Perl mailing list