tank064 发表于 2014-05-22 11:41

如何用zipfile往zip压缩包中写入压缩包的说明信息?

对于一个已经生成的 "test.zip" 压缩包,在shell命令行下echo "comment"|zip -z test.zip可以往zip压缩包中写入"comment"说明信息.

想用python实现这个简单的功能import zipfile

zf = zipfile.ZipFile('test.zip', 'a')
zf.comment = 'comment'
zf.close()ok, 这样没有问题!
但是当我第二次在写入新的说明信息时, 遇到了些问题!

shell终端下:echo "test" |zip -z test.zippython代码import zipfile

zf = zipfile.ZipFile('test.zip', 'a')
zf.comment = 'test'
zf.close()这次, 由于第二次写入的注释信息比第一次短, 造成两种方式生成的 'test.zip'压缩包大小, md5值均发生的变化.

经过多次测试, 当多次写入注释, 且后面写入的注释信息比上一次短时, 用python zipfile实现的方式会与用shellzip -z方式 生成的压缩包的 大小, md5值发生变化!

请问, 是我往zip压缩包里写入注释的方法不对么? 这样的差异问题该如何解决呢?

timespace 发表于 2014-05-22 12:58

zip内容不一样,md5变化是正常的,md5变化影响了谁,改谁的代码。

tank064 发表于 2014-05-22 14:29

root@Debian:~# echo "short"|zip -z test.zip
enter new zip file comment (end with .):
root@Debian:~# md5sum test.zip
48da9079a2c19f9755203e148731dae9test.zip
root@Debian:~# echo "longlong"|zip -z test.zip
current zip file comment is:
short
enter new zip file comment (end with .):
root@Debian:~# md5sum test.zip
3618846524ae0ce51fbc53e4b32aa35btest.zip
root@Debian:~# echo "short"|zip -z test.zip
current zip file comment is:
longlong
enter new zip file comment (end with .):
root@Debian:~# md5sum test.zip
48da9079a2c19f9755203e148731dae9test.zip
root@Debian:~# echo "longlong"|zip -z test.zip
current zip file comment is:
short
enter new zip file comment (end with .):
root@Debian:~# md5sum test.zip
3618846524ae0ce51fbc53e4b32aa35btest.zip第一次和第三次操作后, test.zip md5一致; 第二次和第四次操作后, test.zip md5一致;
现在用python zipfile 这样处理后, 没办法做到这样!
所以我想问问, 该如何使用python zipfile往 "zip"压缩包里添加注释信息?
回复 2# timespace


   

r2007 发表于 2014-05-22 14:33

提交bug给zipfile

timespace 发表于 2014-05-22 15:35

明白楼主意思了。这个从API用法上是无法解释的,即使是解决办法也看不出来。。。
The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.

本质原因需要从zipfile实现和PKZIP算法入手。但应该不用这么纠结吧,如果你很依赖md5的变化与否,就用python调用shell的zip吧。

tank064 发表于 2014-05-22 17:20

哦, 谢谢兄台的解答了, 小弟还以为是我用的不对呢!
回复 5# timespace


   
页: [1]
查看完整版本: 如何用zipfile往zip压缩包中写入压缩包的说明信息?