creating ZIP files on the cheap

John Machin sjmachin at lexicon.net
Wed Dec 23 18:47:47 EST 2009


On Dec 24, 7:34 am, samwyse <samw... at gmail.com> wrote:
> I've got an app that's creating Open Office docs; if you don't know,
> these are actually ZIP files with a different extension.  In my case,
> like many other people, I generating from boilerplate, so only one
> component (content.xml) of my ZIP file will ever change.  Instead of
> creating the entire ZIP file each time, what is the cheapest way to
> accomplish my goal?  I'd kind-of like to just write the first part of
> the file as a binary blob, then write my bit, then write most of the
> table of contents as another blob, and finally write a TOC entry for
> my bit.  Has anyone ever done anything like this?  Thanks.

Option 1: set up a file that contains everything except the
content.xml. Then for each new file: copy the "empty" file, open the
copy with zipfile (mode 'a') and write your content.xml. This at least
is understandable and maintainable.

Option 2 (recommended): insert some timing apparatus into your script.
How much time is taken by the template stuff? Is it worth chancing
your arm on getting the "binary blob" stuff correct? Is it
maintainable? I.e. pretend that the next person to maintain your code
knows where you live and owns a chainsaw.



More information about the Python-list mailing list