Skip to content

Decoded CP437 names cannot round-trip through by_name or index_for_name #1080

Closed

Description

@Goldziher

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

  1. Its-Just-Nans commented on Oct 16, 2026

    @Its-Just-Nans
    Member

    Hello

    Could you give an example of the zip usage you're trying to do ? It looks like your are trying to access the files from there file names as UTF-8 ?

    Something like

    let file_names = archive
      .file_names()
      .map(|n| n.unwrap().to_string())
      .collect::<Vec<String>>();
    for one_name in file_names {
      let file = archive.by_name(&one_name).unwrap();
      eprintln!("{:x?}", file);
    }

    Is there any specific reason to loop over the UTF-8 file_names() ? Have you tried to do something like that:

    for index in 0..archive.len() {
        let file = archive.by_index(index).unwrap();
        eprintln!("{:x?}", file);
    }
  2. Goldziher commented on Oct 17, 2026

    @Goldziher
    ContributorAuthor

    Yes, that is the round-trip that fails: the string comes directly from file_names(), without us re-decoding or changing its encoding.

    let names = archive
        .file_names()
        .map(|name| name.map(|name| name.into_owned()))
        .collect::<Result<Vec<_>, _>>()?;
    for name in names {
        let file = archive.by_name(&name)?;
    }

    by_index works; it is already a passing control in the reproduction. The problem is that file_names() and by_index(0)?.name() expose café.txt, while by_name("café.txt") fails and index_for_name("café.txt") returns None for the same entry. The archive stores CP437 caf\x82.txt with 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.

  3. Its-Just-Nans commented on Oct 17, 2026

    @Its-Just-Nans
    Member

    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 from 9.x the file_names() method return type is a breaking change, and this breaking change could allow/force library user to understand that the file_names() method may not be the correct one to use (based on their usage).

    Nevertheless I agree the "magic" fallback is interesting

    Named lookup is also needed when a package references another member by path without supplying a central-directory index

    Do you have an example for that ? You can use index_for_name() for this case no ?

  4. Goldziher commented on Oct 17, 2026

    @Goldziher
    ContributorAuthor

    index_for_name() uses the same name.as_bytes() lookup, so it also returns None for "café.txt" in the reproduction: the stored bytes are CP437 caf\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 Target paths, resolves the worksheet path, and opens the referenced member with by_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.

  5. Its-Just-Nans commented on Oct 17, 2026

    @Its-Just-Nans
    Member

    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 with café.txt (UTF-8), where does the café.txt comes 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

  6. Goldziher commented on Oct 17, 2026

    @Goldziher
    ContributorAuthor

    The reproduction verifies that ZIP’s own file_names() API yields the decoded "café.txt" (inside its ZipResult<Cow<str>>). by_index(0)?.name()?.into_owned() gives the same Unicode filename. The entry stores CP437 caf\x82.txt; decoding produces Unicode é, and Rust represents that String as UTF-8. Passing it back to by_name() therefore supplies different bytes from the raw-name index. This is a decoded-name round-trip problem even without XML.

    A relationship Target is 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.

  7. Its-Just-Nans commented on Oct 17, 2026

    @Its-Just-Nans
    Member

    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 also name_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!

  8. added this to the 9.1.0 milestone on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions