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

I'm a little confused about how to add a directory and the files in it to a zip file using Archive::Zip

$zip->addDirectory($name); #creates empty directory $zip->addFile($name); #adds file to zip's root folder # I have no clue how to add a file to the newly created directory $zip->addTree($dir,$zipFoldername); #works, but will include all of dir's subfolders if any exist

Replies are listed 'Best First'.
Re: directories in Archive::Zip
by Athanasius (Archbishop) on Feb 08, 2014 at 03:16 UTC

    Hello expat, and welcome to the Monastery!

    Use the addFileOrDirectory method and set zipName to the desired destination:

    #! perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $dirname = 'Foo/'; my $filename = 'Bar.txt'; my $zip = Archive::Zip->new(); $zip->addDirectory($dirname); $zip->addFileOrDirectory( { name => $filename, zipName => $dirname . $filename } ); $zip->writeToFileNamed('test.zip') == AZ_OK or die "Write error: $!";

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,