PHP compression function implementation: Difference between gzencode, gzdeflate and gzcompress

Source: Internet
Author: User
: This article mainly introduces the implementation of PHP compression functions: gzencode, gzdeflate, and gzcompress. For more information about PHP tutorials, see. • Gzencode uses ZLIB_ENCODING_GZIP encoding by default and gzip compression format. In fact, the defalte algorithm is used to compress data, and the file header and adler32 are added for verification.
• Gzdeflate uses the ZLIB_ENCODING_RAW encoding method by default, and uses the deflate data compression algorithm. In fact, it is compressed by LZ77 and then encoded by Hoffmann.
• Gzcompress; ZLIB_ENCODING_DEFLATE encoding is used by default, and zlib compression format is used. In fact, deflate is used to compress data, and zlib header and CRC verification are added.

The comparison of the three functions is essentially a comparison of three compression methods: deflate, zlib, and gzip.
From the performance perspective: deflate is better than gzip and zlib
From the perspective of the size after compression of text files by default, deflate is better than zlib and gzip.

Among the three algorithms, the authors of gzip and zlib are Jean-Loup Gailly and Mark Adler.
The two algorithms and the graphic format png use the deflate algorithm.
The deflate algorithm uses both the LZ77 algorithm and the Huffman Coding algorithm.
It was initially defined by Phil Katz for the second version of his PKZIP archive tool and later defined in RFC 1951 specifications.

The deflate algorithm's compression and decompression implementation process can be found on the zlib compression library.
PHP compression depends on zlib. zlib is a function library that provides the deflate, zlib, and gzip compression methods.
The above three functions are used to convert the encoding parameter to the same one, and the compression ratio is set to the same. then, they call the same function with the same effect and performance.

The zlib implementation of PHP is extended in the ext/zlib directory. Use the deflateInit2 () + deflate () + deflateEnd () functions to compress data. the inflateInit2 () + inflate () + inflateEnd () functions work together to decompress data. Compression is ultimately called through the php_zlib_encode function. apart from the input string, compression ratio, and result output, the encoding parameter is different for different function calls. The fourth parameter of deflateInit2 specifies encoding. PHP defines three constants:

The code is as follows:


# Define PHP_ZLIB_ENCODING_RAW-0xf // deflate-15
# Define PHP_ZLIB_ENCODING_GZIP 0x1f // gzip 15 + 16
# Define PHP_ZLIB_ENCODING_DEFLATE 0x0f // zlib 15

When calling the three functions, you can directly specify encoding to use other algorithms:

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 for better name presentation. The three functions can achieve the same effect by specifying the same encoding, and PHP also provides the zlib_encode function as a common compression function.

References:
Http://shouce.jb51.net/gzip/rfc-deflate.html
Http://www.gzip.org/zlib/rfc-deflate.html

The above describes the implementation of PHP compression functions: gzencode, gzdeflate, and gzcompress, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.