PHP 的“gzuncompress”外壳中的函数?

发布于 2025-01-03 14:23:03 字数 2275 浏览 5 评论 0原文

可能的重复:
Deflate 命令行工具

是的,我知道我可以在 shell 上使用 PHP 本身,但我需要此功能在 PHP 可用之前部署的脚本中。

我尝试过 gzipunzip 但要么我的参数不正确,要么它们只是不使用相同的压缩。

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2025-01-10 14:23:03

gzuncompress 期望的格式是不带 gzip 标头的原始 zlib 格式。如果您使用 zlib 库的 C API,这是默认获得的格式。

您可以使用 zpipe 命令行实用程序解压缩此格式。此实用程序的源代码位于 zlib 源代码中的 examples/zpipe.c 中分配。但这个程序默认是不编译安装的。没有广泛部署的命令行实用程序接受原始 zlib 格式。

如果你想使用 PHP 的 gzip 格式(带有 gzipgunzip 处理的友好标头的格式),那么你需要使用 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 in examples/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文