Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在shell中的'gzuncompress'函数?_Php_Bash_Compression_Zlib - Fatal编程技术网

PHP在shell中的'gzuncompress'函数?

PHP在shell中的'gzuncompress'函数?,php,bash,compression,zlib,Php,Bash,Compression,Zlib,可能重复: 是的,我知道我可以在shell上使用PHP本身,但我需要在PHP可用之前部署的脚本中使用此功能 我尝试过gzip和unzip,但要么我的参数不正确,要么就是它们没有使用相同的压缩 我想在bash脚本中使用它。进入更高级的脚本语言不是一种选择 出于测试目的,我编写了以下PHP脚本: #!/usr/bin/php <? $contents = file_get_contents( $argv[1] ); $data = gzuncompress( $contents );

可能重复:

是的,我知道我可以在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";
?>
如果我尝试解压缩:

正如所建议的,并且考虑到没有其他可行的方法,我选择了本文中提出的解决方案。 但考虑到我想避免使用另一种脚本语言和引入另一种依赖性,我选择了所有这些语言:

_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个版本产生了与我的测试用例完全相同的输出。

gzuncompress期望的格式是没有gzip头的原始zlib格式。如果使用zlib库的C API,这是默认情况下获得的格式

可以使用zpipe命令行实用程序解压缩此格式。此实用程序的源代码可以在源代码发行版的examples/zpipe.c中找到。但默认情况下不会编译和安装此程序。没有广泛部署的接受原始zlib格式的命令行实用程序


如果您想使用gzip格式,而不是gzip和gunzip从PHP处理的带有友好标题的格式,那么您需要使用&而不是&。

如何使用gzip?试试gzip-c/etc/motd | gunzip-c | diff/etc/motd——讽刺的是,gzip和zip使用完全相同的算法。它们恰好比原始数据多。可悲的是,我不控制压缩数据。恐怕,即使你说这不是一个选项,你也必须考虑捆绑像ZBube这样的实用程序,或者使用一种脚本语言来进行解压缩。by链接的问题建议使用Python、Ruby和Perl的方法。此外,您还知道如何在PHP中实现这一点。
$ 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.
_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