Add file to zip, or replace file in zip

Scott David Daniels scott.daniels at acm.org
Sun Apr 30 14:40:14 EDT 2006


Roger Miller wrote:
> First note that zipfile is a plain Python module, so reading
> Python.../Lib/zipfile.py will reveal all its secrets.
> 
> I don't think it is possible to replace archive members using the
> module. You could copy all the files into a new zip file, replacing the
> ones you want to change as you go. But it might be easier just to use
> os.system() or something similar to run an external zip program.
> 
It is not currently possible to delete or replace individual elements
in a zip file, nor (in all likelihood) would you want to do so.  It
would require having the zip[ file in a broken state for some time
as you copy data from one area of the zip to another.  Any error
(or raised exception like Control-C) during this process is likely
to leave you with an inconsistent and therefore unreadable zip file.
The typical technique is to mark some files "deleted" in the directory,
add new files (possibly the replacements for the deleted files), and,
in one pass, copy all non-deleted files to a new zip (which you can then
swap for the original zip).  Shortcutting this process puts all data in
your zip file at risk.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list