PHP 的“gzuncompress”外壳中的函数?
可能的重复:
Deflate 命令行工具
是的,我知道我可以在 shell 上使用 PHP 本身,但我需要此功能在 PHP 可用之前部署的脚本中。
我尝试过 gzip
和 unzip
但要么我的参数不正确,要么它们只是不使用相同的压缩。
我想在 bash 脚本中使用它。进入更高级别的脚本语言不是一个选择。
我出于测试目的编写了以下 PHP 脚本:
#!/usr/bin/php
<?
$contents = file_get_contents( $argv[1] );
$data = gzuncompress( $contents );
echo substr( $data, 0, 20 ) . "\n";
?>
这输出了我所期望的内容(解码数据的开头)。
如果我将相同的文件传递给 gunzip
:
$ gunzip -c data
gzip: data: not in gzip format
如果我尝试 unzip
:
$ unzip data
Archive: data
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of data2 or
data2.zip, and cannot find data2.ZIP, period.
按照建议,并且考虑到没有其他实用方法,我采用了 < href="https://stackoverflow.com/questions/3178566/deflate-command-line-tool">相关问题。
但考虑到我想避免转向另一种脚本语言(并引入另一种依赖项),我选择了所有这些语言:
_uncompressedData=
if hash perl 2>&-; then
echo "Using Perl for decompression..." >&2
_uncompressedData=$(perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < compressed.bin || true)
elif hash ruby 2>&-; then
echo "Using Ruby for decompression..." >&2
_uncompressedData=$(ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' < compressed.bin || true)
elif hash php 2>&-; then
echo "Using PHP for decompression..." >&2
_uncompressedData=$(php -r "echo gzuncompress(file_get_contents('php://stdin'));" < compressed.bin || true)
elif hash python 2>&-; then
echo "Using Python for decompression..." >&2
_uncompressedData=$(python -c "import zlib,sys;print zlib.decompress(sys.stdin.read())" < compressed.bin || true)
else
echo "Unable to find decompressor!" >&2
exit 1
fi
这 4 个版本与我的测试用例产生了完全相同的输出。
Possible Duplicate:
Deflate command line tool
Yes, I know I can use PHP itself on the shell, but I need this functionality in a script that is deployed before PHP is available.
I've tried gzip
and unzip
but either my parameters are incorrect or they plain just don't use the same compression.
I want to use this in a bash script. Going into a higher level scripting language isn't an option.
I wrote the following PHP script for testing purposes:
#!/usr/bin/php
<?
$contents = file_get_contents( $argv[1] );
$data = gzuncompress( $contents );
echo substr( $data, 0, 20 ) . "\n";
?>
This outputs what I expect (the beginning of the decoded data).
If I pass the same file to gunzip
:
$ gunzip -c data
gzip: data: not in gzip format
If I try unzip
:
$ unzip data
Archive: data
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of data2 or
data2.zip, and cannot find data2.ZIP, period.
As suggested, and given that there is no other practical way, I went with the solution presented in the related question.
But given that I wanted to avoid moving to another scripting language (and introducing another dependency) I went with all of them:
_uncompressedData=
if hash perl 2>&-; then
echo "Using Perl for decompression..." >&2
_uncompressedData=$(perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < compressed.bin || true)
elif hash ruby 2>&-; then
echo "Using Ruby for decompression..." >&2
_uncompressedData=$(ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' < compressed.bin || true)
elif hash php 2>&-; then
echo "Using PHP for decompression..." >&2
_uncompressedData=$(php -r "echo gzuncompress(file_get_contents('php://stdin'));" < compressed.bin || true)
elif hash python 2>&-; then
echo "Using Python for decompression..." >&2
_uncompressedData=$(python -c "import zlib,sys;print zlib.decompress(sys.stdin.read())" < compressed.bin || true)
else
echo "Unable to find decompressor!" >&2
exit 1
fi
These 4 versions produced the exact same output with my test cases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gzuncompress
期望的格式是不带 gzip 标头的原始 zlib 格式。如果您使用 zlib 库的 C API,这是默认获得的格式。您可以使用
zpipe
命令行实用程序解压缩此格式。此实用程序的源代码位于 zlib 源代码中的examples/zpipe.c
中分配。但这个程序默认是不编译安装的。没有广泛部署的命令行实用程序接受原始 zlib 格式。如果你想使用 PHP 的 gzip 格式(带有
gzip
和gunzip
处理的友好标头的格式),那么你需要使用 gzencode & gzdecode 而不是 gzcompress & gzuncompress。The format that
gzuncompress
expects is the raw zlib format without the gzip header. This is the format you get by default if you use the zlib library's C API.You can uncompress this format using the
zpipe
command-line utility. The source code for this utility is found inexamples/zpipe.c
in the zlib source code distribution. But this program is not compiled and installed by default. There are no widely deployed command-line utilities that accept the raw zlib format.If you want to use the gzip format instead (the one with the friendly header which
gzip
&gunzip
deal with) from PHP then you need to use gzencode & gzdecode instead of gzcompress & gzuncompress.