Break up large function into two functions

This commit is contained in:
Kovid Goyal 2025-04-07 08:32:50 +05:30
parent d7d5144790
commit a5f4f19887
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1118,10 +1118,7 @@ class ZipFile:
for zipinfo in zimembers: for zipinfo in zimembers:
self._extract_member(zipinfo, path, pwd) self._extract_member(zipinfo, path, pwd)
def _extract_member(self, member, targetpath, pwd): def _get_targetpath(self, member: ZipInfo, targetpath: str) -> str:
'''Extract the ZipInfo object 'member' to a physical
file on the path targetpath.
'''
# build the destination pathname, replacing # build the destination pathname, replacing
# forward slashes to platform specific separators. # forward slashes to platform specific separators.
# Strip trailing path separator, unless it represents the root. # Strip trailing path separator, unless it represents the root.
@ -1151,18 +1148,25 @@ class ZipFile:
try: try:
os.makedirs(upperdirs) os.makedirs(upperdirs)
except OSError: # Added by Kovid except OSError: # Added by Kovid
targetpath = os.path.join(base_target,
sanitize_file_name(fname))
upperdirs = os.path.dirname(targetpath) upperdirs = os.path.dirname(targetpath)
targetpath = os.path.join(upperdirs, sanitize_file_name(fname))
if upperdirs and not os.path.exists(upperdirs): if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs) os.makedirs(upperdirs)
return targetpath
def _extract_member(self, member, targetpath, pwd):
return self._extract_member_to(member, self._get_targetpath(member, targetpath), pwd)
def _extract_member_to(self, member, targetpath, pwd):
'''Extract the ZipInfo object 'member' to a physical
file on the path targetpath.
'''
if member.filename[-1] == '/': if member.filename[-1] == '/':
if not os.path.isdir(targetpath): if not os.path.isdir(targetpath):
try: try:
os.mkdir(targetpath) os.mkdir(targetpath)
except OSError: # Added by Kovid except OSError: # Added by Kovid
targetpath = os.path.join(base_target, sanitize_file_name(fname)) targetpath = os.path.join(os.path.dirname(targetpath), sanitize_file_name(os.path.basename(targetpath)))
os.mkdir(targetpath) os.mkdir(targetpath)
return targetpath return targetpath
@ -1172,9 +1176,7 @@ class ZipFile:
try: try:
target = open(targetpath, 'wb') target = open(targetpath, 'wb')
except OSError: except OSError:
components = list(os.path.split(targetpath)) targetpath = os.path.join(os.path.dirname(targetpath), sanitize_file_name(os.path.basename(targetpath)))
components[-1] = sanitize_file_name(components[-1])
targetpath = os.sep.join(components)
target = open(targetpath, 'wb') target = open(targetpath, 'wb')
with target, closing(self.open(member, pwd=pwd)) as source: with target, closing(self.open(member, pwd=pwd)) as source: