【问题标题】:how to extract tar files to destination directory in Perl如何在 Perl 中将 tar 文件提取到目标目录
【发布时间】:2014-11-02 06:34:47
【问题描述】:

我是 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 $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;

当我运行这个脚本时,我收到一个错误:存档中没有这样的文件。

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

【问题讨论】:

    标签: perl tar


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多