PHP - 在保持文件扩展名的同时压缩 txt 文件

标签 php compression gzip

我想用 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/

相关文章:

php - 如何使用 multipart/form-data 编码发布数组?

PHP:如何在不损失可见质量的情况下(自动)压缩图像?

3d - 哪些语言或方法允许图形和音乐演示适合64kb EXE?

linux - 如何使用部分文件名查找在目录中最新创建的 .gz 文件的文件名

php - 提供压缩的 JavaScript 文件

php - 无法弄清楚 foreach 循环

php - 如何在php中修改上传文件的内容类型?

java - 7Zip 无法解压缩由 Oracle 11g DB 中的 JAVA 源创建的文件

android - Cordova 是否支持压缩响应?

php - 移动网站后数据库错误?