Skip to content
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

fix: Restore original file timestamp when unzipping with chrono #46

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@
files_by_unix_mode.push((outpath.clone(), mode));
}
}
// Set original timestamp.
if let Some(t) = datetime_to_systemtime(&file.last_modified()) {

Check failure on line 983 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

mismatched types

Check failure on line 983 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

mismatched types
outfile.set_modified(t)?;

Check failure on line 984 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

use of unstable library feature 'file_set_times'
}
}
#[cfg(unix)]
{
Expand Down Expand Up @@ -1714,6 +1718,33 @@
}))
}

/// Generate a `SystemTime` from a `DateTime`.
fn datetime_to_systemtime(datetime: &DateTime) -> Option<std::time::SystemTime> {
if let Some(t) = generate_chrono_datetime(datetime) {
let time = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(t, chrono::Utc);

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1724 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

failed to resolve: use of undeclared crate or module `chrono`
return Some(time.into());
}
None
}

/// Generate a `NaiveDateTime` from a `DateTime`.
fn generate_chrono_datetime(datetime: &DateTime) -> Option<chrono::NaiveDateTime> {

Check failure on line 1731 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1731 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

failed to resolve: use of undeclared crate or module `chrono`
if let Some(d) = chrono::NaiveDate::from_ymd_opt(

Check failure on line 1732 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

failed to resolve: use of undeclared crate or module `chrono`

Check failure on line 1732 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

failed to resolve: use of undeclared crate or module `chrono`
datetime.year().into(),
datetime.month().into(),
datetime.day().into(),
) {
if let Some(d) = d.and_hms_opt(
datetime.hour().into(),
datetime.minute().into(),
datetime.second().into(),
) {
return Some(d);
}
}
None
}

#[cfg(test)]
mod test {
use crate::result::ZipResult;
Expand Down
Loading