Skip to content

Commit

Permalink
Fix LeanZipFile support for Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
itziakos committed Aug 17, 2024
1 parent 2419e64 commit d094509
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions zipfile2/_lean_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
stringFileHeader,
structCentralDir,
structFileHeader,
crc32
)

from .common import TooManyFiles
from .common import TooManyFiles, PY312

_UTF8_EXTENSION_FLAG = 0x800

Expand Down Expand Up @@ -121,6 +122,7 @@ def get_zip_infos(self, *filenames):
if centdir[_CD_SIGNATURE] != stringCentralDir:
raise BadZipFile("Bad magic number for central directory")
filename = fp.read(centdir[_CD_FILENAME_LENGTH])
orig_filename_crc = crc32(filename)
flags = centdir[5]
if flags & _UTF8_EXTENSION_FLAG:
# UTF-8 file names extension
Expand All @@ -144,8 +146,10 @@ def get_zip_infos(self, *filenames):
x._raw_time = t
x.date_time = ((d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F,
t >> 11, (t >> 5) & 0x3F, (t & 0x1F) * 2)

x._decodeExtra()
if PY312:
x._decodeExtra(orig_filename_crc)
else:
x._decodeExtra()
x.header_offset = x.header_offset + concat

# update total bytes read from central directory
Expand Down
3 changes: 3 additions & 0 deletions zipfile2/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import zipfile
import platform

PYTHON_VERSION = tuple(map(int, platform.python_version_tuple()))
PY312 = PYTHON_VERSION >= (3, 12, 0)

class TooManyFiles(zipfile.BadZipfile):
pass

0 comments on commit d094509

Please sign in to comment.