Home >Backend Development >PHP Tutorial >PHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress_php skills

PHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress_php skills

WBOY
WBOYOriginal
2016-05-16 19:59:091332browse

•gzencode uses ZLIB_ENCODING_GZIP encoding by default and uses gzip compression format. It actually uses the defalte algorithm to compress data, and then adds file headers and adler32 check
•gzdeflate uses the ZLIB_ENCODING_RAW encoding method by default and uses the deflate data compression algorithm. In fact, it is first compressed with LZ77 and then compressed with Huffman coding
•gzcompress; ZLIB_ENCODING_DEFLATE encoding is used by default, using zlib compression format. It actually uses deflate to compress the data, and then adds zlib header and CRC check

The comparison of these three functions is essentially a comparison of three compression methods: deflate, zlib, and gzip.
From a performance perspective: deflate is better than gzip and zlib
Judging from the dimension of the text file’s default compression rate after compression: deflate is better than zlib and is better than gzip

The authors of gzip and zlib among these three algorithms are Jean-Loup Gailly and Mark Adler.
These two algorithms, as well as the graphics format png, use the deflate algorithm.
The deflate algorithm is a lossless data compression algorithm that uses both the LZ77 algorithm and Huffman Coding.
It was originally defined by Phil Katz for version 2 of his PKZIP archiving tool and later in the RFC 1951 specification.

The implementation process of compression and decompression of the deflate algorithm can be found on the compression library zlib.
PHP's compression implementation relies on zlib, which is a function library that provides deflate, zlib, and gzip compression methods.
The above three functions we used, if the encoding in the parameters is converted to the same value and the compression rate is set to the same value, the same function will be called in the end, and the effect and performance will be the same.

PHP’s zlib implementation exists in the ext/zlib directory as an extension. The three functions of deflateInit2() deflate() deflateEnd() cooperate to complete the compression function, and the three functions of inflateInit2() inflate() inflateEnd() cooperate to complete the decompression function. Compression is ultimately called through the php_zlib_encode function. In addition to the input string, compression rate, and result output, the different entry function call parameters are their encoding. The fourth parameter of deflateInit2 specifies encoding. PHP defines three constants:

Copy code The code is as follows:

#define PHP_ZLIB_ENCODING_RAW    -0xf   //deflate -15
#define PHP_ZLIB_ENCODING_GZIP           0x1f                                                                                                 #define PHP_ZLIB_ENCODING_DEFLATE 0x0f // zlib 15

You can directly specify encoding to use other algorithms during the calling process of the three functions:

Copy code The code is as follows:
zlib: ZLIB_ENCODING_DEFLATE
gzip: ZLIB_ENCODING_GZIP
deflate: ZLIB_ENCODING_RAW

These three functions are simple calling methods of the three algorithms, presented with better names. The three functions can achieve the same effect by specifying the same encoding, and PHP also provides the zlib_encode function as a universal compression function.

Reference:
http://shouce.jb51.net/gzip/rfc-deflate.html
http://www.gzip.org/zlib/rfc-deflate.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn