Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-34606: decode extra data only if zip64 end of central directory record is present #9107

Closed
wants to merge 3 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_zipfile.py
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
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
@@ -0,0 +1 @@
Decode extra data only if Zip64 End of central directory record is present