PHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress, gzencodegzdeflate_PHP tutorial

WBOY
Release: 2016-07-12 08:59:56
Original
828 people have browsed it

PHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress, gzencodegzdeflate

•gzencode uses ZLIB_ENCODING_GZIP encoding by default, uses gzip compression format, and actually uses the defalte algorithm to compress data , then add the file header 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

Articles you may be interested in:

    Detailed examples of php ZipArchive compression function

http://www.bkjia.com/PHPjc/1096141.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1096141.htmlTechArticlePHP compression function implementation: the difference between gzencode, gzdeflate and gzcompress, gzencodegzdeflate gzencode uses ZLIB_ENCODING_GZIP encoding by default and uses gzip compression Format, actually use...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template