Repository navigation
Read archives that aren't in memory, a byte range at a time - #10
Conversation
ZipReader takes the whole archive as one slice, so a 60 GB archive on a server meant 60 GB in memory. ZipIndex opens an archive of a given length through reads of byte ranges: the end of the archive (one read of up to 65 KB, which also holds the directory when it is small), then the central directory in reads of at most 16 MiB. An entry is read on its own, its local header and then the entry, and a password check takes two small reads. ZipIndex::open and fetch_entry take a blocking read(offset, buf); ZipIndexOpener, header_range, entry_range and password_check_range do the same one step at a time for asynchronous callers. The opener follows ZipReader::new step by step and entry reads share its code, so a new differential test holds the two readers to the same entries, comment, errors, entry bytes and password checks on every fixture (plain, ZIP64, stub-prefixed, ZipCrypto, AES), every truncation and every single-byte corruption, and the fuzz target asserts the same. A 50 GiB test archive opens with one 65 KB read. The test writer gains ZipCrypto, AES-256 and ZIP64 end records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1777d299fa
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| pub fn step(&self) -> Result<OpenStep> { | ||
| match self.open() { |
There was a problem hiding this comment.
Resume directory parsing instead of restarting on every step
For central directories larger than the 16 MiB read limit, every call to step() reruns open() from the beginning, creating a fresh entries vector and parsing all previously supplied headers again before requesting the next chunk. Both the blocking and asynchronous APIs therefore perform quadratic parsing and string-allocation work as the directory grows: a 1 GiB directory requires roughly 64 passes over progressively larger prefixes, processing about 32 GiB of directory data. This particularly penalizes the large archives this API is intended to support. Preserve the parsing cursor and accumulated entries across reads so each header is parsed once.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in aa68e1f. The opener now keeps its walk over the directory (cursor, headers left, entries parsed so far) between reads, so each header is parsed once. It drops directory bytes it has already parsed, and asks only for the part of a read not yet supplied. The API is now next_read / supply / finish. New unit test a_big_directory_is_parsed_once_and_not_held_twice: a 21 MB directory supplied 64 KiB at a time (300+ reads) opens in 0.3 s, matches ZipReader, and never holds more than the tail plus one header between reads.
Every step re-ran the open from the start, so a directory needing several reads was parsed again on each, quadratic in its size (Codex review). The opener now keeps its walk (cursor, count left, entries so far) between reads, drops directory bytes it has parsed, and asks only for the part of a read not yet supplied. Its API becomes next_read / supply / finish. A 21 MB directory supplied in over 300 partial reads now opens in 0.3 s, holding at most the tail and one header between reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What
ZipReader::new(&[u8])takes the whole archive as one slice, so reading a 60 GB archive on a server, or one not on local disk, meant 60 GB in memory. Someone trying to use zipview for that hit exactly this.ZipIndexopens an archive of a known length through reads of byte ranges, and holds only what it read:fetch_entryreads the 30-byte local header, then that entry's header and data.fetch_password_checkreads the header, then the first 18 bytes of data.Two ways to drive it:
ZipIndex::open(len, read),fetch_entry(entry, password, read)andfetch_password_check(entry, read)takeread(offset, buf). Errors come back as the caller's error type viaFrom<ZipReadError>.std::io::Errornow converts, so|o, b| file.read_exact_at(b, o)just works.while let Some(range) = opener.next_read()? { opener.supply(range.start, &fetch(range).await?)?; } let index = opener.finish()?;. For entries,header_range/entry_range/password_check_rangename the bytes, andread_entry/password_checkfinish from them.How it's built:
src/index.rs). It followsZipReader::newstep by step: end-record search, ZIP64 locator and record, the declared-then-computed directory location for stub-prefixed archives, and the entry walk. Central headers go through the sameparse_central_entry.ZipReader::read_entry_with_passwordandpassword_checkwere split into a sharedread_local_entry/local_password_checkover "bytes from the local header on". The in-memory reader passes the whole archive and an offset;ZipIndexpasses what it read. This is a pure move.parse_central_entryis now#[inline(always)]. With a second caller, the compiler had stopped inlining it intoZipReader::new, andopen-20000went 460 → 515 µs. With the attribute it's back to 448–462 µs, against main's 454–470.Measured (fastest round, M4 Air,
examples/bench.rs)ZipReader)ZipIndexover the same bytes from memory)index-open-20000pays for copying the tail and directory out of the reads, which is small next to any real fetch.Correctness
tests/writer.rs) open every input both ways and require the same entries, comment and open error. They also require the same bytes or error from every entry, with no password, the right one and a wrong one, and the same password checks. The inputs:ZipReader. Between reads the opener holds at most the tail plus one header. This is a unit test; before the fix from review, each read re-parsed the directory from the start.readme.txt, and refuses the 50 GiB entry without reading it. A 60 GB archive whose end record claims a 4 GiB directory takes at most 3 reads of at most 16 MiB.fuzz_zipnow also opens every input withZipIndexand asserts it agrees withZipReaderon the entries, every entry's bytes with and without a password, and every password check. It ran 180,806 inputs locally (3 minutes, on the final code) with no findings.cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo test --all-targets(debug, as CI) andcargo test --docpass on aarch64-apple-darwin. The same checks pass in release on x86_64-linux.🤖 Generated with Claude Code