如何在Perl中将tar文件提取到目标目录
我是Perl新手,我正在尝试编写一个脚本,获取tar文件的路径,检查其中是否有txt文件,如果是,则将该文件提取到输出文件夹。这是我的密码:如何在Perl中将tar文件提取到目标目录,perl,tar,Perl,Tar,我是Perl新手,我正在尝试编写一个脚本,获取tar文件的路径,检查其中是否有txt文件,如果是,则将该文件提取到输出文件夹。这是我的密码: #!/usr/bin/perl use strict; use warnings; use Archive::Tar; use File::Spec::Functions qw(catdir); #checking if tar file contain a txt file, if yes extract it to a folder my $ta
#!/usr/bin/perl
use strict;
use warnings;
use Archive::Tar;
use File::Spec::Functions qw(catdir);
#checking if tar file contain a txt file, if yes extract it to a folder
my $tarPath = 'path/to/tarArchive';
my $tar = Archive::Tar->new($tarPath);
my @files = $tar->list_files;
my $output_dir = 'C:/output/path';
foreach my $file (@files) {
if ( $file =~ /txt$/ ) {
my $extracted_file = catdir( $fileName, $file );
my $extracted_path = catdir( $output_dir, $file );
$tar->extract( $extracted_file, $output_dir );
print $extracted_file;
}
}
exit 0;
当我运行这个脚本时,我得到一个错误:存档中没有这样的文件
请您帮助我了解我做错了什么,正确的命令应该是什么?提取方法不允许您指定输出目录;它只允许输入文件的列表。您需要使用提取\u文件
方法:
$tar->extract_file($extracted_file, $output_dir);