Skip to content

Fix length calculation when adding extended ascii filenames - #32

Merged
pR0Ps merged 1 commit into
masterfrom
bugfix/length-non-asci-names
Aug 20, 2026
Merged

pR0Ps merged 1 commit into
masterfrom
bugfix/length-non-asci-names

Conversation

@pR0Ps

@pR0Ps pR0Ps commented Aug 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #31

@pR0Ps
pR0Ps force-pushed the bugfix/length-non-asci-names branch from e496c79 to 8814cda Compare August 20, 2026 03:29
python/cpython#150091 changed the default non-utf8 encoding from 'ascii'
to 'cp437' for encoding zip filenames. This means that characters that
could be encoded in cp437 but not ascii would be encoded in the zip data
using cp437 vs. previously they woud've used utf-8.

Note that non-ascii characters that are valid cp437 characters will
always take up a single byte when encoded as cp437, but multiple when
encoded using utf-8.

Because `len(zipstream)` assumes that filenames are encoded using only
ascii or utf-8, this resulted in the calculated length being off by (at
least) 1 byte for every cp437, non-ascii character in all the filenames.

The fix in this commit is to override `ZipInfo._encodeFilenameFlags` to
revert Python's change and force filenames to only use ascii/utf-8.
The change was made to support modifying existing zips with
cp437-encoded filenames which isn't relevant to this project. And while
technically cp437 is the standard encoding for zipfile names, using the
more strict ascii subset is less likely to be misinterpreted by
unarchivers since they won't have to guess at what extended ascii
encoding was used - it'll always be strict ascii or utf-8.
@pR0Ps
pR0Ps force-pushed the bugfix/length-non-asci-names branch from 8814cda to 012acbd Compare August 20, 2026 03:38
@pR0Ps
pR0Ps merged commit a97bee5 into master Aug 20, 2026
25 checks passed
@pR0Ps
pR0Ps deleted the bugfix/length-non-asci-names branch August 20, 2026 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sized ZipStream over-estimates length for non-ASCII member names

1 participant