当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP中的gzcompress、gzdeflate、gzencode函数详解

PHP中的gzcompress、gzdeflate、gzencode函数详解

2019年03月20日  | 移动技术网IT编程  | 我要评论

php中存在一组看起来很像的压缩解压函数:

压缩函数:gzcompress gzdeflate gzencode

解压函数:gzuncompress gzinflate gzdecode

gzdecode是php 5.4.0之后才加入的,使用的时候要注意兼容性问题。

这几个函数都以gz开头,让人想到gzip压缩,而光看函数名却又看不出它们之间的区别,只能查文档。

gzcompress gzdeflate gzencode函数的区别在于它们压缩的数据格式不同:

gzcompress使用的是zlib格式;

gzdeflate使用的是纯粹的deflate格式;

gzencode使用的是gzip格式;

但是有一点是相同的,它们压缩数据时都使用了deflate压缩算法(理论上zlib和gzip格式可以使用其他的压缩算法,但是目前实践中只使用deflate算法),zlib和gzip只不过是在deflate的基础之上加了一些头部和尾部而已。

顺便提一下,http协议中的content-encoding: deflate使用的是zlib格式而不是纯deflate格式。

从php 5.4.0开始,gzcompress和gzdeflate函数加入了第三个参数$encoding,可以是三个常量:

zlib_encoding_raw 对应于纯deflate格式;

zlib_encoding_gzip 对应于gzip格式;

zlib_encoding_deflate 对应于zlib格式(注意不是纯deflate格式);

虽然文档没有提及,但是这三个常量也可以用在gzencode函数的第三个参数$encoding_mode中。

其实从php 5.4.0开始,这三个函数是一样的,只不过第三个参数的默认值不同;如果调用时传入第三个参数,那么这三个函数返回的数据相同。可以写一个简单的脚本测试:

复制代码 代码如下:

<?php
$url = 'http://jb51.net';
$s1 = gzdeflate($url, 1);
$s2 = gzencode($url, 1, zlib_encoding_raw);
if (strcmp($s1, $s2) == 0) echo 'the same';
?>

运行可以看到$s1和$s2是相同的,为什么会这样呢?可以从php源码中找到答案,打开php-5.5.4\ext\zip\zlib.c,可以找到这样的代码:


复制代码 代码如下:

#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);
/* }}} */

可以看到,gzdeflate gzencode gzcompress三个函数都是用相同的php_zlib_encode_func宏定义的(是不是有些泛型的意味?),所以它们当然是相同的。

代码中的注释也承认这几个函数的名字起得不好,至于为什么会用这样的名字就不得而知了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网