Repository navigation
Description
file_names() and by_index(...).name() decode a CP437 member name to café.txt, but passing that decoded name to by_name returns FileNotFound and index_for_name returns None.
Reproduced independently with the published 8.6.0 and 9.0.4 sources on Linux amd64 / Rust 1.96.0. This is present in both versions, not a ZIP9-only regression. For each version, six focused tests execute: ASCII lookup, UTF-8 lookup, CP437 decoded enumeration and CP437 indexed payload reading pass; CP437 decoded-name lookup and name-to-index lookup fail.
The fixture below is authored synthetic data: one Stored member containing synthetic-content\n. Its local and central filename bytes are caf\x82.txt, with general-purpose bit 11 clear in both headers (both flags are 0). Python zipfile lists café.txt and reads the exact payload by that name. No third-party document is involved.
Minimal reproduction with zip = { version = "=9.0.4", default-features = false }:
use std::io::{Cursor, Read};
use zip::ZipArchive;
const CP437: &[u8] = &[
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x93, 0x50, 0x5d, 0x4f, 0x3f,
0xf6, 0xc5, 0x12, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x63, 0x61,
0x66, 0x82, 0x2e, 0x74, 0x78, 0x74, 0x73, 0x79, 0x6e, 0x74, 0x68, 0x65, 0x74, 0x69, 0x63, 0x2d,
0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00,
0x00, 0x00, 0x00, 0x00, 0xe8, 0x93, 0x50, 0x5d, 0x4f, 0x3f, 0xf6, 0xc5, 0x12, 0x00, 0x00, 0x00,
0x12, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x63, 0x61, 0x66, 0x82, 0x2e, 0x74, 0x78, 0x74, 0x50, 0x4b,
0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x36, 0x00, 0x00, 0x00, 0x38, 0x00,
0x00, 0x00, 0x00, 0x00,
];
#[test]
fn should_read_cp437_member_by_decoded_name() -> Result<(), Box<dyn std::error::Error>> {
let mut archive = ZipArchive::new(Cursor::new(CP437))?;
let mut indexed = String::new();
archive.by_index(0)?.read_to_string(&mut indexed)?;
assert_eq!(indexed, "synthetic-content\n");
let mut named = String::new();
archive.by_name("café.txt")?.read_to_string(&mut named)?;
assert_eq!(named, indexed);
Ok(())
}
#[test]
fn should_resolve_cp437_decoded_name_to_index() -> Result<(), Box<dyn std::error::Error>> {
let archive = ZipArchive::new(Cursor::new(CP437))?;
assert_eq!(archive.index_for_name("café.txt"), Some(0));
Ok(())
}Actual results: the first test returns Error: FileNotFound; the second reports left: None, right: Some(0).
In 9.0.4 (and current repository source), file_names decodes names via data.name(file_name_raw), while index_for_name and by_name_with_optional_password look up name.as_bytes() in the raw-byte map. UTF-8 bytes for é differ from CP437 0x82, so the decoded name cannot round-trip. This also breaks a spreadsheet reader that enumerates decoded ZIP paths and subsequently opens those paths by name.
Expected: decoded names exposed by the reader can be used with its string lookup APIs, preserving existing exact raw-name lookup precedence. An O(1) raw-key fast path followed by a decoded-name fallback on misses may address this without affecting normal successful lookups.
Activity
Its-Just-Nans commentedon Oct 16, 2026
Hello
Could you give an example of the
zipusage you're trying to do ? It looks like your are trying to access the files from there file names asUTF-8?Something like
Is there any specific reason to loop over the
UTF-8file_names() ? Have you tried to do something like that:Goldziher commentedon Oct 17, 2026
Yes, that is the round-trip that fails: the string comes directly from
file_names(), without us re-decoding or changing its encoding.by_indexworks; it is already a passing control in the reproduction. The problem is thatfile_names()andby_index(0)?.name()exposecafé.txt, whileby_name("café.txt")fails andindex_for_name("café.txt")returnsNonefor the same entry. The archive stores CP437caf\x82.txtwith the UTF-8 flag clear. We are not expecting those raw bytes to be UTF-8; we are using the decoded name returned by the reader itself.The downstream use case is a spreadsheet/package reader that enumerates member paths and later opens members by their logical names. Named lookup is also needed when a package references another member by path without supplying a central-directory index. Iterating by index can work around enumeration-only cases, but requires a separate decoded-name index for later named access. Should the string lookup APIs accept the same decoded names that the string enumeration API returns? Preserving exact raw-name lookup precedence and falling back to decoded-name matching only on a miss would keep existing successful lookups unchanged.
The proposed change and regression coverage are in #1085.
Its-Just-Nans commentedon Oct 17, 2026
On my side I'm not a big fan of the "magic" fallback to cp437 for the hookup and would prefer that library users know that
by_name()is directly using the&[u8]representation. Also starting from9.xthefile_names()method return type is a breaking change, and this breaking change could allow/force library user to understand that thefile_names()method may not be the correct one to use (based on their usage).Nevertheless I agree the "magic" fallback is interesting
Do you have an example for that ? You can use
index_for_name()for this case no ?Goldziher commentedon Oct 17, 2026
index_for_name()uses the samename.as_bytes()lookup, so it also returnsNonefor"café.txt"in the reproduction: the stored bytes are CP437caf\x82.txt, whereas that decoded string supplies UTF-8 bytes. Maintaining our own decoded-name-to-index map is a workable alternative.A concrete named-reference example is Calamine's XLSX reader: it reads relationship
Targetpaths, resolves the worksheet path, and opens the referenced member withby_name(). For example,Target="worksheets/sheet1.xml"supplies a path without a directory index. This illustrates the access pattern; the CP437 failure remains the synthetic ZIP reproduction, not an additional customer XLSX claim.If retaining raw-only semantics is preferable, would an explicitly decoded-name lookup API be a better direction for #1085? I can adapt the proposal to that instead of an implicit fallback.
Its-Just-Nans commentedon Oct 17, 2026
Yes I understand the lookup problem, but I struggle to understand: if the stored bytes are CP437
caf\x82.txt, how do you ends up withcafé.txt(UTF-8), where does thecafé.txtcomes from ?Does that mean the reads relationship Target paths is encoded as
café.txt?I think the fallback in #1085 is ok, if we go with this option
Goldziher commentedon Oct 17, 2026
The reproduction verifies that ZIP’s own
file_names()API yields the decoded"café.txt"(inside itsZipResult<Cow<str>>).by_index(0)?.name()?.into_owned()gives the same Unicode filename. The entry stores CP437caf\x82.txt; decoding produces Unicodeé, and Rust represents thatStringas UTF-8. Passing it back toby_name()therefore supplies different bytes from the raw-name index. This is a decoded-name round-trip problem even without XML.A relationship
Targetis independently decoded XML text, so a non-ASCII target would likewise reach Calamine as a Unicode string. The linked Calamine code illustrates that named-reference pattern; I have not established a real XLSX with this particular CP437/Target combination. I’ll retain the fallback proposal in #1085, including exact raw-match precedence and rejection of ambiguous decoded matches.Its-Just-Nans commentedon Oct 17, 2026
Okay, I understand now if you are using
by_index(0)?.name()?.into_owned()yes, it will give a UTF-8 variable.Note that 9.x introduces a breaking change on the
name()(Now a result) and introduce alsoname_raw(). It's also made for performance purpose:name()needs a decoding. Sometimes you don't need the decoding or can lazy decode the name.Thanks for the information!
fix: resolve decoded CP437 member names (zip-rs#1080) (zip-rs#1085)