Home >Backend Development >PHP Tutorial >PHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress_php skills
•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:
You can directly specify encoding to use other algorithms during the calling process of the three functions:
Reference:
http://shouce.jb51.net/gzip/rfc-deflate.html
http://www.gzip.org/zlib/rfc-deflate.html