gh-123424: add ZipInfo._for_archive to set suitable default properties (#123429)

---------

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
Bénédikt Tran 2024-12-29 19:30:53 +01:00 committed by GitHub
parent ffece5590e
commit 7e819ce0f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 72 additions and 29 deletions

View file

@ -634,7 +634,7 @@ class TestPath(unittest.TestCase):
"""
data = io.BytesIO()
zf = zipfile.ZipFile(data, "w")
zf.writestr(DirtyZipInfo.for_name("foo\\bar", zf), b"content")
zf.writestr(DirtyZipInfo("foo\\bar")._for_archive(zf), b"content")
zf.filename = ''
root = zipfile.Path(zf)
(first,) = root.iterdir()
@ -657,20 +657,3 @@ class DirtyZipInfo(zipfile.ZipInfo):
def __init__(self, filename, *args, **kwargs):
super().__init__(filename, *args, **kwargs)
self.filename = filename
@classmethod
def for_name(cls, name, archive):
"""
Construct the same way that ZipFile.writestr does.
TODO: extract this functionality and re-use
"""
self = cls(filename=name, 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