cpython: 7a47fd7f2fdc (original) (raw)
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -548,8 +548,6 @@ class TestsWithSourceFile(unittest.TestC ('/foo/bar', 'foo/bar'), ('/foo/../bar', 'foo/bar'), ('/foo/../../bar', 'foo/bar'),
('//foo/bar', 'foo/bar'),[](#l1.7)
('../../foo../../ba..r', 'foo../ba..r'),[](#l1.8) ][](#l1.9) if os.path.sep == '\\': # Windows.[](#l1.10) hacknames.extend([[](#l1.11)
@@ -571,19 +569,32 @@ class TestsWithSourceFile(unittest.TestC (r'\?\C:\foo\bar', 'foo/bar'), (r'C:/../C:/foo/bar', 'C_/foo/bar'), (r'a:b\ce|f"g?h*i', 'b/c_d_e_f_g_h_i'),
('../../foo../../ba..r', 'foo/ba..r'),[](#l1.16)
])[](#l1.17)
else: # Unix[](#l1.18)
hacknames.extend([[](#l1.19)
('//foo/bar', 'foo/bar'),[](#l1.20)
('../../foo../../ba..r', 'foo../ba..r'),[](#l1.21)
(r'foo/..\bar', r'foo/..\bar'),[](#l1.22) ])[](#l1.23)
for arcname, fixedname in hacknames: content = b'foobar' + arcname.encode() with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
zipfp.writestr(arcname, content)[](#l1.28)
zinfo = zipfile.ZipInfo()[](#l1.29)
# preserve backslashes[](#l1.30)
zinfo.filename = arcname[](#l1.31)
zinfo.external_attr = 0o600 << 16[](#l1.32)
zipfp.writestr(zinfo, content)[](#l1.33)
arcname = arcname.replace(os.sep, "/")[](#l1.35) targetpath = os.path.join('target', 'subdir', 'subsub')[](#l1.36) correctfile = os.path.join(targetpath, *fixedname.split('/'))[](#l1.37)
with zipfile.ZipFile(TESTFN2, 'r') as zipfp: writtenfile = zipfp.extract(arcname, targetpath)
self.assertEqual(writtenfile, correctfile)[](#l1.41)
self.assertEqual(writtenfile, correctfile,[](#l1.42)
msg="extract %r" % arcname)[](#l1.43) self.check_file(correctfile, content)[](#l1.44) shutil.rmtree('target')[](#l1.45)
@@ -596,7 +607,8 @@ class TestsWithSourceFile(unittest.TestC with zipfile.ZipFile(TESTFN2, 'r') as zipfp: writtenfile = zipfp.extract(arcname)
self.assertEqual(writtenfile, correctfile)[](#l1.51)
self.assertEqual(writtenfile, correctfile,[](#l1.52)
msg="extract %r" % arcname)[](#l1.53) self.check_file(correctfile, content)[](#l1.54) shutil.rmtree(fixedname.split('/')[0])[](#l1.55)
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1238,11 +1238,14 @@ class ZipFile: 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))
# filter illegal characters on Windows[](#l2.7) if os.path.sep == '\\':[](#l2.8)
# filter illegal characters on Windows[](#l2.9) illegal = ':<>|"?*'[](#l2.10) table = str.maketrans(illegal, '_' * len(illegal))[](#l2.11) arcname = arcname.translate(table)[](#l2.12)
# remove trailing dots[](#l2.13)
arcname = (x.rstrip('.') for x in arcname.split(os.path.sep))[](#l2.14)
arcname = os.path.sep.join(x for x in arcname if x)[](#l2.15)
targetpath = os.path.join(targetpath, arcname) targetpath = os.path.normpath(targetpath)