使用 Python 访问 .zipx

Accessing .zipx with Python

我正在尝试编写一个非常简单的脚本来计算给定 ZIP 文件的 entries/files 数量,以进行一些统计。

我正在使用 zipfile 库,我 运行 遇到了这个库似乎不支持 .zipx 格式的问题。

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

谷歌搜索显示 zipx 格式与 zip 格式不同,所以我可能不希望它起作用。进一步谷歌搜索虽然无法调出一个实际上 可以 处理 zipx 的库。搜索堆栈溢出也没有找到太多。

我不可能是唯一一个想在 python 中操作 zipx 文件的人吧?有什么建议吗?

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!"

API 文档:http://www.chilkatsoft.com/refdoc/pythonCkCompressionRef.html

没有直接的 python 包来解压 python 中的 zipx 文件。 因此,解压缩它的一种简单方法是使用子进程和 winzip 应用程序。请找到以下代码。

import subprocess

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

subprocess.run(command, shell=True, timeout=120)