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

Hi Monks!
I am trying to unzip files into a directory after grabbing this file(.zip) using FTP. I am getting an error "Read of Archive::Zip::Archive=HASH(0x8d41a58) failed". I am posting some of the code here and maybe someone will know what is going wrong. Or maybe there is a simpler way to do this
Here is the code:
#!/usr/bin/perl -w use strict; use CGI::Carp qw( fatalsToBrowser set_message ); use File::Temp qw/ :POSIX /; use Net::FTP; use Archive::Zip qw(:ERROR_CODES); # Set up FTP connection my $ftp = Net::FTP->new( $myhost ); $ftp->login( $myuser, $mypass ) || die "Could not login to $host: $ +@"; $ftp->cwd( $ftpsourcedir ) || die "Could not cwd $ftpsourcedir: $@" +; $ftp->binary(); # Get a list of files my @dircontents = $ftp->dir, "\n" || die "Could not list $ftpsource_di +r: $@"; @dircontents = sort( @dircontents ); foreach my $filestats (@dir_contents) { $err_msg = ''; my @stats = split(' ', $filestats); my $filename = $stats[5]; my $file_size = $stats[1]; # I can see the zip file name here. print "\n$filename\n\n"; #unzip these files into a direcotory my $zip = Archive::Zip->new(); my $status = $zip->read($filename); my @members = $zip->memberNames(); die "Read of $zip failed\n" if $status != AZ_OK; foreach (@members) { $zip->extractMember("$_", "temp/$_"); }

Thanks!

Replies are listed 'Best First'.
Re: Unzipping files into a directory issue!
by Eliya (Vicar) on Feb 17, 2011 at 21:01 UTC
    ...after grabbing this file(.zip) using FTP

    Maybe I'm missing something, but I don't see you actually fetching the zip file(s).  All you do is a (remote) directory listing ($ftp->dir), and then you try to unzip the listed files locally on the machine the script runs on...

    Also (maybe just a typo), @dir_contents is not @dircontents.

      I am fetching the file here:
      # Set up FTP connection my $ftp = Net::FTP->new( $myhost ); $ftp->login( $myuser, $mypass ) || die "Could not login to $host: $ +@"; $ftp->cwd( $ftpsourcedir ) || die "Could not cwd $ftpsourcedir: $@" +; $ftp->binary(); # Get a list of files my @dircontents = $ftp->dir, "\n" || die "Could not list $ftpsource_di +r: $@"; @dircontents = sort( @dircontents );

      Yes it was a typo for demonstrate the issue on "@dir_contents is not @dircontents.", it is actually @dircontents.
        I am fetching the file here: ...

        No, you're not :)

        $ftp->dir only retrieves the file names from the remote directory, not the files themselves.  For actually fetching the files, there's the ->get method.

Re: Unzipping files into a directory issue!
by davido (Cardinal) on Feb 17, 2011 at 20:41 UTC

    Just a thought: Have you verified that $stats[5] contains a filename? Try printing $stats[5] to verity it contains what you think it does. That may not be the issue, but it's a simple thing to check in your quest to eliminate suspects.


    Dave

      I do verify the file and I know that file is there. May be there is a better way to do this.
Re: Unzipping files into a directory issue!
by Gulliver (Monk) on Feb 17, 2011 at 23:37 UTC

    You probably wanted to have "Read of $filename failed\n" not "Read of $zip..." The way you have it is printing the zip object.

    You might try a file check like if(-e $filename) before you try to read the archive. Then if it exists, do something like

    unless($zip->read($filename) == AZ_OK ) {die "failed to open $filename +";}
      I know at this point that $filename exists, and still with $filename been true it still doesn't work, thats why I can't figure it out.