php zlib decompress,改变zlib.减压从python到php

本文介绍了如何在PHP中使用zlib.inflate过滤器和gzdecode函数解压使用Python的zlib.compressobj(31窗口大小)压缩的数据。虽然gzuncompress和gzinflate在默认配置下会失败,但gzdecode和zlib_decode成功地解压了数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

反馈后编辑答案

这有点麻烦,但是可以用PHP compression filters来完成。在$params = array('window' => 31);

$fp = fopen('your_data', 'rb');

stream_filter_append($fp, 'zlib.inflate', STREAM_FILTER_READ, $params);

print fread($fp, 1024);

唯一的问题是你需要从一个流中获取压缩数据。。。我不是PHP的人,所以我将把它留给你来解决。在

以下PHP>=5.4.0的原始答案

简短回答:尝试gzdecode()或{}而不是{}。在

长时间回答…

在python(python 3)中:

^{pr2}$

这意味着数据最初是使用31的窗口大小进行压缩的。在PHP中,大多数zlib函数的默认窗口大小为15,因此gzuncompress()和{}失败。然而,似乎gzdecode()和{}确实起作用了(我实际上不知道为什么):

使用python3压缩>>> import zlib

>>> compressor = zlib.compressobj(wbits=(16+zlib.MAX_WBITS))

>>> compressed = compressor.compress(b'Here is a test string compressed by Python with a window size of 31\n')

>>> compressed += compressor.flush()

>>> compressed

b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\r\xca\xb1\r\x800\x0c\x04\xc0\x9e)~\x05\xc4\x12\x94\xac\x00\xe4!.\x12#\xdb\x92\x15\xa6\x87\xabo\xa5\x11\xe2\xd8\x11\xf4\x80\x87I\xbfqj{\x8c\xee,8\x06\xb6\x11U;R\xa2\xfe/\xa5\x17M\xb8\xbc\x84^X\xe6\xe9\x03\xabEV\xdbD\x00\x00\x00'

>>> open('/tmp/compressed', 'wb').write(compressed)

85

使用PHP解压缩php > $compressed = file_get_contents('/tmp/compressed');

php > print gzuncompress($compressed);

PHP Warning: gzuncompress(): data error in php shell code on line 1

php > print gzinflate($compressed);

PHP Warning: gzinflate(): data error in php shell code on line 1

php > print gzdecode($compressed);

Here is a test string compressed by Python with a window size of 31

php > print zlib_decode($compressed);

Here is a test string compressed by Python with a window size of 31

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值