Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-41928: Add support for Unicode Path Extra Field in ZipFile #23736

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

Closed
wants to merge 6 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ def _EndRecData(fpin):
# Unable to find a valid end of central directory structure
return None

def _sanitize_filename(filename):
"""Terminate the file name at the first null byte and
ensure paths always use forward slashes as the directory separator."""

# Terminate the file name at the first null byte. Null bytes in file
# names are used as tricks by viruses in archives.
null_byte = filename.find(chr(0))
if null_byte >= 0:
filename = filename[0:null_byte]
# This is used to ensure paths in generated ZIP files always use
# forward slashes as the directory separator, as required by the
# ZIP format specification.
if os.sep != "/" and os.sep in filename:
filename = filename.replace(os.sep, "/")
return filename


class ZipInfo (object):
"""Class with attributes describing each file in the ZIP archive."""
Expand All @@ -336,6 +352,7 @@ class ZipInfo (object):
'external_attr',
'header_offset',
'CRC',
'orig_filename_crc',
'compress_size',
'file_size',
'_raw_time',
Expand All @@ -344,16 +361,9 @@ class ZipInfo (object):
def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
self.orig_filename = filename # Original file name in archive

# Terminate the file name at the first null byte. Null bytes in file
# names are used as tricks by viruses in archives.
null_byte = filename.find(chr(0))
if null_byte >= 0:
filename = filename[0:null_byte]
# This is used to ensure paths in generated ZIP files always use
# forward slashes as the directory separator, as required by the
# ZIP format specification.
if os.sep != "/" and os.sep in filename:
filename = filename.replace(os.sep, "/")
# Terminate the file name at the first null byte and
# ensure paths always use forward slashes as the directory separator.
filename = _sanitize_filename(filename)

self.filename = filename # Normalized file name
self.date_time = date_time # year, month, day, hour, min, sec
Expand Down Expand Up @@ -484,6 +494,24 @@ def _decodeExtra(self):
except struct.error:
raise BadZipFile(f"Corrupt zip64 extra field. "
f"{field} not found.") from None
elif tp == 0x7075:
data = extra[4:ln+4]
# Unicode Path Extra Field
try:
up_version, up_name_crc = unpack('<BL', data[:5])
if up_version == 1 and up_name_crc == self.orig_filename_crc:
up_unicode_name = data[5:].decode('utf-8')
if up_unicode_name:
self.filename = _sanitize_filename(up_unicode_name)
else:
import warnings
warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
except struct.error:
import warnings
warnings.warn("Corrupt unicode path extra field (0x7075)", stacklevel=2)
except UnicodeDecodeError:
import warnings
warnings.warn('Corrupt unicode path extra field (0x7075): invalid utf-8 bytes', stacklevel=2)

extra = extra[ln+4:]

Expand Down Expand Up @@ -1354,6 +1382,7 @@ def _RealGetContents(self):
if self.debug > 2:
print(centdir)
filename = fp.read(centdir[_CD_FILENAME_LENGTH])
orig_filename_crc = crc32(filename)
flags = centdir[5]
if flags & 0x800:
# UTF-8 file names extension
Expand All @@ -1378,6 +1407,7 @@ def _RealGetContents(self):
x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )

x.orig_filename_crc = orig_filename_crc
x._decodeExtra()
x.header_offset = x.header_offset + concat
self.filelist.append(x)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for Unicode Path Extra Field in ZipFile.