Perl Archive::zip -- 文件未添加

发布于 2024-11-05 01:33:59 字数 1653 浏览 0 评论 0原文

我正在遍历目录树(颠覆备份)并将找到的每个文件和目录添加到 zip 文件中。这工作起来非常流畅。但是有两个文件丢失。

sub zipFolder
{
    my $dir = 'D:\\SVN-Backup\\EnterpriseDataRepository\\20110502-0630';
    my $zip = Archive::Zip->new();
    my $zipped = $zip->addDirectory($dir);
    $zipped->desiredCompressionLevel( 9 );
    $zipped->desiredCompressionMethod( COMPRESSION_DEFLATED );
        print "Before find\n";
    find(\&zip_file, $dir);
    print "after find\n";
    die 'write error' unless $zip->writeToFileNamed('D:/SVN-Backup/CCBuild/backup.zip' ) == AZ_OK;

    sub zip_file 
    {
          print $File::Find::name;
          if ( -d $File::Find::name ) { # just grab directories, not files.
            $zip->addDirectory($File::Find::name);
            print " : dir\n";
         } else { #zip files
           print " : file\n";
           $zip->addFile($File::Find::name) != AZ_OK ||  print "couldn't add file \n";
         }

    }
}

文件 D:\SVN-Backup\20110502-0630/db/revprops/0/0 将不会添加到 zip 存档中。知道我可以做什么来添加这个文件吗?

...
D:\SVN-Backup\20110502-0630/db/uuid : file
D:\SVN-Backup\20110502-0630/db/revprops : dir
D:\SVN-Backup\20110502-0630/db/revprops/0 : dir
D:\SVN-Backup\20110502-0630/db/revprops/0/0 : file
D:\SVN-Backup\20110502-0630/db/revprops/0/1 : file
D:\SVN-Backup\20110502-0630/db/revprops/0/10 : file
...

祝贺安迪的贡献。他解决了问题。

相关错误: rt.cpan.org/Public/Bug/Display.html?id=27463 , 但这只是关于文件,而不是 目录。什么版本的 Archive::Zip 你有吗?

我有 perl 5.8.8,它似乎比修复程序更旧。

I am traversing a directory tree (subversion backup) and add every file and directory that I find to a zip file. This works pretty slick. However two files are missing.

sub zipFolder
{
    my $dir = 'D:\\SVN-Backup\\EnterpriseDataRepository\\20110502-0630';
    my $zip = Archive::Zip->new();
    my $zipped = $zip->addDirectory($dir);
    $zipped->desiredCompressionLevel( 9 );
    $zipped->desiredCompressionMethod( COMPRESSION_DEFLATED );
        print "Before find\n";
    find(\&zip_file, $dir);
    print "after find\n";
    die 'write error' unless $zip->writeToFileNamed('D:/SVN-Backup/CCBuild/backup.zip' ) == AZ_OK;

    sub zip_file 
    {
          print $File::Find::name;
          if ( -d $File::Find::name ) { # just grab directories, not files.
            $zip->addDirectory($File::Find::name);
            print " : dir\n";
         } else { #zip files
           print " : file\n";
           $zip->addFile($File::Find::name) != AZ_OK ||  print "couldn't add file \n";
         }

    }
}

The file D:\SVN-Backup\20110502-0630/db/revprops/0/0 will not be added to the zip archive. Any idea what I can do to get the this file added as well?

...
D:\SVN-Backup\20110502-0630/db/uuid : file
D:\SVN-Backup\20110502-0630/db/revprops : dir
D:\SVN-Backup\20110502-0630/db/revprops/0 : dir
D:\SVN-Backup\20110502-0630/db/revprops/0/0 : file
D:\SVN-Backup\20110502-0630/db/revprops/0/1 : file
D:\SVN-Backup\20110502-0630/db/revprops/0/10 : file
...

Congrats to Andy for his contribution. He solved the Problem.

A related bug:
rt.cpan.org/Public/Bug/Display.html?id=27463,
but that's only about files, not
directories. What version of
Archive::Zip do you have?

I have perl 5.8.8 which does seem to be older than the fix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2024-11-12 01:33:59

我发现了CPAN bug 27463,如果您有旧版本的 Archive::Zip (<= 1.20,据我从 CPAN 可以看出)。

正如史努比和我猜测的那样,该错误是由于文件名 "0" 在用作条件时为 false 造成的。

I found CPAN bug 27463, which would explain the issue if you have an older version of Archive::Zip (<= 1.20, as best I can tell from CPAN).

As both snoopy and I guessed, the bug was due to the filename "0" being false when used as a condition.

非要怀念 2024-11-12 01:33:59

<罢工>
我正在查看 Archive::Zip 的源代码。这个addFile方法位于Archive::Zip::Archive中,它又调用addMember在同一源文件中:

sub addMember {
    my $self       = shift;
    my $newMember  = ( ref( $_[0] ) eq 'HASH' ) ? shift->{member} : shift;
    push( @{ $self->{'members'} }, $newMember ) if $newMember;
    return $newMember;
}

当 $newMember 为“0”(假)时,此子程序将失败。我认为 Archive::Zip 可能需要针对它提交 RT 票证,以及修复上述问题的补丁。


I'm having a look at the source code for Archive::Zip. This addFile method is in Archive::Zip::Archive, which in turn calls addMember in the same source file:

sub addMember {
    my $self       = shift;
    my $newMember  = ( ref( $_[0] ) eq 'HASH' ) ? shift->{member} : shift;
    push( @{ $self->{'members'} }, $newMember ) if $newMember;
    return $newMember;
}

This sub will fail when $newMember is '0' (false). I think Archive::Zip may need an RT ticket submitted against it, with a patch to fix the above.

独夜无伴 2024-11-12 01:33:59

您可以递归地添加整个目录:

    my $zip = Archive::Zip->new();
    $zip->addTree('C:\~~~\some dir', 'new name for dir');
    $zip->writeToFileNamed( 'C:\~~~\great.zip' );

这会将目录及其内容添加到 zip 存档中,并保留所有相关内容。

You can add the whole directory recursively:

    my $zip = Archive::Zip->new();
    $zip->addTree('C:\~~~\some dir', 'new name for dir');
    $zip->writeToFileNamed( 'C:\~~~\great.zip' );

This will add directory and of it's content to zip archive with all relative preserved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文