| 1 | From 5ea38d6eaaa5c0d452b5dd73699486f79641b870 Mon Sep 17 00:00:00 2001
|
| 2 | From: Ioannis Tziakos <[email protected]>
|
| 3 | Date: Sat, 17 Aug 2024 12:18:04 +0100
|
| 4 | Subject: [PATCH] Fix LeanZipFile support for Python 3.12
|
| 5 |
|
| 6 | ---
|
| 7 | zipfile2/_lean_zipfile.py | 10 +++++++---
|
| 8 | zipfile2/common.py | 3 +++
|
| 9 | 2 files changed, 10 insertions(+), 3 deletions(-)
|
| 10 |
|
| 11 | diff --git a/zipfile2/_lean_zipfile.py b/zipfile2/_lean_zipfile.py
|
| 12 | index 0a6e1c4..e9af3bd 100644
|
| 13 | |
| 14 | |
| 15 | @@ -62,9 +62,10 @@
|
| 16 | stringFileHeader,
|
| 17 | structCentralDir,
|
| 18 | structFileHeader,
|
| 19 | + crc32
|
| 20 | )
|
| 21 |
|
| 22 | -from .common import TooManyFiles
|
| 23 | +from .common import TooManyFiles, PY312
|
| 24 |
|
| 25 | _UTF8_EXTENSION_FLAG = 0x800
|
| 26 |
|
| 27 | @@ -164,6 +165,7 @@ def get_zip_infos(self, *filenames):
|
| 28 | if centdir[_CD_SIGNATURE] != stringCentralDir:
|
| 29 | raise BadZipFile("Bad magic number for central directory")
|
| 30 | filename = fp.read(centdir[_CD_FILENAME_LENGTH])
|
| 31 | + orig_filename_crc = crc32(filename)
|
| 32 | flags = centdir[5]
|
| 33 | if flags & _UTF8_EXTENSION_FLAG:
|
| 34 | # UTF-8 file names extension
|
| 35 | @@ -187,8 +189,10 @@ def get_zip_infos(self, *filenames):
|
| 36 | x._raw_time = t
|
| 37 | x.date_time = ((d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F,
|
| 38 | t >> 11, (t >> 5) & 0x3F, (t & 0x1F) * 2)
|
| 39 | -
|
| 40 | - x._decodeExtra()
|
| 41 | + if PY312:
|
| 42 | + x._decodeExtra(orig_filename_crc)
|
| 43 | + else:
|
| 44 | + x._decodeExtra()
|
| 45 | x.header_offset = x.header_offset + concat
|
| 46 |
|
| 47 | # update total bytes read from central directory
|
| 48 | diff --git a/zipfile2/common.py b/zipfile2/common.py
|
| 49 | index 7dbaea0..a25645a 100644
|
| 50 | |
| 51 | |
| 52 | @@ -1,5 +1,8 @@
|
| 53 | import zipfile
|
| 54 | +import platform
|
| 55 |
|
| 56 | +PYTHON_VERSION = tuple(map(int, platform.python_version_tuple()))
|
| 57 | +PY312 = PYTHON_VERSION >= (3, 12, 0)
|
| 58 |
|
| 59 | class TooManyFiles(zipfile.BadZipfile):
|
| 60 | pass
|