Search code examples
pythonzipgzip

Why does 7-zip not decode what is encoded by zlib.compress()?


In the following Python (3.10) program testCompress.py, as a test input, the source code itself gets compressed applying zlib, and shall get unzipped using 7-zip (20.00 alpha) on Windows 10. But it fails. Not accepting the compressed bytes as an archive. The output is given below the program.

This is an extract. My real problem is, to unzip the Python-compressed file using Java java.util.zip.GZIPInputStream or ZipInputStream. They also justify the bytes as not an archive.

from tempfile import gettempdir
from zlib import compress
from subprocess import run

filename=r"testCompress.py"
zipname = gettempdir() + r"\zipped.zip"
unzippedname = gettempdir() + r"\unzipped.txt"
unzip = r'"C:\Program Files\7-Zip\7z.exe" x ' + zipname

# zip using zlib

with open(filename,"r") as f:
    text = f.read()
b = bytes(text,"ascii")
bcompressed = compress(b)
with open(zipname,"wb") as f:
    f.write(bcompressed)
    f.close()

# unzip using 7-zip
print("now unzipping on cmd level", unzippedname)
run(unzip, shell=True)

leading to the ouput:

PS C:\Users\ngong\python-workspace> & C:/Users/ngong/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ngong/python-workspace/xslt/testCompress.py
unzipping C:\Users\NGONG\AppData\Local\Temp\unzipped.txt

7-Zip 20.00 alpha (x64) : Copyright (c) 1999-2020 Igor Pavlov : 2020-02-06

Scanning the drive for archives:
1 file, 9041 bytes (9 KiB)

Extracting archive: C:\Users\NGONG\AppData\Local\Temp\zipped.zip
ERROR: C:\Users\NGONG\AppData\Local\Temp\zipped.zip
C:\Users\NGONG\AppData\Local\Temp\zipped.zip
Open ERROR: Can not open the file as [zip] archive


ERRORS:
Is not archive

Can't open as archive: 1
Files: 0
Size:       0
Compressed: 0
PS C:\Users\ngong\python-workspace> 

Solution

  • zlib.compress() will always produce a zlib stream. On the Java side you will apparently be looking for a gzip stream or a zip file. Those are three different things.

    You can use zlib.compressobj() in Python to generate a gzip stream. (See the documentation for how to request that.)

    See this answer for some background on those three formats.