You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
zipfile append corrupts a file due to auto filename sanitization #153625
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.
This can easily happen if a member containing \ is added in POSIX and the archive is then appended in Windows.
For example:
importioimportzipfilefromunittestimportmockTESTFN=io.BytesIO()
# A file written in POSIX allows '\\'withmock.patch('os.sep', '/'), mock.patch('os.altsep', None), \
zipfile.ZipFile(TESTFN, mode="w") aszipfp:
zi=zipfile.ZipInfo('MyFolder/My\\File.txt')
zipfp.writestr(zi, 'foo')
# A file written in Windows has '\\' replaced with '/'withmock.patch('os.sep', '\\'), mock.patch('os.altsep', '/'), \
zipfile.ZipFile(TESTFN, "a") aszipfp:
# trigger archive rewritingzipfp.comment=b''withzipfile.ZipFile(TESTFN, "r") aszipfp:
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.
Activity