Extracting individual files from a Zip archive over FTP...
sgifford
created: 2006-04-03 12:32:44
...without downloading the whole thing. 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.

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 [cpan://Net::FTP::AutoReconnect] will re-connect as if nothing happened. When [cpan://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. :)

Re: Extracting individual files from a Zip archive over FTP...
created: 2006-04-09 23:46:56
Hey, havent tried it yet, but sounds really cool and usefull!

perlmonks.org content © perlmonks.org and Ace128, sgifford

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03