【问题标题】:PHP to extract "some-dir/file.ext" from TAR to "another-dir/file.ext"PHP 将“some-dir/file.ext”从 TAR 提取到“another-dir/file.ext”
【发布时间】:2018-07-23 20:29:56
【问题描述】:

我使用由第 3 方每月创建的 .tar.gz。我可以解压到 .tar;但是当我从 tar 中提取文件时,我无法覆盖 tar 目录名称。

tar 有一个文件夹“dirname_latestDate”,其中包含许多文件。我希望通过指定要提取的单个文件,然后忽略 tar 的“文件夹”(测试脚本从 stackoverflow 和其他地方的代码中拼凑而成)。

$archive = new PharData($theTar);
// error cheecking excluded    
foreach($archive as $entry) {
  $extractDir = basename($file) . '/';
  if($file->isDir()) {
    $dir = new PharData($file->getPathname());          
    foreach($dir as $child) {
      $extract_file = $extractDir . basename($child);
      $archive->extractTo('/mypath/my-dir', $extract_file, true);
    }
  }
}

但这仍然会导致文件被放置在以 tar 文件夹命名的子目录中,例如/mypath/my-dir/unwanted-tar-dirname/file.ext.

我可以从子目录复制文件然后删除,但这似乎效率低下且不必要。有没有办法覆盖 un-tar 上的 Tar 文件夹名称? tar 包含一个大约 4MB(未压缩)的文件和一些其他小文件。

【问题讨论】:

    标签: php tar phar


    【解决方案1】:

    不,您不能直接使用extractTo 更改输出文件名。

    但是,您可以将file_put_contents$phar['filepath]->getContent() 结合使用来手动提取文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2015-01-05
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多