Repository navigation
fix: keep extended timestamp and NTFS extra fields in raw copies #1293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Pr0methean
merged 8 commits into
zip-rs:master
from
compiler-segfault:fix/raw-copy-keep-extra-fields
Nov 15, 2026
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
80dddf6
fix: keep extended timestamp and NTFS extra fields in raw copies
compiler-segfault 7e48ca6
Update src/extra_fields/extended_timestamp.rs
Pr0methean 74443a5
Merge branch 'master' into fix/raw-copy-keep-extra-fields
Pr0methean 07ce8f2
cargo fmt --all
Pr0methean b808cbe
Merge branch 'master' into fix/raw-copy-keep-extra-fields
cincrement 9f00c65
Fix size calculation for serialized extra field
Its-Just-Nans 5f2cb8a
Fix size calculation in extended_timestamp.rs
Its-Just-Nans 0c1c7c4
Merge branch 'master' into fix/raw-copy-keep-extra-fields
Pr0methean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| //! A raw copy has to keep the extra fields that the reader parsed for the source entry. | ||
| //! | ||
| //! `ZipWriter::raw_copy_file` passes them through `ExtraField::write`, which used to have no | ||
| //! arm for the extended timestamp (0x5455) and the NTFS file times (0x000a): the serialized | ||
| //! field came back empty and the copy silently lost it (see #1285). | ||
|
|
||
| use std::io::{Cursor, Write}; | ||
|
|
||
| use zip::extra_fields::ExtraField; | ||
| use zip::write::{FullFileOptions, ZipWriter}; | ||
| use zip::{CompressionMethod, ZipArchive}; | ||
|
|
||
| const MODIFIED: u32 = 1_715_000_001; | ||
| const MTIME: u64 = 133_000_000_000_000_000; | ||
| const ATIME: u64 = 133_000_000_000_000_001; | ||
| const CTIME: u64 = 133_000_000_000_000_002; | ||
|
|
||
| /// Extended timestamp (0x5455) with only the modification time, as written in | ||
| /// the central directory. | ||
| fn extended_timestamp() -> Vec<u8> { | ||
| let mut data = vec![0x01]; | ||
| data.extend_from_slice(&MODIFIED.to_le_bytes()); | ||
| data | ||
| } | ||
|
|
||
| /// NTFS file times (0x000a): reserved field, file-times attribute tag and | ||
| /// size, then the three Windows file times. | ||
| fn ntfs_times() -> Vec<u8> { | ||
| let mut data = Vec::new(); | ||
| data.extend_from_slice(&0u32.to_le_bytes()); // reserved for future use | ||
| data.extend_from_slice(&0x0001u16.to_le_bytes()); // attribute tag: file times | ||
| data.extend_from_slice(&24u16.to_le_bytes()); // attribute size | ||
| data.extend_from_slice(&MTIME.to_le_bytes()); | ||
| data.extend_from_slice(&ATIME.to_le_bytes()); | ||
| data.extend_from_slice(&CTIME.to_le_bytes()); | ||
| data | ||
| } | ||
|
|
||
| fn source_archive() -> Vec<u8> { | ||
| let mut options = FullFileOptions::default().compression_method(CompressionMethod::Stored); | ||
| options | ||
| .add_extra_field(0x5455, extended_timestamp(), false) | ||
| .unwrap(); | ||
| options | ||
| .add_extra_field(0x000a, ntfs_times(), false) | ||
| .unwrap(); | ||
| let mut writer = ZipWriter::new(Cursor::new(Vec::new())); | ||
| writer.start_file("a.txt", options).unwrap(); | ||
| writer.write_all(b"hello").unwrap(); | ||
| writer.finish().unwrap().into_inner() | ||
| } | ||
|
|
||
| fn assert_fields(archive: &mut ZipArchive<Cursor<Vec<u8>>>, expected: bool) { | ||
| let file = archive.by_index_raw(0).unwrap(); | ||
| let mut found_timestamp = false; | ||
| let mut found_ntfs = false; | ||
| for field in file.extra_data_fields() { | ||
| match field { | ||
| ExtraField::ExtendedTimestamp(timestamp) => { | ||
| found_timestamp = true; | ||
| assert_eq!(timestamp.mod_time(), Some(MODIFIED)); | ||
| } | ||
| ExtraField::Ntfs(ntfs) => { | ||
| found_ntfs = true; | ||
| assert_eq!(ntfs.mtime(), MTIME); | ||
| assert_eq!(ntfs.atime(), ATIME); | ||
| assert_eq!(ntfs.ctime(), CTIME); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| assert_eq!(found_timestamp, expected, "extended timestamp field"); | ||
| assert_eq!(found_ntfs, expected, "NTFS file times field"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn raw_copy_keeps_the_extended_timestamp_and_ntfs_times() { | ||
| let source = source_archive(); | ||
|
|
||
| // The source archive has both fields, so the copy has something to keep. | ||
| let mut archive = ZipArchive::new(Cursor::new(source.clone())).unwrap(); | ||
| assert_fields(&mut archive, true); | ||
|
|
||
| let mut copy = ZipWriter::new(Cursor::new(Vec::new())); | ||
| copy.raw_copy_file(archive.by_index_raw(0).unwrap()) | ||
| .unwrap(); | ||
| let copied = copy.finish().unwrap().into_inner(); | ||
|
|
||
| let mut copy_archive = ZipArchive::new(Cursor::new(copied)).unwrap(); | ||
| assert_fields(&mut copy_archive, true); | ||
|
|
||
| // The copied entry is still readable, and its contents are intact. | ||
| let mut file = copy_archive.by_name("a.txt").unwrap(); | ||
| let mut contents = Vec::new(); | ||
| std::io::Read::read_to_end(&mut file, &mut contents).unwrap(); | ||
| assert_eq!(contents, b"hello"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.