在 PHP 中使用哪种压缩方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/621976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which compression method to use in PHP?
提问by Milan Babu?kov
I have a large amount of data to move using two PHP scripts: one on the client side using a command line PHP script and other behind Apache. I POST the data to the server side and use php://input stream to save it on the web-server end. To prevent from reaching any memory limits, data is separated into 500kB chunks for each POST request. All this works fine.
我有大量数据要使用两个 PHP 脚本移动:一个在客户端使用命令行 PHP 脚本,另一个在 Apache 后面。我将数据发布到服务器端并使用 php://input 流将其保存在网络服务器端。为了防止达到任何内存限制,每个 POST 请求的数据被分成 500kB 块。所有这些工作正常。
Now, to save the bandwidth and speed it up, I want to compress the data before sending and decompress when received on the other end. I found 3 pairs of functions that can do the job, but I cannot decide which one to use:
现在,为了节省带宽并加快速度,我想在发送之前压缩数据,并在另一端接收时解压缩。我找到了 3 对可以完成这项工作的函数,但我无法决定使用哪一个:
Which pair of functions would you recommend and why?
你会推荐哪一对函数,为什么?
UPDATE:I just read zlib FAQ:
更新:我刚刚阅读了 zlib 常见问题解答:
The gzip format (gzencode) was designed to retain the directory information about a single file, such as the name and last modification date. The zlib format (gzcompress) on the other hand was designed for in-memory and communication channel applications, and has a much more compact header and trailer and uses a faster integrity check than gzip.
gzip 格式 ( gzencode) 旨在保留有关单个文件的目录信息,例如名称和上次修改日期。gzcompress另一方面,zlib 格式 ( ) 是为内存和通信通道应用程序设计的,具有更紧凑的标头和尾标,并且使用比 gzip 更快的完整性检查。
回答by thomasrutter
All of these can be used. There are subtle differences between the three:
所有这些都可以使用。三者之间有细微的差别:
- gzencode()uses the GZIP file format, the same as the
gzipcommand 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.
- gzencode()使用 GZIP 文件格式,与
gzip命令行工具相同。这种文件格式的头部包含可选的元数据、DEFLATE 压缩数据,以及包含 CRC32 校验和和长度校验的页脚。 - gzcompress()使用 ZLIB 格式。它有一个较短的标头,仅用于识别压缩格式、DEFLATE 压缩数据和一个包含 ADLER32 校验和的页脚。
- gzdeflate() 单独使用原始 DEFLATE 算法,这是其他两种格式的基础。
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.
所有这三个引擎都使用相同的算法,因此它们在速度或效率上不会有所不同。 gzencode()添加了包含原始文件名和其他环境数据的功能(仅在压缩字符串时未使用)。 gzencode()并且gzcompress()两者都添加了校验和,因此可以验证存档的完整性,这对于不可靠的传输和存储方法非常有用。如果所有内容都存储在本地并且您不需要任何额外的元数据,那么gzdeflate()就足够了。为了可移植性,我建议gzencode()(GZIPgzcompress()格式)在其他工具中可能比(ZLIB 格式)得到更好的支持。
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 bytesgzcompress('')= 8 bytesgzdeflate('')= 2 bytes
gzencode('')= 20 字节gzcompress('')= 8 字节gzdeflate('')= 2 字节
回答by Cheeso
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.
我不是 PHP 专家,无法回答提出的问题,但似乎这里有很多猜测,并且提供了模糊的信息。
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.
DEFLATE 是 ZLIB、GZIP 等使用的压缩算法的名称。理论上,GZIP 支持替代压缩算法,但实际上没有。
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.
没有“GZIP 算法”这样的东西。GZIP 使用 DEFLATE 算法,并在压缩数据周围放置帧数据。使用 GZIP,您可以添加诸如文件名、文件时间、CRC 甚至注释之类的内容。然而,这个元数据是可选的,许多 gzippers 只是省略了它。
ZLIB is similar, except with a different, more limited set of metadata, and a specific 2-byte header.
ZLIB 与 ZLIB 类似,除了不同的、更有限的元数据集和特定的 2 字节标头。
This is all in IETF RFCs 1950, 1951, and 1952.
这是所有的IETF RFC 1950年,1951年和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.
说“gzip 算法比 DEFLATE 压缩得更好”纯属无稽之谈。没有 gzip 算法。GZIP 格式中使用的算法是 DEFLATE。
回答by Jan Jungnickel
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.
所有方法本质上是相同的,它们之间的区别主要在于标题。我个人会使用 gzencode,这将产生等于对 gzip 实用程序的命令行调用的输出。
回答by AKJ
I had to decompress a gzip compressed file in PHP with C++.
I found out that the PHP gzencodeand its counterpart gzdecodeuse the Z_NO_FLUSHmethod and at the end of the block of the data to encode/decode, applies Z_FINISH.
The example/tutorial in C shown on the zlib website can be used to decompress and compress gziped files from PHP, as long as the windows bit and the memory level is changed to allow gzip deflations and inflations.
我不得不用 C++ 解压 PHP 中的 gzip 压缩文件。我发现 PHPgzencode及其对应物gzdecode使用该Z_NO_FLUSH方法,并且在要编码/解码的数据块的末尾,应用Z_FINISH. zlib 网站上显示的 C 语言示例/教程可用于从 PHP 解压和压缩 gzip 文件,只要更改 windows 位和内存级别以允许 gzip 压缩和膨胀。
Extra: It seems most people in this thread have no idea what "Compression method" meant.
额外:似乎该线程中的大多数人都不知道“压缩方法”是什么意思。

