ZipFile - file adding API incomplete?

Diez B. Roggisch deets at nospam.web.de
Tue Nov 17 03:33:41 EST 2009


Glenn Maynard schrieb:
> 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.

No idea why the write doesn't accept an open file - OTOH, as passing a 
string is just


   writestr(info, in_file.read())


I don't think that's *that* much of an inconvenience..

And regarding your second idea: can that really work? Intuitively, I 
would have thought that compression is adaptive, and based on prior 
additions to the file. I might be wrong with this though.

Diez



More information about the Python-list mailing list