ZipFile - file adding API incomplete?

Glenn Maynard glenn at zewt.org
Mon Nov 16 19:23:51 EST 2009


I want to do something fairly simple: read files from one ZIP and add
them to another, so I can remove and replace files.  This led me to a
couple things that seem to be missing from the API.

The simple approach would be to open each file in the source ZIP, and
hand it off to newzip.write().  There's a missing piece, though:
there's no API that lets me pass in a file-like object and a ZipInfo,
to preserve metadata.  zip.write() only takes the filename and
compression method, not a ZipInfo; writestr takes a ZipInfo but only
accepts a string, not a file.  Is there an API call I'm missing?
(This seems like the fundamental API for adding files, that write and
writestr should be calling.)

The correct approach is to copy the data directly, so it's not
recompressed.  This would need two new API calls: rawopen(), acting
like open() but returning a direct file slice and not decompressing
data; and rawwrite(zinfo, file), to pass in pre-compressed data, where
the compression method in zinfo matches the compression type used.

I was surprised that I couldn't find the former.  The latter is an
advanced one, important for implementing any tool that modifies large
ZIPs.  Short-term, at least, I'll probably implement these externally.

-- 
Glenn Maynard



More information about the Python-list mailing list