PHP compression functions Gzencode, gzdeflate and gzcompress use

Source: Internet
Author: User
Tags comparison strlen


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. The fourth parameter of DeflateInit2 specifies that the 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



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



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.



Comparison of Gzcompress, Gzencode and gzdeflate compression ratios



Below we use a program to compare the compression ratios of these three functions.


The code is as follows

$string = " 8aeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer8aaaaaaaaaaaaaaaaaaaaaaadasd456as 
  d456asd456asd456asd456asd456asd456asd456asd456asd456asd456asd456fasdf45645645645645645 
  6456456456456456456456456456456456456456456456456456456456456456456456456a56fs4s4s4s4s 
  4S4S4S4S4S4S4S4S4S4DSDGA133333333333333333333W8ETW7Q9999999999999999999A23S1DFFFFFFFFF 
  Fffffffffffffffa456ssssssssssssdv2sdddddddddddddddddddf "; 
 echo "string length:"; 
 echo strlen ($XX); 
 echo "<br/>gzcompress after compression length:"; 
 echo strlen ($string, 9)); 
 echo "<br/>gzencode after compression length:"; 
 echo strlen ($string, 9)); 
 echo "<br/>gzdeflate after compression length:"; 
 echo strlen ($string, 9));





The results of the operation are:
String Length: 388
Gzcompress length after compression: 80
Gzencode length after compression: 92
Gzdeflate length after compression: 74


Thus, these three functions are sorted according to the compression effect: Gzdeflate, Gzcompress, gzencode,gzdeflate the best compression effect, gzcompress second, gzencode the worst.



Analysis:
Check the next manual, Gzcompress, Gzencode, gzdeflate these three functions, respectively, using the zlib, gzip, deflate compression format. Through the results of the above operation, I do not know if you can think so, DEFLATE, zlib, gzip compression effect of diminishing


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.