Der*_*ler 0 php compression bash zlib
可能重复:
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";
?>
Run Code Online (Sandbox Code Playgroud)
这会输出我所期望的(解码数据的开头).
如果我将同一个文件传递给gunzip:
$ gunzip -c data
gzip: data: not in gzip format
Run Code Online (Sandbox Code Playgroud)
如果我尝试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.
Run Code Online (Sandbox Code Playgroud)
正如所建议的那样,鉴于没有其他实用的方法,我选择了相关问题中提出的解决方案.
但鉴于我想避免转向另一种脚本语言(并引入另一种依赖),我选择了所有这些语言:
_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
Run Code Online (Sandbox Code Playgroud)
这四个版本在我的测试用例中产生了完全相同的输出.
gzuncompress期望的格式是没有gzip标头的原始zlib格式.如果您使用zlib库的C API,这是默认情况下获得的格式.
您可以使用zpipe命令行实用程序解压缩此格式.该实用程序的源代码examples/zpipe.c位于zlib源代码分发中.但是默认情况下不会编译和安装此程序.没有广泛部署的命令行实用程序接受原始zlib格式.
如果你想用PHP 代替gzip格式(带有友好标题gzip和格式的那个gunzip)那么你需要使用gzencode和gzdecode而不是gzcompress和gzuncompress.
| 归档时间: |
|
| 查看次数: |
2623 次 |
| 最近记录: |