Friday, 15 July 2011

change zlib.decompress from python to php -



change zlib.decompress from python to php -

i want convert python code php.

my problem line (python):

zlib.decompress( $gzipped, 16 + zlib.max_wbits );

i don't know zlib.max_wbits in php

my code (in php) :

$response = gzuncompress( $gzipped, ??? );

answer edited after feedback

it's bit of hassle, can 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);

the problem need source compressed info stream... i'm no php guy leave figure out.

original reply below php >= 5.4.0

short answer: seek gzdecode() or zlib_decode() instead of gzuncompress().

long answer...

in python (python 3):

>>> print(zlib.decompress.__doc__) decompress(string[, wbits[, bufsize]]) -- homecoming decompressed string. optional arg wbits window buffer size. optional arg bufsize initial output buffer size. >>> print(zlib.max_wbits) 15 >>> print(16 + zlib.max_wbits) 31

this implies info compressed using window size of 31. in php default window size zlib functions 15, hence gzuncompress() , gzinflate() fail. however, seems gzdecode() , zlib_decode() work ( don't know why):

compress using python3

>>> import zlib >>> compressor = zlib.compressobj(wbits=(16+zlib.max_wbits)) >>> compressed = compressor.compress(b'here test string compressed python 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

decompress using php

php > $compressed = file_get_contents('/tmp/compressed'); php > print gzuncompress($compressed); php warning: gzuncompress(): info error in php shell code on line 1 php > print gzinflate($compressed); php warning: gzinflate(): info error in php shell code on line 1 php > print gzdecode($compressed); here test string compressed python window size of 31 php > print zlib_decode($compressed); here test string compressed python window size of 31

php python

No comments:

Post a Comment