Replacing files in a zip archive

Scott David Daniels Scott.Daniels at Acm.Org
Thu Apr 30 19:26:57 CEST 2009


MRAB wrote:
> Дамјан Георгиевски wrote:
>> I'm writing a script that should modify ODF files. ODF files are just 
>> .zip archives with some .xml files, images etc.
>> So far I open the zip file and play with the xml with lxml.etree, but 
>> I can't replace the files in it.
>>
>> Is there some recipe that does this ?
>>
> You'll have to create a new zip file and copy the files into that.

I agree.  In particular, even if there _were_ a good way to replace a
file in a zip, you risk the entire .zip if anything goes wrong
while you make changes like removing a file from the zip (and it
is essentially impossible to replace-in-place a zip element).  Since
zips are often used to store collections of files, that is a problem.
You can write multiple versions of a file in the zip, but "which one
wins" becomes problematic (often defined by implementation), and that
approach means that your zip grows with every change.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list