【问题标题】:PHP: how to retrieve the filename of the file contained in .gz?PHP:如何检索 .gz 中包含的文件的文件名?
【发布时间】:2018-08-28 12:35:25
【问题描述】:

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

我如何解压缩 test.gz 以便它给我 image1.jpg ?

我试过那个方法:

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

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

问候

【问题讨论】:

  • GZip 通常不保存原始文件名(或任何元数据),.gz 只是一个扩展名,表明该文件已使用 GZip 压缩。如果您想将文件名与压缩数据一起保存,您将不得不使用额外的层,例如 tar 或提出您自己的数据格式。

标签: php gzip


【解决方案1】:

您需要使用 PHP PharData 来提取和读取其内容。

提取tartar.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 />";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2020-09-29
    相关资源
    最近更新 更多