$ echo "short"|zip -z test.zip
enter new zip file comment (end with .):
$ md5sum test.zip
48da9079a2c19f9755203e148731dae9 test.zip
$ echo "longlong"|zip -z test.zip
current zip file comment is:
short
enter new zip file comment (end with .):
$ md5sum test.zip
3618846524ae0ce51fbc53e4b32aa35b test.zip
$ echo "short"|zip -z test.zip
current zip file comment is:
longlong
enter new zip file comment (end with .):
root@Debian:~# md5sum test.zip
48da9079a2c19f9755203e148731dae9 test.zip
$ echo "longlong"|zip -z test.zip
current zip file comment is:
short
enter new zip file comment (end with .):
$ md5sum test.zip
3618846524ae0ce51fbc53e4b32aa35b test.zip
$
$
$ cat test.py
#/usr/bin/env python
#coding: utf-8
import zipfile
import subprocess
zip_name = 'test.zip'
def add_comment(zip_name, comment):
with zipfile.ZipFile(zip_name, 'a') as zf:
zf.comment = comment
add_comment(zip_name, "short")
print subprocess.Popen(['md5sum', zip_name], stdout=subprocess.PIPE).communicate()[0],
add_comment(zip_name, "longlong")
print subprocess.Popen(['md5sum', zip_name], stdout=subprocess.PIPE).communicate()[0],
add_comment(zip_name, "short")
print subprocess.Popen(['md5sum', zip_name], stdout=subprocess.PIPE).communicate()[0],
add_comment(zip_name, "longlong")
print subprocess.Popen(['md5sum', zip_name], stdout=subprocess.PIPE).communicate()[0],
$
$ python test.py
48da9079a2c19f9755203e148731dae9 test.zip
3618846524ae0ce51fbc53e4b32aa35b test.zip
89c2038501f737991ca21aa097ea9956 test.zip
3618846524ae0ce51fbc53e4b32aa35b test.zip
At shell env, the fist time and the third time, "test.zip" md5 values are the same, the second time and the forth time, "test.zip" md5 values are the same, but when i use python zipfile, the result is not like that. How can I do this with the Python zipfile
module?
The zipfile.ZipFile()
class has a comment
attribute you can set.
Open the file in append mode, alter the comment, and it'll be written out when closed:
from zipfile import ZipFile
with ZipFile('test.zip', 'a') as testzip:
testzip.comment = 'short'
When you use a ZipFile
as a context manager, like a regular file it'll automatically be closed when the with
block is exited.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.