skip to Main Content

The php code is
”’

$input_file = "a.txt";
$source = file_get_contents($input_file);
$source = gzcompress($source);
file_put_contents("php.txt",$source)

”’

The python code is
”’

testFile = "a.txt"
content = None
with open(testFile,"rb") as f:
    content = f.read()

outContent = zlib.compress(content)
with open("py.txt","wb") as f:
    f.write(outContent)

”’

The python3 version is [Python 3.6.9] The php version is [PHP 7.2.17]

I need the same result for same md5.

2

Answers


  1. Chosen as BEST ANSWER

    I find the solution. The code is

            compress = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,zlib.DEFLATED, 15, 9)
            outContent = compress.compress(content)
            outContent += compress.flush()
    

    The python zlib provide a interface "zlib.compressobj",which returns a compressobj,and the parameters decide the result. You can adjust parameters to make sure the python's result is same with php's


  2. The problem is not in PHP or Python, but rather in your "need". You cannot expect to get the same result, unless the two environments happen to be using the same version of the same compression code with the same settings. Since you do not have control of the version of code being used, your "need" can never be guaranteed to be met.

    You should instead be doing your md5 on the decompressed data, not the compressed data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search