mirror of
https://github.com/python/cpython.git
synced 2025-07-30 22:54:16 +00:00
Issue 6003: ZipFile.writestr "compression_type" argument
This commit is contained in:
parent
2bd52dcccb
commit
dd25e86cf4
4 changed files with 30 additions and 2 deletions
|
@ -1063,13 +1063,14 @@ class ZipFile:
|
|||
self.filelist.append(zinfo)
|
||||
self.NameToInfo[zinfo.filename] = zinfo
|
||||
|
||||
def writestr(self, zinfo_or_arcname, bytes):
|
||||
def writestr(self, zinfo_or_arcname, bytes, compress_type=None):
|
||||
"""Write a file into the archive. The contents is the string
|
||||
'bytes'. 'zinfo_or_arcname' is either a ZipInfo instance or
|
||||
the name of the file in the archive."""
|
||||
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 = 0600 << 16
|
||||
else:
|
||||
|
@ -1079,6 +1080,9 @@ class ZipFile:
|
|||
raise RuntimeError(
|
||||
"Attempt to write to ZIP archive that was already closed")
|
||||
|
||||
if compress_type is not None:
|
||||
zinfo.compress_type = compress_type
|
||||
|
||||
zinfo.file_size = len(bytes) # Uncompressed size
|
||||
zinfo.header_offset = self.fp.tell() # Start of header bytes
|
||||
self._writecheck(zinfo)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue