From b0c666aa0c466d5cb1a7f894c8c9e4e064390ef1 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Thu, 23 May 2024 09:44:06 -0700 Subject: [PATCH] chore: Continue to accept archives with invalid DateTime, and use `now_utc()` as default only when writing, not reading --- src/read.rs | 6 ++++-- src/types.rs | 16 ++++++++++++++++ src/write.rs | 8 +------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/read.rs b/src/read.rs index c13b5329..91080941 100644 --- a/src/read.rs +++ b/src/read.rs @@ -1009,7 +1009,8 @@ fn central_header_to_zip_file_inner( CompressionMethod::from_u16(compression_method) }, compression_level: None, - last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)?, + last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time) + .unwrap_or_else(|_| DateTime::default()), crc32, compressed_size: compressed_size as u64, uncompressed_size: uncompressed_size as u64, @@ -1396,7 +1397,8 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult Self { + OffsetDateTime::now_utc() + .try_into() + .unwrap_or_else(|_| DateTime::default()) + } + + /// Returns the current time if possible, otherwise the default of 1980-01-01. + #[cfg(not(feature = "time"))] + pub fn default_for_write() -> Self { + DateTime::default() + } +} + #[cfg(fuzzing)] impl arbitrary::Arbitrary<'_> for DateTime { fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result { diff --git a/src/write.rs b/src/write.rs index f97822b1..3f104204 100644 --- a/src/write.rs +++ b/src/write.rs @@ -34,9 +34,6 @@ use flate2::{write::DeflateEncoder, Compression}; #[cfg(feature = "bzip2")] use bzip2::write::BzEncoder; -#[cfg(feature = "time")] -use time::OffsetDateTime; - #[cfg(feature = "deflate-zopfli")] use zopfli::Options; @@ -444,10 +441,7 @@ impl<'k, T: FileOptionExtension> Default for FileOptions<'k, T> { Self { compression_method: Default::default(), compression_level: None, - #[cfg(feature = "time")] - last_modified_time: OffsetDateTime::now_utc().try_into().unwrap_or_default(), - #[cfg(not(feature = "time"))] - last_modified_time: DateTime::default(), + last_modified_time: DateTime::default_for_write(), permissions: None, large_file: false, encrypt_with: None,