这是生成的文件的大小zip
:
$ seq 10000 > 1.txt
$ zip 1 1.txt
adding: 1.txt (deflated 54%)
$ ls -og 1.zip
-rw-r--r-- 1 22762 Aug 29 10:04 1.zip
这是一个等效的 python 脚本:
import zipfile
z = zipfile.ZipFile(sys.argv[1], 'w', zipfile.ZIP_DEFLATED)
fn = sys.argv[1]
z.writestr(zipfile.ZipInfo(fn), sys.stdin.read())
z.close()
生成的 zip 文件的大小如下:
$ seq 10000 | ./main.py 2.zip 2.txt
$ ls -go 2.zip
-rw-r--r-- 1 49002 Aug 29 10:15 2.zip
有谁知道为什么python版本生成的zip文件没有像生成的那样小zip
?
事实证明(在 python 3 中检查)当ZipInfo
用来,writestr()
不会使用compression
and compresslevel
of zipfile.ZipFile.__init()
。这是一个糟糕的 API 设计的例子。是否使用ZipInfo应该是设计好的,compression
and compresslevel
总是使用构造函数中的。
When passing a ZipInfo instance as the zinfo_or_arcname parameter, the compression method used will be that specified in the compress_type member of the given ZipInfo instance. By default, the ZipInfo constructor sets this member to ZIP_STORED.
因此,原帖中显示的 python 代码基本上没有压缩。因此,python代码生成的文件大小较大。
这个API设计的另一个问题是参数compression
来自构造函数的内容与compress_type
of .writestr()
但它们的名字并不相同。这是另一个糟糕的设计。没有理由为同一个事物赋予不同的名称。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)