1

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> 
ngong
  • 754
  • 1
  • 8
  • 23
  • Sorry, this question was asked a bit too fast: I overlooked that - obviously - a gzip file needs [more](http://www.zlib.org/rfc-gzip.html#file-format) than just the compressed bytes. Shall I leave it or delete it? What is best *stackoverflow* habbit? – ngong Nov 03 '21 at 11:07
  • If you think the mistake is something that others are likely to make in the future, then this could still serve as a useful reference for them. In that case, you should post an answer and accept it when the system allows you to do so (there's a longer delay on when you're allowed to accept your own answer; the system makes you wait 48 hours). If this was really just a typo (or think-o), then we have a close reason for that (#2 at https://stackoverflow.com/help/on-topic). If you'd like to close this question on that basis, reply to me in a comment here, and I can do it for you. – Cody Gray - on strike Nov 03 '21 at 11:23
  • @CodyGray Super, thanks! Nobody is perfect, especially not me. As I looked for compressing strings in *Python*, I did not come around **gzip**, just **zlib**. This mistake is likely to repeat with others. – ngong Nov 03 '21 at 13:26
  • See https://stackoverflow.com/questions/20762094/how-are-zlib-gzip-and-zip-related-what-do-they-have-in-common-and-how-are-they/20765054#20765054 – Mark Adler Nov 03 '21 at 20:16

2 Answers2

0

Exchanging the Python module zlib by gzip does not explain the error, but can be taken as a workaround.

from tempfile import gettempdir
import gzip
from subprocess import run

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

# zip using zlib

with gzip.open(zipname,"wb") as gz:
    with open(filename,"r") as f:
        gz.write( bytes(f.read(), "utf-8") )
        gz.close()

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

leads to the expected output:

PS C:\Users\ngong\python-workspace> & C:/Users/ngong/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ngong/python-workspace/testGzip.py
now unzipping on cmd level 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, 305 bytes (1 KiB)

Extracting archive: C:\Users\NGONG\AppData\Local\Temp\zipped.gz
--
Path = C:\Users\NGONG\AppData\Local\Temp\zipped.gz
Type = gzip
Headers Size = 17

Everything is Ok

Size:       484
Compressed: 305
ngong
  • 754
  • 1
  • 8
  • 23
0

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.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158