简体   繁体   English

Perl:如何在解压缩tar.gz文件时获取新目录

[英]Perl: How to get new directory made while unziping tar.gz files

I want to make a perl script that extracts a file, then opens the result and dose stuff in there.I have this code: 我想创建一个提取文件的perl脚本,然后在那里打开结果和剂量。我有这样的代码:

$res = qx{tar -xvf $tmp -C /tmp/}; #$tmp was specified earlier

but I don't know how to see what new directory was created? 但我不知道如何看到创建了什么新目录?

This may not be the most efficient way, but I'm new to Perl and don't know all the modules. 这可能不是最有效的方式,但我是Perl的新手并且不了解所有模块。

One option is to parse the output of tar, which gets messy and can change between versions of tar. 一种选择是解析tar的输出,它会变得混乱,并且可以在tar版本之间进行更改。

Or you can untar in an empty directory. 或者您可以在空目录中解压缩。 Then whatever is in the directory after untaring is what came from the tarball. 然后解压后目录中的任何内容都是来自tarball的内容。 This works even if the tarball is impolite and does not create a directory. 即使tarball不礼貌并且不创建目录,这也可以工作。

use File::Temp;

my $tempdir = File::Temp->newdir;
qx{tar -xvf $tarball -C $tempdir};

Or you can also use Archive::Tar or Archive::Extract to list the files in the archive. 或者您也可以使用Archive :: TarArchive :: Extract列出存档中的文件。 Archive::Extract has the benefit of being a generic interface to all sorts of archive formats. Archive :: Extract具有作为各种存档格式的通用接口的好处。

use Archive::Extract;

my $ae = Archive::Extract->new( archive => $tarball );
my $files = $ae->files;

您可以在解压缩tarball之前构建一个目录列表(使用readdir()),然后重新生成它,并推断出新列表中没有原始列表中的任何内容都来自tarball。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM