免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5836 | 回复: 5
打印 上一主题 下一主题

如何用zipfile往zip压缩包中写入压缩包的说明信息? [复制链接]

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-22 11:41 |只看该作者 |倒序浏览
对于一个已经生成的 "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压缩包里写入注释的方法不对么? 这样的差异问题该如何解决呢?

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2014-05-22 12:58 |只看该作者
zip内容不一样,md5变化是正常的,md5变化影响了谁,改谁的代码。

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
3 [报告]
发表于 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


   

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
4 [报告]
发表于 2014-05-22 14:33 |只看该作者
提交bug给zipfile

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
5 [报告]
发表于 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吧。

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
6 [报告]
发表于 2014-05-22 17:20 |只看该作者
哦, 谢谢兄台的解答了, 小弟还以为是我用的不对呢!
回复 5# timespace


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP