PHP compression function Implementation: The difference between Gzencode, gzdeflate and Gzcompress

Source: Internet
Author: User
Tags rfc
Gzencode uses Zlib_encoding_gzip encoding by default, using the gzip compression format, in effect compressing the data using the Defalte algorithm, and then adding the header and Adler32 checksum
Gzdeflate uses the Zlib_encoding_raw encoding method by default, using the DEFLATE data compression algorithm, which is actually first compressed with LZ77 and then encoded with Hoffman
gzcompress, using zlib_encoding_deflate encoding by default, uses the zlib compression format, actually compresses the data with deflate, and then adds zlib header and CRC checksum

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

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

The process of compressing and extracting the deflate algorithm can be found on the compression library zlib.
PHP's compression implementation relies on Zlib,zlib as a library of functions that provide the deflate, zlib, and gzip compression methods.
We use the above three functions, the parameters in the encoding to the same, the compression rate setting is the same, the final call is the same function, the same effect and performance.

The zlib implementation of PHP exists in the Ext/zlib directory in an extended manner. Through the DeflateInit2 () + deflate () + deflateend () Three functions with the completion of the compression function, inflateInit2 () + inflate () + inflateend () three functions together to complete the decompression function. Compression is ultimately achieved through the Php_zlib_encode function call, in addition to the input string, compression ratio, the output of the results, different entry function call parameters are different is its encoding. The fourth parameter of DeflateInit2 specifies that the encoding,php defines three constants:

Copy the 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 in the calling procedure can be directly specified encoding using other algorithms:

Copy the Code code as follows:


Zlib:zlib_encoding_deflate
Gzip:zlib_encoding_gzip
Deflate:zlib_encoding_raw

These three functions are 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 general-purpose compression function.

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

The above describes the PHP compression function implementation: Gzencode, gzdeflate and gzcompress differences, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.