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

Hi Monks!

My code here is generating this error:\"Can't call method "desiredCompressionMethod" on an undefined value at c:/dir_test/cgi-bin/test/error.pl line 27."

I just don't understand where it could be undefined. I am trying to unzip a file.

Any Help???
Here is the code:
#!/perl/bin/perl -w use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use File::Find; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); $|=1; my $dir; my $zip ; my $zipped; print header(); #=comment # Read a Zip file my $somezip = Archive::Zip->new(); unless ( $somezip->read( 'c:/dir_test/cgi-bin/test/error.zip' ) == +AZ_OK ) { die 'read error'; } # Change the compression type for a file in the Zip my $member = $somezip->memberNamed( 'unzipped.txt' ); $member->desiredCompressionMethod(COMPRESSION_DEFLATED); unless ( $zip->writeToFileNamed( 'some.zip' ) == AZ_OK ) { die 'write error'; }

Replies are listed 'Best First'.
Re: Unzipping a File
by Hofmator (Curate) on Nov 14, 2006 at 15:42 UTC
    Well, it seems that $somezip->memberNamed( 'unzipped.txt' ); returns undef - which probably means that there is no file by that name in the zip archive.

    What does print $somezip->memberNames(); show?

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Unzipping a File
by andyford (Curate) on Nov 14, 2006 at 15:40 UTC

    I would guess that unzipped.txt doesn't exist, thus causing $member to be undefined. Try testing for unzipped.txt before trying to operate on it.

    non-Perl: Andy Ford

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Unzipping a File
by ahmad (Hermit) on Nov 14, 2006 at 19:49 UTC

    your code should work fine , it dies because the file you

    want to extract is actually not inside the archive

    you'll need to add extra code to check if the file found or not

    #After this code my $member = $somezip->memberNamed( 'unzipped.txt' ); # Add this line if ( not defined $member ) { die "File not found inside the archive"; }else{ # the rest of your code goes here }

    by the way your code actually take the file from one archive file to another one

Re: Unzipping a File
by chanakya (Friar) on Nov 15, 2006 at 07:38 UTC
    Why dont you try unzipping the zip file and see if the file exists.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $obj = Archive::Zip->new("./DBtest.zip"); my @members = $obj->memberNames(); #Get the list of members print Dumper @members; foreach my $memb (@members){ my $ret = $obj->extractMember($memb, "/tmp/test-folder/$memb") +; if($ret != 0){ print "Problem extracting $memb\n"; } }
    Hope this helps
      Thanks a Lot!!!
      It was a great help!!!!!