Skip to content

Commit

Permalink
Fix Python 3.11 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
itziakos committed Oct 26, 2024
1 parent 6772185 commit 59732c6
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions zipfile2/_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,12 @@ def extractall(

def extract_to(self, member, destination, path=None, pwd=None,
preserve_permissions=PERMS_PRESERVE_NONE):

if not isinstance(member, zipfile.ZipInfo):
member = self.getinfo(member)

if path is None:
path = os.getcwd()

return self._extract_member_to(member, destination, path, pwd,
preserve_permissions)
return self._extract_member_to(
member, destination, path, pwd, preserve_permissions)

def write(
self, filename, arcname=None,
Expand Down Expand Up @@ -223,20 +221,14 @@ def _extract_member_to(self, member, arcname, targetpath, pwd,
# interpret absolute pathname as relative, remove drive letter or
# UNC path, redundant separators, "." and ".." components.
arcname = os.path.splitdrive(arcname)[1]
arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
if x not in ('', os.path.curdir,
os.path.pardir))
invalid_path_parts = ('', os.path.curdir, os.path.pardir)
arcname = os.path.sep.join(
x for x in arcname.split(os.path.sep)
if x not in invalid_path_parts)

if os.path.sep == '\\':
# filter illegal characters on Windows
illegal = ':<>|"?*'
if isinstance(arcname, str):
table = dict((ord(c), ord('_')) for c in illegal)
else:
table = string.maketrans(illegal, '_' * len(illegal))
arcname = arcname.translate(table)
# remove trailing dots
arcname = (x.rstrip('.') for x in arcname.split(os.path.sep))
arcname = os.path.sep.join(x for x in arcname if x)
arcname = self._sanitize_windows_name(arcname, os.path.sep)

targetpath = os.path.join(targetpath, arcname)
targetpath = os.path.normpath(targetpath)
Expand All @@ -246,7 +238,7 @@ def _extract_member_to(self, member, arcname, targetpath, pwd,
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)

if member.filename[-1] == '/':
if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
return targetpath
Expand Down

0 comments on commit 59732c6

Please sign in to comment.