Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

problem with Archive::Zip

by fionbarr (Friar)
on Aug 09, 2010 at 11:55 UTC ( #853788=perlquestion: print w/replies, xml ) Need Help??
fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I am trying:
my $zip = Archive::Zip->new(); $zip->addFile($renamed_file)->desiredCompressionLevel(0); if( $zip->writeToFileNamed($zip_filename) != AZ_OK ) { print "wr +ite error...$!"; } print "Added $renamed_file to $zip_filename\n"; }
and getting:
Can't call method "desiredCompressionLevel" on an undefined value at / +home/fds/lang/perl/fdsPerl-20100809/lib/site_perl/5.12.1/Archive/Zip/ line 249. at /home/fds/lang/perl/fdsPerl-20100809/lib/site_perl/5.12.1/Archive/ +Zip/ line 249 Archive::Zip::Archive::addFile('Archive::Zip::Archive=HASH(0x1 +49ff318)', 'vm.don') called at line 75

Replies are listed 'Best First'.
Re: problem with Archive::Zip
by Utilitarian (Vicar) on Aug 09, 2010 at 12:06 UTC
    From Archive::Zip
    If the name given does not represent a readable plain file or symbolic + link, undef will be returned
    It looks as though $renamed_file is not "a readable plain file or symbolic link".

    Try wrapping your code with this check to be sure:

    if ( ( -r $renamed_file )&& ( ( -f $renamed_file ) || ( -l $renamed_fi +le ) ) ){ #add to zip } else{ print "$renamed_file is not a readable valid file id\n" }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      that was it simple but I wasn't seeing it. Thank you.
Re: problem with Archive::Zip
by almut (Canon) on Aug 09, 2010 at 12:01 UTC

    My guess would be that the $zip->addFile($renamed_file) failed and returned undef...  (so you're then trying to call desiredCompressionLevel(0) on this undefined value)

Re: problem with Archive::Zip
by talexb (Canon) on Aug 09, 2010 at 18:28 UTC

    I'd recommend more defensive programming, as follows:

    my $zip = Archive::Zip->new() or croak "Failed to create a new archive"; $zip->addFile($renamed_file)->desiredCompressionLevel(0) or croak "Failed to add $renamed_file) to archive"; if( $zip->writeToFileNamed($zip_filename) != AZ_OK ) { croak "Failed to write to archive: $!"; } print "Added $renamed_file to $zip_filename\n";

    I've merely copied your use of $! in the last croak; I'm not sure if that ever gets populated by Archive::Zip.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: problem with Archive::Zip
by Khen1950fx (Canon) on Aug 10, 2010 at 10:47 UTC
    I fixed a few mistakes. Here's what I came up with. Compare it to your script, and you'll see the mistakes.
    !/usr/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $file1 = '/root/Desktop/'; my $file2 = '/root/Desktop/'; my $zip = Archive::Zip->new(); my $file_member = $zip->addFile($file1, 'copy_log'); $file_member->desiredCompressionMethod( COMPRESSION_STORED ); unless ( $zip->writeToFileNamed($file2) == AZ_OK ) { die 'write error'; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://853788]
Approved by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2018-07-23 16:03 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (472 votes). Check out past polls.