gpt4 book ai didi

linux - 将文件添加到perl中的压缩文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:01 25 4
gpt4 key购买 nike

我有以下 perl 脚本,它旨在接受命令行参数,将所有用户数据文件存档到一个 zip 文件中,然后删除原始数据。该脚本没有问题,但是当以不同的用户作为参数再次运行时,它会覆盖 userData.zip 文件中的先前数据。我已经搜索过但无法找到如何执行此任务。它应该继续接受用户作为参数并将他们的文件夹附加到 userData.zip 文件。

感谢任何帮助。

use 5.14.2;
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Path;
my ($DATAFILEIN, $DATAFILEOUT);
my ($new,$zip);
use constant COLUMNS => 6;

sub main {
verifyArguments();
setDataFileIn();
zipFiles();
deleteUserFiles();
#setDataFileOut();
#printData();
#writeData();
}


sub verifyArguments {
if (!(@ARGV) || !(-e $ARGV[0])) {
die "\n\nYou must specify correct file name upon command invocation.\n\n";
}
}

sub setDataFileIn {
$DATAFILEIN = $ARGV[0];
}

sub zipFiles {
print "\nBacking up ".$DATAFILEIN."\n";
sleep 1;

$zip = Archive::Zip->new();
opendir (DIR, $DATAFILEIN) or die $!;

while (my $file = readdir(DIR)) {

# Use -f to look for a file
next unless (-f $DATAFILEIN."\\".$file);

$zip->addFile($DATAFILEIN."\\".$file, );
print "Added $file to zip\n";
}
closedir(DIR);
my $fileName = $DATAFILEIN;
unless ( $zip->writeToFileNamed('userData.zip') == AZ_OK ) {
die 'write error';
}
print "Successfully backed up $fileName to userData.zip\n";
}

sub deleteUserFiles{
rmtree($DATAFILEIN);

}


main();

最佳答案

您是否阅读过 Archive::Zip 常见问题解答的这一部分?

Can't Read/modify/write same Zip file

Q: Why can't I open a Zip file, add a member, and write it back? I getan error message when I try.

A: Because Archive::Zip doesn't (and can't, generally) read filecontents into memory, the original Zip file is required to stay arounduntil the writing of the new file is completed.

The best way to do this is to write the Zip to a temporary file andthen rename the temporary file to have the old name (possibly afterdeleting the old one).

Archive::Zip v1.02 added the archive methods overwrite() andoverwriteAs() to do this simply and carefully.

See examples/updateZip.pl for an example of this technique.

我在你的代码中没有看到 $zip->overwrite()

查找 CPAN 模块信息的最佳位置是 http://metacpan.org .在这种情况下,Archive::Zip页。该页面有一个指向 Archive::Zip::FAQ 的文档链接。您可以在那里阅读它,或者您可以在安装了该模块的系统上键入 perldoc Archive::Zip::FAQ

示例是下载包的一部分。如果您使用 cpan 命令安装 Archive::Zip,那么示例将位于构建位置。默认情况下,这将是 ~/.cpan/build/Archive-Zip-*/examples

关于linux - 将文件添加到perl中的压缩文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com