Skip to content
Closed
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
24 changes: 24 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,30 @@ def test_from_dir(self):
self.assertEqual(zi.file_size, 0)


class ZipInfoWithExtraTest(unittest.TestCase):
path = pathlib.Path(TESTFN2)
def setUp(self):
os.mkdir(ZipInfoWithExtraTest.path)

def tearDown(self):
rmtree(ZipInfoWithExtraTest.path)

@requires_zlib
def test_extra_field(self):

f = ZipInfoWithExtraTest.path / "f.zip"
with zipfile.ZipFile(f, 'w') as zf:
zi = zipfile.ZipInfo("0")
zi.extra = b"12345"
zf.writestr(zi, b"some string")
try:
with zipfile.ZipFile(f) as zf:
infolist = zf.infolist()
self.assertTrue(len(infolist) == 1)
self.assertEqual(infolist[0].extra, b"12345")
except zipfile.BadZipfile as e:
self.fail(f"Unexpected exception: {e}")

class CommandLineTest(unittest.TestCase):

def zipfilecmd(self, *args, **kwargs):
Expand Down
6 changes: 4 additions & 2 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,8 @@ def _RealGetContents(self):

# "concat" is zero, unless zip was concatenated to another file
concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
if endrec[_ECD_SIGNATURE] == stringEndArchive64:
hasZip64ECD = endrec[_ECD_SIGNATURE] == stringEndArchive64
if hasZip64ECD:
# If Zip64 extension structures are present, account for them
concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)

Expand Down Expand Up @@ -1324,7 +1325,8 @@ def _RealGetContents(self):
x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )

x._decodeExtra()
if hasZip64ECD:
x._decodeExtra()
x.header_offset = x.header_offset + concat
self.filelist.append(x)
self.NameToInfo[x.filename] = x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Decode extra data only if Zip64 End of central directory record is present