This article mainly introduces the gzcompress, gzdeflate, and gzencode functions in PHP. This article analyzes their similarities and differences in depth, for more information, see a set of compression and decompression functions in PHP:
Compression function: gzcompress gzdeflate gzencode
Unzipping function: gzuncompress gzinflate gzdecode
Gzdecode is added after PHP 5.4.0. pay attention to compatibility issues when using gzdecode.
These functions all start with gz and let people think of gzip compression, while the differences between them are not visible only when you look at the function name. you can only query the document.
The difference between gzcompress gzdeflate gzencode functions is that they compress data in different formats:
Gzcompress uses the ZLIB format;
Gzdeflate uses pure DEFLATE format;
Gzencode uses the GZIP format;
However, the DEFLATE compression algorithm is used to compress data. (in theory, other compression algorithms can be used in ZLIB and GZIP formats, but only the DEFLATE algorithm is used in practice ), ZLIB and GZIP only add some headers and tails on the basis of DEFLATE.
By the way, Content-Encoding: deflate in HTTP uses the ZLIB format instead of the pure DEFLATE format.
Starting from PHP 5.4.0, the gzcompress and gzdeflate functions add the third parameter $ encoding, which can be three constants:
ZLIB_ENCODING_RAW corresponds to the pure DEFLATE format;
ZLIB_ENCODING_GZIP corresponds to the GZIP format;
ZLIB_ENCODING_DEFLATE corresponds to ZLIB format (note that it is not in pure DEFLATE format );
Although not mentioned in this document, these three constants can also be used in the third parameter $ encoding_mode of the gzencode function.
In fact, from PHP 5.4.0, the three functions are the same, but the default values of the third parameter are different. if the third parameter is input during the call, the data returned by the three functions is the same. You can write a simple script for testing:
The code is as follows:
<? Php
$ Url = 'http: // jb51.net ';
$ S1 = gzdeflate ($ url, 1 );
$ S2 = gzencode ($ url, 1, ZLIB_ENCODING_RAW );
If (strcmp ($ s1, $ s2) = 0) echo 'the same ';
?>
Run $ s1 and $ s2. why? You can find the answer from the PHP source code, open the php-5.5.4 \ ext \ zip \ zlib. c, you can find such code:
The code is as follows:
# Define PHP_ZLIB_ENCODE_FUNC (name, default_encoding )\
Static PHP_FUNCTION (name )\
{\
Char * in_buf, * out_buf ;\
Int in_len ;\
Size_t out_len ;\
Long level =-1 ;\
Long encoding = default_encoding ;\
If (default_encoding ){\
If (SUCCESS! = Zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s | ll", & in_buf, & in_len, & level, & encoding )){\
Return ;\
}\
} Else {\
If (SUCCESS! = Zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "sl | l", & in_buf, & in_len, & encoding, & level )){\
Return ;\
}\
}\
If (level <-1 | level> 9 ){\
Php_error_docref (NULL TSRMLS_CC, E_WARNING, "compression level (% ld) must be within-1 .. 9", level );\
RETURN_FALSE ;\
}\
Switch (encoding ){\
Case PHP_ZLIB_ENCODING_RAW :\
Case PHP_ZLIB_ENCODING_GZIP :\
Case PHP_ZLIB_ENCODING_DEFLATE :\
Break ;\
Default :\
Php_error_docref (NULL TSRMLS_CC, E_WARNING, "encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE ");\
RETURN_FALSE ;\
}\
If (SUCCESS! = Php_zlib_encode (in_buf, in_len, & out_buf, & out_len, encoding, level TSRMLS_CC )){\
RETURN_FALSE ;\
}\
RETURN_STRINGL (out_buf, out_len, 0 );\
}
/* NOTE: The naming of these userland functions was quite unlucky */
/* {Proto binary gzdeflate (binary data [, int level =-1 [, int encoding = ZLIB_ENCODING_RAW])
Encode data with the raw deflate encoding */
PHP_ZLIB_ENCODE_FUNC (gzdeflate, PHP_ZLIB_ENCODING_RAW );
/*}}}*/
/* {Proto binary gzencode (binary data [, int level =-1 [, int encoding = ZLIB_ENCODING_GZIP])
Encode data with the gzip encoding */
PHP_ZLIB_ENCODE_FUNC (gzencode, PHP_ZLIB_ENCODING_GZIP );
/*}}}*/
/* {Proto binary gzcompress (binary data [, int level =-1 [, int encoding = ZLIB_ENCODING_DEFLATE])
Encode data with the zlib encoding */
PHP_ZLIB_ENCODE_FUNC (gzcompress, PHP_ZLIB_ENCODING_DEFLATE );
/*}}}*/
As you can see, the three gzdeflate gzencode gzcompress functions are defined using the same PHP_ZLIB_ENCODE_FUNC macro (is there some generics ?), So of course they are the same.
The comments in the code also admit that the names of these functions are not good. it is unknown why such names are used.