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

Hi, is addTree not the right method to use when adding a subdirectory to a zip file?

I keep getting "Can't locate object method "addTree" via package "Archive::Zip::Archive" (perhaps you forgot to load "Archive::Zip::Arch ive"?)"

Can someone tell me if this is the correct method to use?

When I take out the line with the addTree method, everything works and I get a perfectly created zip file. Here is the code:

------------------------------------------------

#!/opt/ActivePerl-5.6/bin/perl # Archive::Zip is available for ActiveState perl use strict; use warnings; use Archive::Zip qw ( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new(); my @files = glob("*.txt"); foreach (@files) { my $member = $zip->addFile($_); die "Cannot add $_ to zip!" unless $member; } ################################## ### RIGHT HERE, the addTree ###### ################################## $zip->addTree("dir1"); $zip->writeToFileNamed( "zipfile.zip" );

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: $zip->addTree(...) isn't found.
by halley (Prior) on Jun 21, 2007 at 16:44 UTC
    A brief examination of the documentation shows addDirectory() but no addTree(). You can get documentation by looking at Archive::Zip or typing on your own command line:
    perldoc Archive::Zip

    Update: After checking the CPAN docs, newer versions of Archive::Zip do merge in the ::Tree functions. Older builds (such as what I consulted earlier) do not have these methods. Reading your perldoc will generally match the version you're using, so I recommend it.

    --
    [ e d @ h a l l e y . c c ]

      Hi, thanks so much for your response.

      It is greatly appreciated.

      You know, the addDirectory didn't do anything.

      It didn't produce any errors, but it just didn't do anything.

      Would you know why? I'm going to go troubleshoot it now.
        addDirectory( $directoryName [, $fileName ] ) Append a member created from the given directory name. The dir +ectory name does not have to name an existing directory. If the named directory exists, the file modification time and permissions a +re set from the existing directory, otherwise they are set to now and permissive default permissions. $directoryName must be in loca +l file system format. The optional second argument sets the name of t +he archive member (which defaults to $directoryName). If given, i +t must be in Zip (Unix) format. Returns the new member.
Re: $zip->addTree(...) isn't found.
by syphilis (Archbishop) on Jun 21, 2007 at 16:43 UTC
    Not sure about your usage, but you should certainly be able to $zip->addTree("dir1"); without getting that error you reported ... using the latest version of Archive::Zip, anyway.

    Which version of Archive::Zip are you running ?
    What does 'perldoc Archive::Zip' have to say about the addTree method ?

    Cheers,
    Rob