【问题标题】:How to combine zip files with Archive::Zip in Perl如何在 Perl 中将 zip 文件与 Archive::Zip 合并
【发布时间】:2011-12-07 16:45:03
【问题描述】:

我有两个 zip 文件,A.zipB.zip。我想将A.zip 中的所有文件添加到B.zip

如何在 Perl 中使用 Archive::Zip 来做到这一点?我以为我可以这样做:

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);
}

但除非我调用 writeToFileNamed(),否则不会创建任何文件,如果我调用它,B.zip 会被A.zip 的内容覆盖

我可以读入B.zip 的内容,然后将它们与A.zip 的内容一起写回B.zip,但这似乎效率很低。 (我的问题实际上涉及将数百万个文本文件压缩成数千个 zip 文件。)

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: perl zip


    【解决方案1】:

    使用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;
    

    问题是:

    1. 您需要添加实际成员,而不仅仅是成员名称。
    2. 您需要先阅读 B.zip,然后才能添加。

    (我会留给你做错误处理等)

    【讨论】:

    • 真的需要$zipA->removeMember($_);吗?
    【解决方案2】:

    您可以尝试chilkat::CkZip 而不是Archive::Zip。它的QuickAppend() 方法似乎很有帮助。

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多