Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,16 @@ def test_decompress_without_3rd_party_library(self):
with zipfile.ZipFile(zip_file) as zf:
self.assertRaises(RuntimeError, zf.extract, 'a.txt')

def test_reading_archive_member_starts_with_slash(self):
member_path = '/folder/file.txt'
member_data = b'hello world'
with open(TESTFN, "wb") as f:
f.write(member_data)
with zipfile.ZipFile(TESTFN2, 'w') as zf:
zf.write(TESTFN, member_path)
with zipfile.ZipFile(TESTFN2) as zf:
self.assertEqual(zf.read(member_path), member_data)

def tearDown(self):
unlink(TESTFN)
unlink(TESTFN2)
Expand Down
5 changes: 4 additions & 1 deletion Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,10 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True):
if arcname is None:
arcname = filename
arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
while arcname[0] in (os.sep, os.altsep):
while (arcname[0] in (os.sep, os.altsep) and
arcname[1] in (os.sep, os.altsep)):
arcname = arcname[1:]
if arcname[0] in (os.sep, os.altsep) and os.sep not in arcname[1:]:
arcname = arcname[1:]
if isdir:
arcname += '/'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix unnecessary removing of a leading slash in :meth:`zipfile.ZipInfo.from_file`
when member's archive name is in the form of `/directory/filename`.