首页 > 解决方案 > 如何将所有嵌套的 tar.gz 和 zip 提取到 PHP 中的目录?

问题描述

我需要在PHP中提取一个tar.gz文件。该文件包含许多JSON文件、tar.gzzip文件和子目录。我只需要将JSON文件移动到目录./Dataset/processing并继续提取嵌套的 tar.gz 和 zip 以从那里获取所有 JSON 文件。这些文件也可能有嵌套的文件夹/目录。

结构如下:

origin.tar.gz
 ├───sub1.tar.gz
 │   ├───sub2.tar.gz
 │   ├───├───a.json
 │   ├───├───├───├───├───├───...(unknown depth)
 │   ├───b.json
 │   ├───c.json
 ├───sub3.zip
 │   ├───sub4.tar.gz
 │   ├───├───d.json
 │   ├───├───├───├───├───├───...(unknown depth)
 │   ├───e.json
 │   ├───f.json
 ├───subdirectory
 │   ├───g.json
 ├───h.json
 ├───i.json
 |   ..........
 |   ..........
 |   ..........
 |   many of them

提取后 ./Dataset将如下所示

Dataset/processing
 ├───a.json
 ├───b.json
 ├───c.json
 ├───d.json
 ├───e.json
 ├───f.json
 ├───g.json
 ├───h.json
 ├───i.json
 |   ..........
 |   ..........
 |   ..........
 |   many of them

我知道如何在 PHP 中使用 PharData 提取 tar.gz,但它仅适用于单级深度。我在想,如果某种递归可以使这项工作适用于多层次深度。

$phar = new PharData('origin.tar.gz');
$phar->extractTo('/full/path'); // extract all files in the tar.gz

我已经稍微改进了我的代码并尝试了这个,它适用于多深度但当有一个目录(文件夹或嵌套文件夹)也包含 JSON 时会失败。有人也可以帮我提取它们。

<?php

$path = './';

// Extraction of compressed file
function fun($path) {    
    $array = scandir($path); 
    for ($i = 0; $i < count($array); $i++) {
        if($i == 0 OR $i == 1){continue;}
        else {
            $item = $array[$i];
            $fileExt = explode('.', $item);

            // Getting the extension of the file
            $fileActualExt = strtolower(end($fileExt));
            if(($fileActualExt == 'gz') or ($fileActualExt == 'zip')){
                $pathnew = $path.$item; // Dataset ./data1.tar.gz
                $phar = new PharData($pathnew);
                // Moving the files
                $phar->extractTo($path);
                // Del the files
                unlink($pathnew);
                $i=0;
            }
        }
        $array = scandir($path);


    }
}
fun($path);

// Move only the json to ./dataset(I will add it later)
?>

提前致谢。

标签: phpjsonziptardata-extraction

解决方案


在第一步,像你提到的那样提取你的 tar.gz 文件:

$phar = new PharData('origin.tar.gz');
$phar->extractTo('/full/path'); // extract all files in the tar.gz

然后递归读取目录,将所有 json 类型的文件移动到目标目录,这是我的带注释的代码:

$dirPath='./';       // the root path of your very first extraction of your tar.gz

recursion_readdir($dirPath,1);


function recursion_readdir($dirPath,$Deep=0){
    $resDir=opendir($dirPath);
    while($basename=readdir($resDir)){
        //current file path
        $path=$dirPath.'/'.$basename;
        if(is_dir($path) AND $basename!='.' AND $basename!='..'){
            //it is directory, then go deeper
            $Deep++;//depth+1
            recursion_readdir($path,$Deep);
        }else if(basename($path)!='.' AND basename($path)!='..'){
            //it is not directory,
            //when the file is json file
                if(strstr($basename,'json')) {
                        //copy the file to your destination path
                    copy($path, './dest/' . $basename);

            } else if(strstr($basename,'tar')){
                //when the file is tar.gz file, extract this tar.gz file
                $phar = new PharData($basename);
                $phar->extractTo($dirPath, null, true);
            }
        }

    }
    closedir($resDir);
}
function forChar($char='-',$times=0){
  $result='';
  for($i=0;$i<$times;$i++){
     $result.=$char;
  }
  return $result;
}

推荐阅读