Skip to content

Commit

Permalink
Invent DirtyZipInfo to create an unsanitized zipfile with backslashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 26, 2024
1 parent 0a3a7b4 commit 7708409
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pickle
import stat
import sys
import time
import unittest
from zipp.compat.overlay import zipfile

Expand Down Expand Up @@ -627,7 +628,7 @@ def test_backslash_not_separator(self):
"""
data = io.BytesIO()
zf = zipfile.ZipFile(data, "w")
zf.writestr("foo\\bar", b"content")
zf.writestr(DirtyZipInfo.for_name("foo\\bar", zf), b"content")
zf.filename = ''
root = zipfile.Path(zf)
(first,) = root.iterdir()
Expand All @@ -640,3 +641,29 @@ def test_interface(self, alpharep):

zf = zipfile.Path(alpharep)
assert isinstance(zf, Traversable)


class DirtyZipInfo(zipfile.ZipInfo):
"""
Bypass name sanitization.
"""

def __init__(self, filename, *args, **kwargs):
super().__init__(filename, *args, **kwargs)
self.filename = filename

@classmethod
def for_name(cls, name, archive):
"""
Construct a ZipInfo the same way that ZipFile.writestr
does.
"""
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('/'):
self.external_attr = 0o40775 << 16 # drwxrwxr-x
self.external_attr |= 0x10 # MS-DOS directory flag
else:
self.external_attr = 0o600 << 16 # ?rw-------
return self

0 comments on commit 7708409

Please sign in to comment.