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

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开始,这三个函数是一样的,只不过第三个参数的默认值不同;如果调用时传入第三个参数,那么这三个函数返回的数据相同。可以写一个简单的脚本测试:

$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宏定义的(是不是有些泛型的意味?),所以它们当然是相同的。

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

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值