Skip to content

Commit c8a6b24

Browse files
author
Sebastian Gassner
committed
fixing #111791: delegating extraction in shutil.unpack_archive to zipfile
shutil.unpack_archive fails, if file name contains '..'; zipfile handles everything correctly, i.e. in the same way than 'unzip'; let zipfile unpack archives, instead of reinventing the wheel here
1 parent d4426e8 commit c8a6b24

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

Lib/shutil.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,27 +1252,10 @@ def _unpack_zipfile(filename, extract_dir):
12521252
if not zipfile.is_zipfile(filename):
12531253
raise ReadError("%s is not a zip file" % filename)
12541254

1255-
zip = zipfile.ZipFile(filename)
1256-
try:
1257-
for info in zip.infolist():
1258-
name = info.filename
1259-
1260-
# don't extract absolute paths or ones with .. in them
1261-
if name.startswith('/') or '..' in name:
1262-
continue
1263-
1264-
targetpath = os.path.join(extract_dir, *name.split('/'))
1265-
if not targetpath:
1266-
continue
1267-
1268-
_ensure_directory(targetpath)
1269-
if not name.endswith('/'):
1270-
# file
1271-
with zip.open(name, 'r') as source, \
1272-
open(targetpath, 'wb') as target:
1273-
copyfileobj(source, target)
1274-
finally:
1275-
zip.close()
1255+
with zipfile.ZipFile(filename) as zf:
1256+
# delegate extracting the archive to zipfile
1257+
_ensure_directory(extract_dir)
1258+
zf.extractall(extract_dir)
12761259

12771260
def _unpack_tarfile(filename, extract_dir, *, filter=None):
12781261
"""Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`

0 commit comments

Comments
 (0)