我想用 PHP 压缩一个 .txt
文件,同时保持文件扩展名。当我解压缩 .gz
文件时,.gz
文件中的扩展名 (.txt
) 被删除。 (test.txt
变成 -> test
)
下面是我的 PHP 代码示例,用于将 test.txt
压缩到 .gz
文件中:
<?php
// Name of the file we are compressing
$file = "test.txt";
// Name of the gz file we are creating
$gzfile = "test.gz";
// Open the gz file (w9 is the highest compression)
$fp = gzopen ($gzfile, 'w9');
// Compress the file
gzwrite ($fp, file_get_contents($file));
// Close the gz file and we are done
gzclose($fp);
?>
有人知道我做错了什么吗?或者这是因为 gzip
限制?
最佳答案
通常 gzip 在解压缩时不会使用 gzip header 中的名称,而且您也没有在 header 中存储名称。它只是从文件名中删除 .gz
。您需要将文件命名为 test.txt.gz
才能将其解压缩为 test.txt
。
关于PHP - 在保持文件扩展名的同时压缩 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16649957/