Perl Archive::zip -- 文件未添加
我正在遍历目录树(颠覆备份)并将找到的每个文件和目录添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了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.<罢工>
我正在查看 Archive::Zip 的源代码。这个addFile方法位于Archive::Zip::Archive中,它又调用addMember在同一源文件中:
当 $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:
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.
您可以递归地添加整个目录:
这会将目录及其内容添加到 zip 存档中,并保留所有相关内容。
You can add the whole directory recursively:
This will add directory and of it's content to zip archive with all relative preserved.