问题描述
我有两个PHP脚本移动大量的数据:一个在客户端使用命令行PHP脚本和其他背后的Apache。我将数据POST到服务器端,并使用php://输入流将其保存在Web服务器端。为了防止达到任何内存限制,每个POST请求将数据分成500kB的块。所有这些工作正常。
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.
现在,为了节省带宽并加快速度,我想在发送和解压之前压缩数据,而在另一端接收。我发现3对函数可以做这个工作,但我不能决定使用哪一个:
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:
- /
- / li>
- /
gzencode
/gzdecode
gzdeflate
/gzinflate
gzcompress
/gzuncompress
您会建议哪一组功能?为什么?
Which pair of functions would you recommend and why?
UPDATE:我只是阅读zlib常见问题:
UPDATE: I just read zlib FAQ:
gzip格式( gzencode
)旨在保留有关单个文件的目录信息,例如名称和上次修改日期。另一方面,zlib格式(
gzcompress
)是为内存和通信通道应用程序设计的,并且具有更加紧凑的头部和尾部,并使用更快的完整性检查gzip。
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.
推荐答案
所有这些都可以使用。三者之间有细微的差别:
All of these can be used. There are subtle differences between the three:
- gzencode()使用GZIP文件格式,
gzip
命令行工具。此文件格式的标题包含可选元数据,DEFLATE压缩数据,以及包含CRC32校验和和长度检查的页脚。 - gzcompress()使用ZLIB格式。它有一个较短的头,仅用于标识压缩格式,DEFLATE压缩数据和包含ADLER32校验和的页脚。
- gzdeflate()使用原始DEFLATE算法,它是其他格式的基础。
- 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 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()
添加了包括原始文件名和其他环境数据的能力(这只是在压缩字符串时不使用)。 gzencode()
和 gzcompress()
都添加一个校验和,因此可以验证归档的完整性,对不可靠的传输和存储方法有用。如果一切都存储在本地,并且不需要任何额外的元数据,那么 gzdeflate()
就足够了。对于可移植性,我推荐 gzcompress()
(ZLIB)支持的 gzencode()
格式)等工具。
All three use the same algorithm under the hood. gzencode()
adds the ability to include the original file name and other environmental data (this is unused when 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.
这篇关于在PHP中使用哪种压缩方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!