itzy
itzy

Reputation: 11755

How to combine zip files with Archive::Zip in Perl

I have two zip files, A.zip and B.zip. I want to add whatever files are in A.zip to B.zip.

How can I do this with Archive::Zip in Perl? I thought I could do something like this:

my $zipA = Archive::Zip->new();
my $zipB = Archive::Zip->new();

die 'read error' unless ($zipA->read( 'A.zip' ) == AZ_OK );

my @members = $zipA->memberNames();
for my $m (@members) {
 my $file = $zipA->removeMember($m);
 $zipB->addMember($file);
}

but unless I call writeToFileNamed() then no files get created, and if I do call it, B.zip gets overwritten with the contents of A.zip.

I could read in the contents of B.zip, and write them along with the contents of A.zip back to B.zip but this seems really inefficient. (My problem actually involves millions of text files compressed into thousands of zip files.)

Is there a better way to do this?

Upvotes: 1

Views: 987

Answers (2)

stevenl
stevenl

Reputation: 6798

Using Archive::Zip:

my $zipA = Archive::Zip->new('A.zip');
my $zipB = Archive::Zip->new('B.zip');

foreach ($zipA->members) {
    $zipA->removeMember($_);
    $zipB->addMember($_);
}
$zipB->overwrite;

The problem is:

  1. You need to add the actual members, not just the memberNames.
  2. You need to read B.zip before you can add to it.

(I'll leave it to you to do error handling etc)

Upvotes: 2

Yang Tang
Yang Tang

Reputation: 344

You may try chilkat::CkZip instead of Archive::Zip. Its QuickAppend() method seems to be helpful.

Upvotes: 0

Related Questions