<分区>
我想使用 python 更新 zip 文件中的条目 zipfile模块。 我的问题是这会生成一个新条目。
请假设我有这段代码:
from zipfile import ZipFile,ZIP_DEFLATED
with ZipFile("myfile.zip","w") as z:
z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
### how to update the hello.txt file here ?
z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
在此之后实际的 zip 文件有两个条目而不是一个:
$ unzip -l myfile.zip
Archive: myfile.zip
Length Date Time Name
--------- ---------- ----- ----
24 2013-02-19 22:48 hello.txt
24 2013-02-19 22:48 hello.txt
--------- -------
48 2 files
$ python --version
Python 3.3.0
$
我知道写一个完整的新文件的方法,但这会 如果内容很大,会花费很多时间。
zip(1) 实用程序可以做到这一点(使用“-u”选项)那么为什么 python 不行呢? 有什么方法我仍然可以使用 python 实现此目的吗?