I know this question has already been asked there: Compress string in php and decompress in python
But there was no answer provided (discussion in chat has been lost).
I want a PHP client to compress a string on its side, send it to the server as a string contained in a json, then I want to be able to decompress it on my side.
I tried with zlib :
$ php -a
Interactive shell
php > $msg = "abcdefghijk";
php > $compressed = gzcompress($msg);
php > echo "'".$compressed."'"
php > ;
'x�KLJNIMK�����c'
$ python3
Python 3.7.8 (heads/master-dirty:daa285d, Jul 28 2020, 20:00:50)
[GCC 9.3.1 20200408 (Red Hat 9.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> comp_msg=r'x�KLJNIMK�����c'
>>> msg = zlib.decompress(comp_msg.encode('utf-8'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
... but it does not work. I guess this is a problem of string encoding, but using PHP's mb_convert_encoding($compressed, "UTF-8"); does not solve the problem.
I can not ask the creator of first occurence of this question by lack of reputation ... Any help will be appreciated.
Thanks
<?php
$msg = "abcdefghijk";
$compressed = gzcompress($msg);
echo "'".addcslashes($compressed, "\x00..\x1F\\\'\"\x7F..\xFF")."'";
// outputs: 'x\234KLJNIMK\317\310\314\312\006\000\031\351\004c'
b
prefix to binary string literals in python:import zlib
comp_msg=b'x\234KLJNIMK\317\310\314\312\006\000\031\351\004c';
msg = zlib.decompress(comp_msg)
print(msg)
# prints: b`abcdefghijk`
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments