...without downloading the whole thing. With some debugging help from other Monks, I put together the modules Net::FTP::AutoReconnect and Net::FTP::RetrHandle this weekend; they can be used in conjunction with Archive::Zip to read a Zip file over FTP, pulling out only the needed parts. For example, to get a list of files, it will just download the Zip file's Table of Contents structure; to extract one file, it will download just the portion of the Zip archive containing that file. I'm using it to extract individual files from 2GB archives, and of course it's a dramatic speed increase; for somebody on a slow Internet connection the increase would be even larger.

It does by implementing a fake filehandle that supports seek, implemented by using the FTP REST and ABOR commands to start the download at an arbitrary point, and end it when we've read all we want. Some FTP servers will disconnect you if you ABORt a transfer, but Net::FTP::AutoReconnect will re-connect as if nothing happened. When Archive::Zip is asked to open an archive on the fake filehandle, it doesn't even know what hit it.

Here's some sample code using these modules:

#!/usr/bin/perl use warnings; use strict; use Net::FTP; use Net::FTP::AutoReconnect; use Net::FTP::RetrHandle; use Archive::Zip; my $ftp = Net::FTP::AutoReconnect->new("ftp.info-zip.com", Debug = +> $ENV{DEBUG}) or die "connect error\n"; $ftp->login('anonymous','example@example.com') or die "login error\n"; $ftp->cwd('/pub/infozip/UNIX/LINUX') or die "cwd error\n"; my $fh = Net::FTP::RetrHandle->new($ftp,'unz551x-glibc.zip') or die "Couldn't get handle to remote file\n"; my $zip = Archive::Zip->new($fh) or die "Couldn't create Zip object\n"; foreach my $fn ($zip->memberNames()) { print "unz551-glibc.zip: $fn\n"; }

Sorry for the bit of tooting my own horn, but this was such a fun hack I had to tell somebody, and I thought that My Fellow Monks would appreciate it. :)

Replies are listed 'Best First'.
Re: Extracting individual files from a Zip archive over FTP...
by Ace128 (Hermit) on Apr 10, 2006 at 03:46 UTC
    Hey, havent tried it yet, but sounds really cool and usefull!
Re: Extracting individual files from a Zip archive over FTP...
by Anonymous Monk on Oct 20, 2010 at 13:34 UTC
    hello, Your code was quite helpful, but i need some more help : after creating the zip object what i do is : my $member1 = $zip->memberNamed("filename.csv"); my $string = $member1->contents(); print Dumper $string; $member1 object is created but it gives me error while reading the contents, I have tried few other methods too but gives different errors, Please help me out Thanks anyways as the above code was really cool stuff