diff -r 8270c5f59d26 Lib/test/test_zipfile.py --- a/Lib/test/test_zipfile.py Mon May 02 18:49:30 2011 +0200 +++ b/Lib/test/test_zipfile.py Mon May 02 19:17:46 2011 +0200 @@ -359,6 +359,15 @@ with open(TESTFN, "rb") as f: self.assertEqual(zipfp.read(TESTFN), f.read()) + def test_writefp_default_name(self): + """Check that calling ZipFile.write without arcname specified + produces the expected result.""" + with zipfile.ZipFile(TESTFN2, "w") as zipfp: + fp = io.BytesIO("hello world".encode("ascii")) + zipfp.writefp("a.txt", fp) + with zipfile.ZipFile(TESTFN2, "r") as zipfp: + self.assertEqual(zipfp.read("a.txt"), b"hello world") + @skipUnless(zlib, "requires zlib") def test_per_file_compression(self): """Check that files within a Zip archive can have different diff -r 8270c5f59d26 Lib/zipfile.py --- a/Lib/zipfile.py Mon May 02 18:49:30 2011 +0200 +++ b/Lib/zipfile.py Mon May 02 19:17:46 2011 +0200 @@ -1189,6 +1189,42 @@ self.filelist.append(zinfo) self.NameToInfo[zinfo.filename] = zinfo + def writefp(self, zinfo_or_arcname, fp): + """Write a file into the archive. The contents is in 'fp' + which is a file object. + 'zinfo_or_arcname' is either a ZipInfo instance or the name of the + file in the archive. + Note: this always store file using ZIP_STORED compression method. + """ + if not isinstance(zinfo_or_arcname, ZipInfo): + zinfo = ZipInfo(filename=zinfo_or_arcname, + date_time=time.localtime(time.time())[:6]) + zinfo.compress_type = self.compression + zinfo.external_attr = 0o600 << 16 + else: + zinfo = zinfo_or_arcname + zinfo.header_offset = self.fp.tell() # Start of header bytes + zinfo.flag_bits = 0x08 + self._didModify = True + self.fp.write(zinfo.FileHeader()) + CRC = 0 + file_size = 0 + while 1: + buf = fp.read(1024 * 8) + if not buf: + break + file_size = file_size + len(buf) + CRC = crc32(buf, CRC) & 0xffffffff + self.fp.write(buf) + fp.close() + zinfo.file_size = file_size + zinfo.compress_size = file_size + zinfo.CRC = CRC & 0xffffffff # CRC-32 checksum + self.fp.write(struct.pack("