Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将zlib.decompress从python更改为php_Php_Python - Fatal编程技术网

将zlib.decompress从python更改为php

将zlib.decompress从python更改为php,php,python,Php,Python,我想把python代码转换成php 我的问题是这一行: zlib.decompress( $gzipped, 16 + zlib.MAX_WBITS ); 我不知道php中的zlib.maxwbits 我的php代码: $response = gzuncompress( $gzipped, ??? ); 反馈后编辑的答案 这有点麻烦,但是你可以用PHP来完成 唯一的问题是您需要从流中获取压缩数据。。。我不是PHP的家伙,所以我会让你去弄清楚 PHP>=5.4.0的原始答案如下 简短回答:尝试

我想把python代码转换成php

我的问题是这一行:

zlib.decompress( $gzipped, 16 + zlib.MAX_WBITS );
我不知道php中的zlib.maxwbits

我的php代码:

$response = gzuncompress( $gzipped, ??? );
反馈后编辑的答案

这有点麻烦,但是你可以用PHP来完成

唯一的问题是您需要从流中获取压缩数据。。。我不是PHP的家伙,所以我会让你去弄清楚

PHP>=5.4.0的原始答案如下

简短回答:尝试gzdecode或zlib_解码而不是gzuncompress

长长的回答

在python 3中:

>>> print(zlib.decompress.__doc__)
decompress(string[, wbits[, bufsize]]) -- Return decompressed string.

Optional arg wbits is the window buffer size.  Optional arg bufsize is
the initial output buffer size.
>>> print(zlib.MAX_WBITS)
15
>>> print(16 + zlib.MAX_WBITS)
31
这意味着数据最初是使用31的窗口大小进行压缩的。在PHP中,大多数zlib函数的默认窗口大小是15,因此gzuncompress和gzinflate失败。然而,gzdecode和zlib_decode似乎可以工作,我不知道为什么:

使用python3压缩

使用PHP解压


您可以将其留空,它是可选参数此参数是解码的最大长度。这里谈一谈。[链接]。似乎很重要,但是在PHP5.4.0中添加了lib_解码,非常好。。。我的主机是PHP5.3。38@user3747500:使用压缩过滤器解决方案更新答案。
>>> print(zlib.decompress.__doc__)
decompress(string[, wbits[, bufsize]]) -- Return decompressed string.

Optional arg wbits is the window buffer size.  Optional arg bufsize is
the initial output buffer size.
>>> print(zlib.MAX_WBITS)
15
>>> print(16 + zlib.MAX_WBITS)
31
>>> 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 > $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