代码之家  ›  专栏  ›  技术社区  ›  yarek

PHP:如何检索.gz中包含的文件名?

  •  -1
  • yarek  · 技术社区  · 6 年前

    我的test.gz包含例如:image1.jpg。

    如何解压缩test.gz,使其提供image1.jpg?

    我试过那种方法:

    $file = gzopen($file_name, 'rb');
    $out_file = fopen($out_file_name, 'wb');
    

    但它假设您需要知道.gz文件中包含的文件名

    当做

    1 回复  |  直到 6 年前
        1
  •  0
  •   akshaypjoshi    6 年前

    你需要使用PHP PharData 提取并阅读其内容。

    提取 tar tar.gz :

        $phar = new PharData('myphar.tar');
        $phar->extractTo('/full/path');
    

    要列出所有文件,可以使用:

    archive = new PharData('/upload/directory/myphar.tar.gz');
    
    foreach (new RecursiveIteratorIterator($archive) as $file) {
        echo $file . "<br />";
    }