'Python's zipfile module cannot update entries [duplicate]
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?
Solution 1:[1]
I doubt the zipfile
module can modify archives like you want. If it could, there would be a method to remove a file from a ZipFile
, which there isn't.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jon-Eric |