Skip to content
Merged
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
60 changes: 60 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,66 @@ def test_empty_zipfile(self):
self.fail("Unable to create empty ZIP file in 'a' mode")
zipf.close()

def test_append_preserves_ascii_utf8_flag(self):
comments = (b'', 'caf\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8'))
updates = (
('append', lambda zf: zf.writestr('added.txt', b'added')),
('comment', lambda zf: setattr(zf, 'comment', b'archive comment')),
)
for comment, (operation, update) in itertools.product(comments, updates):
with self.subTest(comment=comment, operation=operation):
stream = io.BytesIO()
with zipfile.ZipFile(stream, 'w') as archive:
entry = zipfile.ZipInfo('file.txt')
entry.comment = comment
archive.writestr(entry, b'payload')
original = bytearray(stream.getvalue())
central_offset = original.index(b'PK\x01\x02')
struct.pack_into('<H', original, 6, 1 << 11)
struct.pack_into('<H', original, central_offset + 8, 1 << 11)
stream = io.BytesIO(original)
with zipfile.ZipFile(stream, 'a') as archive:
update(archive)
with zipfile.ZipFile(stream) as archive:
entry = archive.getinfo('file.txt')
self.assertEqual(entry.flag_bits, 1 << 11)
self.assertEqual(entry.comment, comment)
self.assertEqual(archive.read('file.txt'), b'payload')
self.assertIsNone(archive.testzip())
self.assertEqual(stream.getvalue()[:central_offset],
original[:central_offset])

def test_new_entry_filename_encoding_flags(self):
cases = (
('file.txt', b'\x82', 0, 'cp437', 0,
'\N{LATIN SMALL LETTER E WITH ACUTE}'),
('caf\N{LATIN SMALL LETTER E WITH ACUTE}.txt', b'\xc3\xa9',
0, 'utf-8', 1 << 11,
'\N{LATIN SMALL LETTER E WITH ACUTE}'),
('file.txt', b'\xc3\xa9', 1 << 11, 'utf-8', 1 << 11,
'\N{LATIN SMALL LETTER E WITH ACUTE}'),
('file.txt', b'', 1 << 11, 'utf-8', 1 << 11, ''),
('file.txt', b'ascii', 1 << 11, 'utf-8', 1 << 11, 'ascii'),
)
for (name, comment, initial_flags, encoding, expected_flags,
expected_comment) in cases:
with self.subTest(name=name, comment=comment,
initial_flags=initial_flags):
stream = io.BytesIO()
with zipfile.ZipFile(stream, 'w') as archive:
entry = zipfile.ZipInfo(name)
entry.flag_bits = initial_flags
entry.comment = comment
archive.writestr(entry, b'payload')
with zipfile.ZipFile(stream) as archive:
entry = archive.getinfo(name)
self.assertEqual(entry.flag_bits, expected_flags)
self.assertEqual(entry.comment, comment)
self.assertEqual(entry.comment.decode(encoding),
expected_comment)
self.assertEqual(archive.read(name), b'payload')
self.assertIsNone(archive.testzip())

def test_open_empty_file(self):
# Issue 1710703: Check that opening a file with less than 22 bytes
# raises a BadZipFile exception (rather than the previously unhelpful
Expand Down
8 changes: 5 additions & 3 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ def FileHeader(self, zip64=None):

def _encodeFilenameFlags(self):
if self.flag_bits & _MASK_UTF_FILENAME:
encoding = 'ascii'
encoding = 'utf-8'
else:
encoding = 'cp437'
try:
return self.filename.encode(encoding), self.flag_bits & ~_MASK_UTF_FILENAME
return self.filename.encode(encoding), self.flag_bits
except UnicodeEncodeError:
return self.filename.encode('utf-8'), self.flag_bits | _MASK_UTF_FILENAME

Expand Down Expand Up @@ -1842,7 +1842,9 @@ def _open_to_write(self, zinfo, force_zip64=False):
zinfo.compress_size = 0
zinfo.CRC = 0

zinfo.flag_bits = _MASK_UTF_FILENAME
zinfo.flag_bits &= _MASK_UTF_FILENAME
if not zinfo.filename.isascii():
zinfo.flag_bits |= _MASK_UTF_FILENAME
if zinfo.compress_type == ZIP_LZMA:
# Compressed data includes an end-of-stream (EOS) marker
zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`zipfile` dropping the UTF-8 (EFS) flag for existing entries
when rewriting an archive in append mode, and honor an explicitly set
flag on new entries. Patch by Kasra Ghodsi.
Loading