在 PHP 中使用哪种压缩方法?

我使用两个 PHP 脚本移动大量数据: 一个在客户端使用命令行 PHP 脚本,另一个在 Apache 后面。我将数据发布到服务器端,并使用 php://input stream 将数据保存到 Web 服务器端。为了防止达到任何内存限制,对于每个 POST 请求,数据被分成500kB 的块。这一切都很好。

现在,为了节省带宽和提高速度,我想在发送前压缩数据,在另一端接收时解压缩。我找到了3对可以完成这项工作的函数,但我无法决定使用哪一对:

  • gzencode /< a href = “ http://php.net/Manual/en/function. gzdecde.php”rel = “ noReferrer”> gzdecode
  • gzdeflate /< a href = “ http://php.net/Manual/en/function. gzflate.php”rel = “ noReferrer”> gzinflate
  • gzcompress /< a href = “ http://php.net/Manual/en/function. gzunpress.php”rel = “ noReferrer”> gzuncompress

您会推荐哪一对函数? 为什么?

更新: 我刚刚读了 zlib 常见问题解答:

Gzip 格式(gzencode)旨在保留关于单个文件的目录信息,比如名称和最后修改日期。另一方面,zlib 格式(gzcompress)是为内存和通信通道应用程序设计的,它有一个更紧凑的头部和尾部,并使用比 gzip 更快的完整性检查。

32692 次浏览

All of these can be used. There are subtle differences between the three:

  • gzencode() uses the GZIP file format, the same as the gzip command line tool. This file format has a header containing optional metadata, DEFLATE compressed data, and footer containing a CRC32 checksum and length check.
  • gzcompress() uses the ZLIB format. It has a shorter header serving only to identify the compression format, DEFLATE compressed data, and a footer containing an ADLER32 checksum.
  • gzdeflate() uses the raw DEFLATE algorithm on its own, which is the basis for both of the other formats.

All three use the same algorithm under the hood, so they won't differ in speed or efficiency. gzencode() adds the ability to include the original file name and other environmental data (this is unused when you are just compressing a string). gzencode() and gzcompress() both add a checksum, so the integrity of the archive can be verified, which can be useful over unreliable transmission and storage methods. If everything is stored locally and you don't need any additional metadata then gzdeflate() would suffice. For portability I'd recommend gzencode() (GZIP format) which is probably better supported than gzcompress() (ZLIB format) among other tools.

When compressing very short strings the overhead of each method becomes relevant since for very short input the overhead can comprise a significant part of the output. The overhead for each method, measured by compressing an empty string, is:

  • gzencode('') = 20 bytes
  • gzcompress('') = 8 bytes
  • gzdeflate('') = 2 bytes

All methods are essentially the same, the difference between them is mostly in the headers. personally I'd use gzencode, this will produce output which is equal to a commandline invocation to the gzip utility.

I am no PHP expert and cannot answer the question posed, but it seems like there is a lot of guessing going on here, and fuzzy information being proffered.

DEFLATE is the name of the compression algorithm that is used by ZLIB, GZIP and others. In theory, GZIP supports alternative compression algorithms, but in practice, there are none.

There is no such thing as "the GZIP algorithm". GZIP uses the DEFLATE algorithm, and puts framing data around the compressed data. With GZIP you can add things like the filename, the time of the file, a CRC, even a comment. This metadata is optional, though, and many gzippers just omit it.

ZLIB is similar, except with a different, more limited set of metadata, and a specific 2-byte header.

This is all in IETF RFCs 1950, 1951, and 1952.

To say that "the gzip algorithm compresses better than DEFLATE" is just nonsense. There is no gzip algorithm. And the algorithm used in the GZIP format is DEFLATE.