我正在尝试编写一个非常简单的脚本来计算给定 ZIP 文件所具有的条目/文件的数量,以进行一些统计。
我正在使用 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可能适用于此。它不是免费图书馆,但有 30 天的试用期。这是来自 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
