diff options
author | Bénédikt Tran <10796600+picnixz@users.noreply.github.com> | 2024-12-29 18:30:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-29 18:30:53 (GMT) |
commit | 7e819ce0f32068de7914cd1ba3b4b95e91ea9873 (patch) | |
tree | ee71bf301533e0f2396839052d79ac288d0a55ac /Lib/zipfile | |
parent | ffece5590e95e89d3b5b6847e3beb443ff65c3db (diff) | |
download | cpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.zip cpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.tar.gz cpython-7e819ce0f32068de7914cd1ba3b4b95e91ea9873.tar.bz2 |
gh-123424: add `ZipInfo._for_archive` to set suitable default properties (#123429)
---------
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Diffstat (limited to 'Lib/zipfile')
-rw-r--r-- | Lib/zipfile/__init__.py | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index f4d396a..052ef47 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -13,6 +13,7 @@ import struct import sys import threading import time +from typing import Self try: import zlib # We may need its compression method @@ -605,6 +606,24 @@ class ZipInfo: return zinfo + def _for_archive(self, archive: ZipFile) -> Self: + """Resolve suitable defaults from the archive. + + Resolve the date_time, compression attributes, and external attributes + to suitable defaults as used by :method:`ZipFile.writestr`. + + Return self. + """ + self.date_time = time.localtime(time.time())[:6] + self.compress_type = archive.compression + self.compress_level = archive.compresslevel + if self.filename.endswith('/'): # pragma: no cover + self.external_attr = 0o40775 << 16 # drwxrwxr-x + self.external_attr |= 0x10 # MS-DOS directory flag + else: + self.external_attr = 0o600 << 16 # ?rw------- + return self + def is_dir(self): """Return True if this archive member is a directory.""" if self.filename.endswith('/'): @@ -1908,18 +1927,10 @@ class ZipFile: the name of the file in the archive.""" if isinstance(data, str): data = data.encode("utf-8") - 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.compress_level = self.compresslevel - if zinfo.filename.endswith('/'): - zinfo.external_attr = 0o40775 << 16 # drwxrwxr-x - zinfo.external_attr |= 0x10 # MS-DOS directory flag - else: - zinfo.external_attr = 0o600 << 16 # ?rw------- - else: + if isinstance(zinfo_or_arcname, ZipInfo): zinfo = zinfo_or_arcname + else: + zinfo = ZipInfo(zinfo_or_arcname)._for_archive(self) if not self.fp: raise ValueError( |