PHP's compression function implementation: Gzencode, gzdeflate and gzcompress differences _php skills

Source: Internet
Author: User
Tags rfc

Gzencode uses the Zlib_encoding_gzip encoding by default, using the GZIP compression format, actually compressing the data using the Defalte algorithm, and then adding the file header and the Adler32 checksum
gzdeflate By default Zlib_encoding_raw encoding, using the DEFLATE data compression algorithm, is actually first compressed with the LZ77, and then the Hoffman code compression
gzcompress By default, using Zlib_encoding_deflate encoding, using zlib compression format, actually compressing the data with deflate and then adding zlib header and CRC checksum

The comparison of these three functions is essentially three compression methods: Deflate, zlib, gzip comparison.
From the dimension of performance: deflate better than gzip better than zlib
From the default compression rate of the text file compressed volume dimensions look: Deflate better than zlib better than gzip

Of these three algorithms, the authors of Gzip and zlib are Jean-loup gailly and Mark Adler.
Both algorithms and graphic format PNG, the compression algorithm used is deflate algorithm.
Deflate algorithm is a lossless data compression algorithm using LZ77 algorithm and Huffman Coding (Huffman coding).
It was originally defined by Phil Katz for his second edition of the PKZIP Archiving tool, which was later defined in the RFC 1951 specification.

The process of deflate algorithm compression and decompression can be found on the zlib of compressed library.
PHP's compression implementation relies on Zlib,zlib is a library of functions that provides a deflate, zlib, gzip compression method.
The above three functions we use to convert the encoding in the parameter to the same, with the same compression rate setting, the final call is the same function, the effect and performance.

The zlib implementation of PHP is in an extended way in the Ext/zlib directory. With the DeflateInit2 () + deflate () + deflateend () Three functions to complete the compression function, inflateInit2 () + inflate () + inflateend () Three functions to complete the decompression function. Compression is ultimately through the Php_zlib_encode function to achieve the call, in addition to the input string, compression rate, the output of the results, different entry function call parameters are different from its encoding. DeflateInit2 's fourth parameter specifies encoding,PHP defines three constants:

Copy Code code 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

Three functions can be directly specified encoding using other algorithms in the calling procedure:

Copy Code code as follows:

Zlib:zlib_encoding_deflate
Gzip:zlib_encoding_gzip
Deflate:zlib_encoding_raw

These three functions are a simple invocation of three algorithms to better name the presentation. Three functions can achieve the same effect by specifying the same encoding, and PHP also provides the Zlib_encode function as a generic compression function.

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

Related Article

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.