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

SOLVED (Scroll down for solution)

I have some zip files.

my @filelist = ('zip1.zip','zip2.zip','zip3.zip');

Let's say that the contents of the zip files look like this:

zip1.zip -->file1.txt -->file2.txt zip2.zip -->directory1 (dir) ---->file1.txt ---->file3.txt -->file4.txt zip3.zp -->file5.txt -->file6.txt -->file7.txt

I wish to dynamically create a zip file called bigzip.zip that will include the contents of the zip files as a sub-directory in bigzip.zip. In other words, I want this:

bigzip.zip -->zip1 (dir) ---->file1.txt ---->file2.txt -->zip2 (dir) ---->directory1 (dir) ------>file1.txt ------>file3.txt ---->file4.txt -->zip3 (dir) ---->file5.txt ---->file6.txt ---->file7.txt

Here's the code I have so far:

my $zip = Archive::Zip->new(); foreach (@filelist) { my $file = Archive::Zip->new($_}); foreach ($file->members()){ $zip->addMember( $_ ); } }

This successfully results in:

bigzip.zip -->file1.txt -->file2.txt -->directory1 (dir) ---->file1.txt ---->file3.txt -->file4.txt -->file5.txt -->file6.txt -->file7.txt

I can't figure out from the documentation if what I'm talking about is at all possible, and how to achieve it. I could, of course, extract the individual zips into the directories I want, then re-zip them, but that seems inelegant.

Any thoughts?

Solved: The following code works. I didn't realize that "directories" in zip files were simply a matter of specifying paths...

my $x=1; my $zip = Archive::Zip->new(); foreach (@filelist) { my $file = Archive::Zip->new($_); my $directoryname = 'zip'.$x; foreach ($file->members()){ my $m = $zip->addMember( $_ ); $m->fileName($directoryname.'/'.$m->fileName()); } $x++; }

Replies are listed 'Best First'.
Re: Archive::Zip: add members of one archive to sub-directory in another archive (elegant)
by Anonymous Monk on May 06, 2015 at 08:07 UTC

    ... inelegant... Any thoughts?

    tunafish, that is the mark of a noob, looking for the best or simplest or ... first get something that works, because good enough should be your goal, it works for nature :)