PHP compression functions: gzencode, gzdeflate, and gzcompress

Source: Internet
Author: User
By default, gzencode uses ZLIB_ENCODING_GZIP encoding and gzip compression format. In fact, it uses the defalte algorithm to compress data, and adds the file header and adler32 to verify that gzdeflate uses ZLIB_ENCODING_RAW encoding by default, in fact, LZ77 is used for compression, and then gzc is compressed using the Hoffmann encoding.

By default, gzencode uses ZLIB_ENCODING_GZIP encoding and gzip compression format. In fact, it uses the defalte algorithm to compress data, and adds the file header and adler32 to verify that gzdeflate uses ZLIB_ENCODING_RAW encoding by default, in fact, LZ77 is used for compression, and then gzc is compressed using the Hoffmann encoding.

  • By default, gzencode uses ZLIB_ENCODING_GZIP encoding and gzip compression format. In fact, it uses the defalte algorithm to compress data and adds the file header and adler32 verification.
  • Gzdeflate uses the ZLIB_ENCODING_RAW encoding method by default, and uses the deflate data compression algorithm. In fact, it uses LZ77 to compress data, and then uses Hoffmann to compress the data.
  • 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 check 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:

     #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:

    zlib:   ZLIB_ENCODING_DEFLATE gzip: ZLIB_ENCODING_GZIPdeflate: 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://www.gzip.org/zlib/rfc-deflate.html

    Original article address: PHP compression function implementation: gzencode, gzdeflate, and gzcompress. Thank you for sharing it.

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.