Skip to content

Commit 4882320

Browse files
committed
Extract reusable _unpack_zipfile_obj from archive_utils
1 parent 069735f commit 4882320

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

setuptools/archive_util.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,37 @@ def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
100100
raise UnrecognizedFormat("%s is not a zip file" % (filename,))
101101

102102
with zipfile.ZipFile(filename) as z:
103-
for info in z.infolist():
104-
name = info.filename
103+
_unpack_zipfile_obj(z, extract_dir, progress_filter)
105104

106-
# don't extract absolute paths or ones with .. in them
107-
if name.startswith('/') or '..' in name.split('/'):
108-
continue
109105

110-
target = os.path.join(extract_dir, *name.split('/'))
111-
target = progress_filter(name, target)
112-
if not target:
113-
continue
114-
if name.endswith('/'):
115-
# directory
116-
ensure_directory(target)
117-
else:
118-
# file
119-
ensure_directory(target)
120-
data = z.read(info.filename)
121-
with open(target, 'wb') as f:
122-
f.write(data)
123-
unix_attributes = info.external_attr >> 16
124-
if unix_attributes:
125-
os.chmod(target, unix_attributes)
106+
def _unpack_zipfile_obj(zipfile_obj, extract_dir, progress_filter=default_filter):
107+
"""Internal/private API used by other parts of setuptools.
108+
Similar to ``unpack_zipfile``, but receives an already opened :obj:`zipfile.ZipFile`
109+
object instead of a filename.
110+
"""
111+
for info in zipfile_obj.infolist():
112+
name = info.filename
113+
114+
# don't extract absolute paths or ones with .. in them
115+
if name.startswith('/') or '..' in name.split('/'):
116+
continue
117+
118+
target = os.path.join(extract_dir, *name.split('/'))
119+
target = progress_filter(name, target)
120+
if not target:
121+
continue
122+
if name.endswith('/'):
123+
# directory
124+
ensure_directory(target)
125+
else:
126+
# file
127+
ensure_directory(target)
128+
data = zipfile_obj.read(info.filename)
129+
with open(target, 'wb') as f:
130+
f.write(data)
131+
unix_attributes = info.external_attr >> 16
132+
if unix_attributes:
133+
os.chmod(target, unix_attributes)
126134

127135

128136
def _resolve_tar_file_or_dir(tar_obj, tar_member_obj):

0 commit comments

Comments
 (0)