【发布时间】:2016-02-09 19:46:30
【问题描述】:
我尝试使用Zlib解压文件,但它只是说“数据错误”并给了我一个空文件。
这是我试过的代码:
// Open a new temp file to write new file to
$tempFile = fopen("tempFile", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);
// Write new decompressed file
fwrite($tempFile, zlib_decode(file_get_contents($path))); // $path = absolute path to data.tar.Z
// close temp file
fclose($tempFile);
我也尝试将它部分解压缩,从 .tar.Z 到 .tar 再到一个文件。我尝试使用 lzw 函数来删除 .Z,但我无法使其工作。有没有办法做到这一点?
编辑:
这是我尝试过的更多代码。只是为了确保file_get_contents 正常工作。我仍然收到“数据错误”。
$tempFile = fopen("tempFile.tar", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);
// Write new decompressed file
$contents = file_get_contents($path);
if ($contents) {
fwrite($tempFile, gzuncompress($contents));
}
// close temp file
fclose($tempFile);
EDIT2:我认为 LZW 不起作用的原因是 .tar.Z 文件的内容如下所示:
��3dЀ��0p���a�
H�H��ŋ3j��@�6l�
我尝试过的 LZW 函数都在其字典中使用 ASCII 字符。这些是什么角色?
【问题讨论】:
-
你想用纯php来完成吗?你在linux服务器上运行吗?你有能力跑
exec或shell_exec吗? -
我不能使用
exec或shell_exec,我更喜欢使用纯PHP。 -
不要这样链接函数。检查
file_get_contents()是否真的能够读取该文件。如果不能,它会返回布尔值false,然后它会被盲目地传递给zlib_decode(),它当然不能解码false——字符串上下文中的布尔值false是一个空字符串。 -
随意移植压缩算法,或者做其他人都会做的事情,调用 uncompress / tar 类似 exec() / system() / proc_open() -- github.com/vapier/ncompress/blob/ncompress-4.2.4/compress42.c
-
您确定存档有效吗?您是否设法在命令行中解压缩它?
标签: php compression lzw