将 tar 文件归档到 Perl 中的不同位置

发布于 2024-08-28 16:28:56 字数 1765 浏览 3 评论 0原文

我正在读取一个包含一些存档文件的目录,并将存档文件一一解压缩。

一切看起来都很好,但是文件在具有运行子模块的主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

违心° 2024-09-04 16:28:58

拼写错误?

$tar->extract_file($arch_file,$dirpath."/$file_name" ) 

可能应该是

$tar->extract_file($arch_file,$dirpath."/".$file_name) 

a typo?

$tar->extract_file($arch_file,$dirpath."/$file_name" ) 

should probably be

$tar->extract_file($arch_file,$dirpath."/".$file_name) 
葮薆情 2024-09-04 16:28:58
$tarFile = "test.tar.gz";
$myTar = Archive::Tar->new($tarFile);
foreach my $member ($myTar->list_files())
{
    my $res = $myTar->extract_file( $member , 'C:/temp/'.$member );
    print "Exract error!\n" unless ($res);
}
$tarFile = "test.tar.gz";
$myTar = Archive::Tar->new($tarFile);
foreach my $member ($myTar->list_files())
{
    my $res = $myTar->extract_file( $member , 'C:/temp/'.$member );
    print "Exract error!\n" unless ($res);
}
〃安静 2024-09-04 16:28:58

我看到一把枪被带到瑞士军刀战斗中。
这是一个 *nix 单行代码,可以满足您的要求:

find /source/dir -name "*.tar" -exec tar -C /target/dir -xvzf '{}' \; -print

是否需要为此编写一个脚本?
除了调试行之外,您不必做任何特殊的事情。

I see a gun being brought to a Swiss Army knife fight.
Here's a *nix one-liner that does what you want:

find /source/dir -name "*.tar" -exec tar -C /target/dir -xvzf '{}' \; -print

Is there a need to write a script for this?
You aren't necessarily doing anything special other than debug lines.

黯然#的苍凉 2024-09-04 16:28:57

您误解了 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 to new; 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, so extract_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文