Repository navigation
fix: Add a OwnedZipFileEntry for #177 - #1270
Conversation
There was a problem hiding this comment.
Review Summary
This PR adds OwnedZipFileEntry, an owned counterpart to the lifetime-parameterized ZipFileEntry<'a>, enabling callers to store a ZIP entry's metadata beyond the lifetime of the original reader (resolving #177). The design — a Box<[u8]> for the raw name plus an owned ZipFileData, with as_ref() returning a borrowed ZipFileEntry<'_> — is sound in principle.
Critical Issues
1. From<ZipFile<'a, R>> for ZipFileEntry<'a> drains the reader unconditionally (src/read/zipfile.rs, lines 292–299).
After mem::take(&mut file.data), file.data becomes Cow::Owned(ZipFileData::default()). The Drop impl treats any Cow::Owned data as a signal that the file came from a streaming reader and must drain the remaining bytes. This means every call to the new From conversion will drain the underlying reader on drop, even for seekable archive reads where that is both wasteful and incorrect.
2. Test relies on an implicit assumption about header_start being 0 (tests/stream.rs, lines 132–137).
check_content reopens mimetype.zip from scratch and calls with_reader using metadata recovered from a streaming read. This works only because header_start happens to be 0 for this fixture. The pattern is not general and the failure mode (wrong seek target) is silent in the success path.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| impl<'a, R: Read + ?Sized> From<ZipFile<'a, R>> for ZipFileEntry<'a> { | ||
| fn from(mut file: ZipFile<'a, R>) -> Self { | ||
| Self { | ||
| file_name_raw: core::mem::take(&mut file.file_name_raw), | ||
| data: core::mem::take(&mut file.data), | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
🛑 Logic Error: After mem::take(&mut file.data), file.data is left as Cow::Owned(ZipFileData::default()). The Drop impl for ZipFile checks if let Cow::Owned(_) = self.data and, finding it owned, calls take_raw_reader() to drain the underlying reader — even for seekable archive files where draining is unnecessary and incorrect. This means every ZipFile-to-ZipFileEntry conversion via From will unconditionally drain the reader after the conversion completes. Replace mem::take on data with a variant that leaves a Cow::Borrowed sentinel, or use ManuallyDrop / std::mem::forget to skip the drop.
| fn check_content(owned_zip_file: OwnedZipFileEntry) { | ||
| let zip_file_entry = owned_zip_file.as_ref(); | ||
| let mut reader = Cursor::new(include_bytes!("./data/mimetype.zip")); | ||
| let mut zip_file = zip_file_entry | ||
| .with_reader(&mut reader, ZipReadOptions::default()) | ||
| .unwrap(); |
There was a problem hiding this comment.
The check_content function opens a brand-new Cursor over the raw mimetype.zip bytes and passes it to with_reader. The OwnedZipFileEntry was obtained from read_zipfile_from_stream, which reads sequentially; the header_start stored in the entry's ZipFileData reflects the byte offset in that stream. When find_content is called on the fresh cursor, it seeks to that offset and reads the local file header. This will work only if mimetype.zip actually begins at byte 0 and the stored offset matches — which happens to be true for this specific fixture. However, this pattern is fragile: if header_start is non-zero or the local file header check fails, with_reader returns an error at runtime. The test doesn't cover this failure path and silently relies on a coincidence of the fixture. Add an assertion or comment documenting that header_start must be 0 for this pattern to be safe, or guard against with_reader returning Err.
There was a problem hiding this comment.
read_zipfile_from_stream doesn't currently work with leading non-ZIP data anyway, but this sounds like it could be an extra barrier to ever removing that limitation.
Possible fix for #177 - but add a new
OwnedZipFileEntryOpen in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.