This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: zipfile.ZipFile.write should accept fp as argument
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: alanmcintyre, proppy, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2011-05-02 16:34 by proppy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
zipfile-add-writefp.patch proppy, 2011-05-03 13:40 patch
Messages (4)
msg134989 - (view) Author: Johan Euphrosine (proppy) Date: 2011-05-02 16:34
Currently it only accept a filename (and writestr only accept bytes).
msg135039 - (view) Author: Johan Euphrosine (proppy) Date: 2011-05-03 13:40
Here is a tentative implementation.

Feel free to review it.
msg135396 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-05-07 01:20
zipfile.ZipFile takes a file arg that can be a 'path to a file (a string) or a file-like object'. The .write() method takes a filename arg. I would think that the proposal should be what the title says, to expand that to a file arg, either a path or 'file', as with ZipFile itself. I cannot think of any reason not to do that.

The patch adds a separate .writefp method with the file and arcname switched and the compression_type omitted. The code is mostly a duplicated subset of .write. While is proves the concept, none of the differences strike me as good. I think instead there should just be one method that branches, where appropriate, on the type of 'file', just as ZipFile.__init__ does. I admit that I do not understand what .write is supposed to do or does do when filename is a directory.

The patch includes a test. It still needs doc and News patches, but those come after the basic patch is done.
msg265543 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-14 21:15
A more general feature is implemented in issue26039. Now you can write to ZIP archive the content of opened file object with following two lines:

        with'file.txt', 'wb') as target:
            shutil.copyfileobj(source, target)
Date User Action Args
2022-04-11 14:57:16adminsetgithub: 56189
2016-05-14 21:15:10serhiy.storchakasetstatus: open -> closed
resolution: rejected
messages: + msg265543

stage: patch review -> resolved
2012-04-07 19:25:47serhiy.storchakasetnosy: + serhiy.storchaka
2011-05-07 01:20:39terry.reedysetversions: + Python 3.3, - Python 3.4
nosy: + alanmcintyre, terry.reedy

messages: + msg135396

stage: patch review
2011-05-03 13:40:55proppysetfiles: + zipfile-add-writefp.patch
keywords: + patch
messages: + msg135039
2011-05-02 16:34:51proppycreate