Chinaunix

标题: 如何用zipfile往zip压缩包中写入压缩包的说明信息? [打印本页]

作者: tank064    时间: 2014-05-22 11:41
标题: 如何用zipfile往zip压缩包中写入压缩包的说明信息?
对于一个已经生成的 "test.zip" 压缩包,  在shell命令行下
  1. echo "comment"|zip -z test.zip
复制代码
可以往zip压缩包中写入"comment"说明信息.

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

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

shell终端下:
  1. echo "test" |zip -z test.zip
复制代码
python代码
  1. import zipfile

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

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

请问, 是我往zip压缩包里写入注释的方法不对么? 这样的差异问题该如何解决呢?
作者: timespace    时间: 2014-05-22 12:58
zip内容不一样,md5变化是正常的,md5变化影响了谁,改谁的代码。
作者: tank064    时间: 2014-05-22 14:29
  1. root@Debian:~# echo "short"|zip -z test.zip
  2. enter new zip file comment (end with .):
  3. root@Debian:~# md5sum test.zip
  4. 48da9079a2c19f9755203e148731dae9  test.zip
  5. root@Debian:~# echo "longlong"|zip -z test.zip
  6. current zip file comment is:
  7. short
  8. enter new zip file comment (end with .):
  9. root@Debian:~# md5sum test.zip
  10. 3618846524ae0ce51fbc53e4b32aa35b  test.zip
  11. root@Debian:~# echo "short"|zip -z test.zip
  12. current zip file comment is:
  13. longlong
  14. enter new zip file comment (end with .):
  15. root@Debian:~# md5sum test.zip
  16. 48da9079a2c19f9755203e148731dae9  test.zip
  17. root@Debian:~# echo "longlong"|zip -z test.zip
  18. current zip file comment is:
  19. short
  20. enter new zip file comment (end with .):
  21. root@Debian:~# md5sum test.zip
  22. 3618846524ae0ce51fbc53e4b32aa35b  test.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


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2