Bug report
Bug description:
When a ZIP archive is opened and rewritten in append mode, a member having a non-standard filename will be corrupted because its filename rewritten to the central directory is auto-sanitized and thus inconsistent with the immutable local file entry when loaded in the future.
Example:
import io
import zipfile
from unittest import mock
TESTFN = io.BytesIO()
with zipfile.ZipFile(TESTFN, mode="w") as zipfp:
zi = zipfile.ZipInfo('MyFolder/My\\File.txt')
# programatically set filename is not sanitized
zi.filename = 'MyFolder/My\\File.txt'
zipfp.writestr(zi, 'foo')
with zipfile.ZipFile(TESTFN, "a") as zipfp:
zipfp.comment = b''
with zipfile.ZipFile(TESTFN, "r") as zipfp:
zi = zipfp.infolist()[0]
print('content:', zipfp.read(zi))
The above code raises an error: zipfile.BadZipFile: File name in directory 'MyFolder/My/File.txt' and header b'MyFolder/My\\File.txt' differ.
CPython versions tested on:
3.16, 3.14
Operating systems tested on:
No response
Linked PRs
Bug report
Bug description:
When a ZIP archive is opened and rewritten in append mode, a member having a non-standard filename will be corrupted because its filename rewritten to the central directory is auto-sanitized and thus inconsistent with the immutable local file entry when loaded in the future.
Example:
The above code raises an error:
zipfile.BadZipFile: File name in directory 'MyFolder/My/File.txt' and header b'MyFolder/My\\File.txt' differ.CPython versions tested on:
3.16, 3.14
Operating systems tested on:
No response
Linked PRs