将 tar 文件归档到 Perl 中的不同位置
我正在读取一个包含一些存档文件的目录,并将存档文件一一解压缩。
一切看起来都很好,但是文件在具有运行子模块的主 Perl 代码模块的文件夹中被解压缩。
我希望在我指定的文件夹中生成存档。
这是我的代码:
sub ExtractFile
{
#Read the folder which was copied in the output path recursively and extract if any file is compressed
my $dirpath = $_[0];
opendir(D, "$dirpath") || die "Can't open dir $dirpath: $!\n";
my @list = readdir(D);
closedir(D);
foreach my $f (@list)
{
print " \$f = $f";
if(-f $dirpath."/$f")
{
#print " File in directory $dirpath \n ";#is \$f = $f\n";
my($file_name, $file_dirname,$filetype)= fileparse($f,qr{\..*});
#print " \nThe file extension is $filetype";
#print " \nThe file name is is $file_name";
# If compressed file then extract the file
if($filetype eq ".tar" or $filetype eq ".tzr.gz")
{
my $arch_file = $dirpath."/$f";
print "\n file to be extracted is $arch_file";
my $tar = Archive::Tar->new($arch_file);
#$tar->extract() or die ("Cannot extract file $arch_file");
#mkdir($dirpath."/$file_name");
$tar->extract_file($arch_file,$dirpath."/$file_name" ) or die ("Cannot extract file $arch_file");
}
}
if(-d $dirpath."/$f")
{
if($f eq "." or $f eq "..")
{ next; }
print " Directory\n";# is $f";
ExtractFile($dirpath."/$f");
}
}
}
递归调用 ExtractFile 方法来循环所有档案。 当使用 $tar->extract() 时,它会在调用此方法的文件夹中解压缩。
当我使用 $tar->extract_file($arch_file,$dirpath."/$file_name")
时出现错误:
存档中没有这样的文件:'/home/fsang/dante/workspace/output/s.tar'位于 /home/fsang/dante/lib/Extraction.pm 第 80 行
请帮忙 我已经检查了路径和输入输出没有与它有关的问题。
似乎有一些我不知道的 $tar->extract_file()
使用问题。
非常感谢任何解决这个问题的人。
问候, 萨克希
I am reading a directory having some archive files and uncompressing the archive files one by one.
Everything seems well however the files are getting uncompressed in the folder which has the main perl code module which is running the sub modules.
I want the archive to be generated in the folder I specify.
This is my code:
sub ExtractFile
{
#Read the folder which was copied in the output path recursively and extract if any file is compressed
my $dirpath = $_[0];
opendir(D, "$dirpath") || die "Can't open dir $dirpath: $!\n";
my @list = readdir(D);
closedir(D);
foreach my $f (@list)
{
print " \$f = $f";
if(-f $dirpath."/$f")
{
#print " File in directory $dirpath \n ";#is \$f = $f\n";
my($file_name, $file_dirname,$filetype)= fileparse($f,qr{\..*});
#print " \nThe file extension is $filetype";
#print " \nThe file name is is $file_name";
# If compressed file then extract the file
if($filetype eq ".tar" or $filetype eq ".tzr.gz")
{
my $arch_file = $dirpath."/$f";
print "\n file to be extracted is $arch_file";
my $tar = Archive::Tar->new($arch_file);
#$tar->extract() or die ("Cannot extract file $arch_file");
#mkdir($dirpath."/$file_name");
$tar->extract_file($arch_file,$dirpath."/$file_name" ) or die ("Cannot extract file $arch_file");
}
}
if(-d $dirpath."/$f")
{
if($f eq "." or $f eq "..")
{ next; }
print " Directory\n";# is $f";
ExtractFile($dirpath."/$f");
}
}
}
The method ExtractFile is called recursively to loop all the archives.
When using $tar->extract()
it uncompresses in the folder which calls this method.
When I use $tar->extract_file($arch_file,$dirpath."/$file_name")
I get an error :
No such file in archive: '/home/fsang/dante/workspace/output/s.tar' at /home/fsang/dante/lib/Extraction.pm line 80
Please help I have checked that path and input output there is no issue with it.
Seems some usage problem I am not aware of for $tar->extract_file()
.
Many thanks for anyone resolving this issue.
Regards,
Sakshi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
拼写错误?
可能应该是
a typo?
should probably be
我看到一把枪被带到瑞士军刀战斗中。
这是一个 *nix 单行代码,可以满足您的要求:
是否需要为此编写一个脚本?
除了调试行之外,您不必做任何特殊的事情。
I see a gun being brought to a Swiss Army knife fight.
Here's a *nix one-liner that does what you want:
Is there a need to write a script for this?
You aren't necessarily doing anything special other than debug lines.
您误解了
extract_file
。第一个参数是要提取的存档内的文件的名称。您正在传递存档本身的路径。您将其传递给new
;你不需要再次传递它。正如错误消息所解释的,s.tar
不包含名为/home/fsang/dante/workspace/output/s.tar
的文件,因此extract_file< /代码> 失败。
您可以使用
$tar->list_files
获取存档中的文件列表。一个更简单的解决方案可能是临时 chdir 到要将存档提取到的目录。 File::pushd 提供了一种简单的方法来做到这一点。
You've misunderstood
extract_file
. The first parameter is the name of a file inside the archive to extract. You're passing in the path of the archive itself. You passed that tonew
; you don't need to pass it in again. As the error message explains,s.tar
does not contain a file named/home/fsang/dante/workspace/output/s.tar
, soextract_file
fails.You can get a list of files in the archive by using
$tar->list_files
.A simpler solution may be to temporarily chdir to the directory you want to extract the archive into. File::pushd provides an easy way to do that.