perl - Archive::Tar 模块备份文件和文件夹,但不备份文件夹中的文件
问题描述
基本问题。
我想使用 Perl 的 Archive::Tar 模块来备份东西。我想Archive::Tar
用来备份文件和文件夹以及文件夹中的文件。使用我当前的代码,前两个有效,但第三个无效。
重现步骤:
- 创建示例目录。
- 在示例目录中,创建两个目录和一个 .txt 文件。
- 使用 vim,在 .txt 文件中写入任何内容。
- 在示例目录的其中一个目录中,重复步骤二和三。
- 在示例目录中,创建一个 perl 脚本。
- 将下面的代码复制到脚本中。
- 运行脚本。在框中,键入 .txt 文件和两个目录的名称。
- 按下大压缩按钮。
- 在压缩目录中打开新的 .tbz 文件。
代码。
use strict; use warnings;
use Archive::Tar;
use Tk;
my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;
my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;
$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;
MainLoop;
sub compress {
my $input = $inputEntry -> get;
my @input;
unless ( $input !~ m/ / ) {
@input = split ( m/ /, $input );
} else {
@input = ( $input );
}
Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}
解决方案
您的重现步骤不清楚,更多示例可能会有所帮助,但您的脚本似乎对我来说很好。我能够成功地将文件夹和文件以及文件夹中的文件添加到存档中。对于文件夹中的每个文件,我必须输入整个相对路径,例如。'样本/file1.txt'。
您可能正在寻找一种在文件夹中自动添加文件的方法。像这样的东西可以解决问题:
use strict; use warnings;
use Archive::Tar;
use Tk;
my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;
my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;
$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;
MainLoop;
sub compress {
my $input = $inputEntry -> get;
my @input;
my @dirfiles;
unless ( $input !~ m/ / ) {
@input = split ( m/ /, $input );
} else {
@input = ( $input );
}
foreach(@input) { if(-d $_) { push(@dirfiles,glob "'${_}/*'"); } }
push(@input,@dirfiles);
Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}
我认为仍然存在一些问题,例如:即使在转义或引用的情况下,此脚本似乎在名称中带有空格的文件/文件夹上也会失败。
推荐阅读
- lotus-notes - 保存为文本字段的 Lotus Notes 数据转换为日期/时间
- python-3.x - 使用 lambdas 和标准 python 函数时得到不同的结果
- mvvm - 如何在层次网格 ZK 上处理 selectedItem
- java - 如何实例化泛型类型?
- python - 如何修改 Wordnet Lemmatizer 以限制特定单词?
- javascript - 如何在归一化输出中返回两个归一化值的总和
- java - 通过将值传递给jsp而不使用javascript自动填充来自servlet的值
- python - “函数参数”的引用计数与“Python 的函数堆栈”的引用计数有何不同 - Python
- python-3.x - 我无法在 python 中使用 OAUTH 2.0 SAML Assertion Bearer 流获取访问令牌
- python - Postgres 现有列不存在错误