I would like to update an entry in a zip file using pythons zipfile module. My problem is that this generates a new entry.
Please assume I have this code:
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)
After this the actual zip file has two entries instead of one:
$ 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
$
I know of the method to write a complete new file, but this would take much time if the content is big.
The zip(1) utility can do this ( using the "-u" option) so why not python? Is there any way I can still achieve this using python?