perl - 如何将 tar 文件解压到目标目录?

标签 perl tar

我正在尝试编写一个脚本,该脚本获取 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 $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;

我收到错误:存档中没有此类文件。

您能否帮助我了解我做错了什么以及正确的命令是什么?

最佳答案

Archive::Tar extract 方法不允许指定输出目录;它只允许输入文件列表。您需要使用 extract_file 方法:

    $tar->extract_file($extracted_file, $output_dir);

关于perl - 如何将 tar 文件解压到目标目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689781/

相关文章:

regex - 计算 qr 正则表达式中的捕获组?

perl - WWW::Mechanize::Chrome 如何关闭选项卡

regex - 如何使用正则表达式删除文件开头的文本?

linux - 无法使用 tar 提取 nodejs 源文件

c - 解析 tar header 时的错误值

bash - 从 tar 存档中删除重复项

perl - 如何在 perl mason CGIHandler 中获取 QUERY_STRING?

linux - 如何使用 tar 命令排除特定文件?

pipe - xargs: tar: 由信号 13 终止

javascript - Server-Sent Events 究竟是如何工作的?