3

I'm attempting to write a very simple script that counts the number of entries/files a given ZIP file has, for some statistics.

I'm using the zipfile library, and I'm running into this problem where the library appears not to support .zipx format.

bash-3.1$ python zipcount.py t.zipx

Traceback (most recent call last):
  File "zipcount.py", line 10, in <module>
    zipCount(file)
  File "zipcount.py", line 5, in zipCount
    with ZipFile(file, "r") as zf:
  File "c:\Python34\lib\zipfile.py", line 937, in __init__
    self._RealGetContents()
  File "c:\Python34\lib\zipfile.py", line 978, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Googling for help reveals that the zipx format is not the same as zip, and so maybe I shouldn't be expecting this to work. Further googling though fails to bring up a library that actually can deal with zipx. Searching stack overflow didn't find much either.

I can't possibly be the only person who wants to manipulate zipx files in python, right? Any suggestions?

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • .zipx appears to use xz compression (http://kb.winzip.com/kb/entry/7). See here for how to decompress xz archives: https://stackoverflow.com/questions/17217073/how-to-decompress-a-xz-file-which-has-multiple-folders-files-inside-in-a-singl – chown Apr 24 '15 at 15:56
  • Attempting to use lzma on zipx seems to yield `_lzma.LZMAError: Input format not supported by decoder` – FrobberOfBits Apr 24 '15 at 16:03
  • 1
    http://kb.winzip.com/help/help_compression.htm states that winzip will use one of these 4 compression methods based on whatever if thinks will make the smallest archive: `bzip2`, `LZMA`, `PPMd`, or `XZ`. So, since xz and lzma dont work, try bz2: https://docs.python.org/2/library/bz2.html. Also check this out for a python interface to all 4: https://www.chilkatsoft.com/compression-python.asp – chown Apr 24 '15 at 16:07

2 Answers2

1

chilkat might work for this. It's not a free library but there is a 30 day trial. Here is an example from http://www.example-code.com/python/ppmd_compress_file.asp:

import sys
import chilkat

compress = chilkat.CkCompression()

#  Any string argument automatically begins a 30-day trial.
success = compress.UnlockComponent("30-day trial")
if (success != True):
    print "Compression component unlock failed"
    sys.exit()

compress.put_Algorithm("ppmd")

#  Decompress back to the original:
success = compress.DecompressFile("t.zipx", "t")
if (success != True):
    print compress.lastErrorText()
    sys.exit()

print "Success!"

The API documentation: http://www.chilkatsoft.com/refdoc/pythonCkCompressionRef.html

chown
  • 51,908
  • 16
  • 134
  • 170
0

There is no direct python package to unzip the zipx files in python. So, One simple way to unzip it is using subprocess and winzip application. Please find the below code.

import subprocess

command = "C:\Program Files\WinZip\wzunzip.exe" "D:\Downloads\hello.zipx" "D:\unzip_location"

subprocess.run(command, shell=True, timeout=120)
David Buck
  • 3,752
  • 35
  • 31
  • 35
anil kumar
  • 774
  • 7
  • 7