Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Archive::Zip - what am I missing?

by regexes (Hermit)
on Apr 10, 2008 at 08:28 UTC ( [id://679433]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks!

This is probably something trivial I'm overlooking but I can't seem to figure out what I'm doing wrong.

What I'm trying to do is replace a file in a zip archive.

The code..
#!/usr/bin/perl use strict; use Archive::Zip qw(:ERROR_CODES); my $zipFile = Archive::Zip->new(); my $status = $zipFile->read( '/home/clueless/blabla.zip' ); my $oldimage1 = 'branch/stick/image1.jpeg'; my $newimage1 = '/home/clueless/newimage1.jpeg'; my $oldmember = $zipFile->replaceMember( $oldimage1, $newimage1 ); my $stat = $zipFile->overwrite();
When running this, I receive the following error:
Can't call method "_writeToFileHandle" without a package or object ref +erence at /usr/local/share/perl/5.8.8/Archive/Zip/Archive.pm line 280
The problem does not appear to be with the replace method because $oldmember contains image1.jpg, i.e. it returns correctly.

The error occurs when trying to update, i.e with the overwrite method.

Any help appreciated...

regexes


-------------------------
Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan "press on" has solved and always will solve the problems of the human race.
-- Calvin Coolidge, 30th President of the USA.

Replies are listed 'Best First'.
Re: Archive::Zip - what am I missing?
by poolpi (Hermit) on Apr 10, 2008 at 10:39 UTC

    It works like this :

    #!/usr/bin/perl -w use strict; use Archive::Zip qw(:ERROR_CODES); use Data::Dumper; my $z = Archive::Zip->new(); unless ( $z->read( 'img.zip' ) == AZ_OK ) { die 'read error'; } my $old = $z->removeMember( 'old.img' ); my $new = $z->replaceMember( $old, 'new.img' ); print Dumper $z->members();

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      Try it, the file doesn't get replaced. You need my $newimage1 =  Archive::Zip::Member->newFromFile(
        Thanks for the help! I knew I had to be missing something... yes, that allows newimage1.jpeg to be included into the zip archive, however, it puts it in the central directory.
        #!/usr/bin/perl use strict; use Archive::Zip qw(:ERROR_CODES); my $zipFile = Archive::Zip->new(); my $status = $zipFile->read( '/home/clueless/blabla.zip' ); my $oldimage1 = 'branch/stick/image1.jpeg'; my $newimage1 = Archive::Zip::Member->newFromFile( 'newimage1.jpeg' ); my $oldmember = $zipFile->replaceMember( $oldimage1, $newimage1 ); my $stat = $zipFile->overwrite();
        I'm trying to replace image1.jpeg in the branch/stick archive directory.

        Maybe my thought process is incorrect but it would seem to me that a replaceMember method would put the new member in exactly the same location as the old one. It does not appear to be that way... must I declare the archive directory as well? How do I do so? I didn't find any specific methods which appeared to handle this... (of course, I could be overlooking something again.)

        regexes
Re: Archive::Zip - what am I missing?
by andreas1234567 (Vicar) on Apr 10, 2008 at 10:33 UTC
    I get almost the same error:
    Can't locate object method "_writeToFileHandle" via package "image2.gi +f" (perhaps you forgot to load "image2.gif"?) at /usr/lib/perl5/site_ +perl/5.8.5/Archive/Zip/Archive.pm line 280.
    I added a warn line around line 280 in Archive.pm:
    282 foreach my $member ( $self->members() ) { 283 use Data::Dumper; 284 warn ("DEBUG: member: " . Dumper($member)); 285 my $retval = $member->_writeToFileHandle( $fh, $fhIsSe +ekable, $o 285 ffset );
    and it spits out:
    DEBUG: member: $VAR1 = 'image2.gif'; Can't locate object method "_writeToFileHandle" via package "image2.gi +f" (perhaps you forgot to load "image2.gif"?) at /usr/lib/perl5/site_ +perl/5.8.5/Archive/Zip/Archive.pm line 285.
    To me it looks like $member is a scalar string when it should have been a ref to a Archive::Zip::Member or something. I suggest you investigate this a little further and file a bug report.
    --
    Andreas
      No, doc says replaceMember( $memberOrName, $newMember )
        In that case I would suggest a little input validation:
        $ diff -ubB /usr/lib/perl5/site_perl/5.8.5/Archive/Zip/Archive.pm Arch +ive.pm --- /usr/lib/perl5/site_perl/5.8.5/Archive/Zip/Archive.pm 2007-1 +1-07 14:02:23.000000000 +0100 +++ Archive.pm 2008-04-10 13:06:07.000000000 +0200 @@ -134,6 +134,11 @@ sub replaceMember { my ( $self, $oldMember, $newMember ) = @_; $oldMember = $self->memberNamed($oldMember) unless ref($oldMember +); + + if (ref($oldMember) ne q{Archive::Zip::Member}){ + die q{First argument to replaceMember() must be ref to Archive +::Zip::Member}; + } + return undef unless $oldMember; return undef unless $newMember; my @newMembers =
        --
        Andreas
Re: Archive::Zip - what am I missing?
by Anonymous Monk on Apr 10, 2008 at 10:36 UTC
    $newimage1 needs to be a member, not a name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://679433]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2024-03-28 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found