Compress string in php and decompress in python using zlib

DuGNu :

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

AterLux :
  1. try to escape non-ASCII charaters in the string constant:
<?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'
  1. try to use 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.

edited at
0

Comments

0 comments
Login to comment

Related

Use zlib.js to decompress python zlib compress

Compress and decompress zlib (RFC 1950) using DEFLATE (RFC 1951) functions

How to compress / decompress string with using SevenZip - 7Zip

Zlib compress in python

Zlib is unable to extract a compress String in java, compression is done in python

How to keep the header and trailer while zlib decompress and compress

Error compress/decompress spdy name/value block with zlib+dictionary

python zlib how to decompress many objects

Zlib won't decompress python 2.7

Compress and decompress utf8 string with brotli

Using Gzip to compress/decompress an array of bytes

Python wont save compressed string using zlib

Re-compress a decompressed string with zlib

How to compress string and store in redis using python

flutter/dart: How to decompress/inflate zlib binary string in flutter

Base64 string compress in client and decompress in server

compress/decompress string in node.js and javascript client side

Decompress zlib stream in Clojure

JavaScript Zlib Decompress

Decompress Stream to String using SevenZipSharp

How to compress and decompress a file using lz4?

Compress in Java, decompress in Python - snappy/redis-py-cluster

Compress a string in php not match

zlib error code -3 while using zlib to decompress PDF Flatedecode stream

Tool to compress/decompress STDIN

Compress/Decompress based on probability

how to compress image using imagemagick + command line string + php?

Are zlib.compress on Python and Deflater.deflate on Java (Android) compatible?

How to compress data in C# to be decompressed in zlib python