diff --git a/src/read.rs b/src/read.rs index 91080941..a6763030 100644 --- a/src/read.rs +++ b/src/read.rs @@ -242,7 +242,7 @@ pub(crate) fn find_content<'a>( pub(crate) fn make_crypto_reader<'a>( compression_method: CompressionMethod, crc32: u32, - last_modified_time: DateTime, + mut last_modified_time: Option, using_data_descriptor: bool, reader: io::Take<&'a mut dyn Read>, password: Option<&[u8]>, @@ -269,7 +269,10 @@ pub(crate) fn make_crypto_reader<'a>( vendor_version, }, (Some(password), None) => { - let validator = if using_data_descriptor { + if !using_data_descriptor { + last_modified_time = None; + } + let validator = if let Some(last_modified_time) = last_modified_time { ZipCryptoValidator::InfoZipMsdosTime(last_modified_time.timepart()) } else { ZipCryptoValidator::PkzipCrc32(crc32) @@ -1009,8 +1012,7 @@ 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) - .unwrap_or_else(|_| DateTime::default()), + last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time).ok(), crc32, compressed_size: compressed_size as u64, uncompressed_size: uncompressed_size as u64, @@ -1252,7 +1254,7 @@ impl<'a> ZipFile<'a> { } /// Get the time the file was last modified - pub fn last_modified(&self) -> DateTime { + pub fn last_modified(&self) -> Option { self.data.last_modified_time } /// Returns whether the file is actually a directory @@ -1397,8 +1399,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult, /// Last modified time. This will only have a 2 second precision. - pub last_modified_time: DateTime, + pub last_modified_time: Option, /// CRC32 checksum pub crc32: u32, /// Size of the file in the ZIP @@ -615,7 +615,7 @@ mod test { using_data_descriptor: false, compression_method: crate::compression::CompressionMethod::Stored, compression_level: None, - last_modified_time: DateTime::default(), + last_modified_time: None, crc32: 0, compressed_size: 0, uncompressed_size: 0, diff --git a/src/write.rs b/src/write.rs index 3f104204..d2f2883f 100644 --- a/src/write.rs +++ b/src/write.rs @@ -573,7 +573,7 @@ impl ZipWriter { let mut options = FileOptions:: { compression_method: src_data.compression_method, compression_level: src_data.compression_level, - last_modified_time: src_data.last_modified_time, + last_modified_time: src_data.last_modified_time.unwrap_or_else(DateTime::default_for_write), permissions: src_data.unix_mode(), large_file: src_data.large_file, encrypt_with: None, @@ -594,7 +594,7 @@ impl ZipWriter { let mut options = FileOptions::<()> { compression_method: src_data.compression_method, compression_level: src_data.compression_level, - last_modified_time: src_data.last_modified_time, + last_modified_time: src_data.last_modified_time.unwrap_or_else(DateTime::default_for_write), permissions: src_data.unix_mode(), large_file: src_data.large_file, encrypt_with: None, @@ -782,6 +782,7 @@ impl ZipWriter { ), _ => (options.compression_method, None), }; + let last_modified_time = options.last_modified_time; let file = ZipFileData { system: System::Unix, version_made_by: DEFAULT_VERSION, @@ -789,7 +790,7 @@ impl ZipWriter { using_data_descriptor: false, compression_method, compression_level: options.compression_level, - last_modified_time: options.last_modified_time, + last_modified_time: Some(options.last_modified_time), crc32: raw_values.crc32, compressed_size: raw_values.compressed_size, uncompressed_size: raw_values.uncompressed_size, @@ -826,8 +827,8 @@ impl ZipWriter { #[allow(deprecated)] writer.write_u16_le(file.compression_method.to_u16())?; // last mod file time and last mod file date - writer.write_u16_le(file.last_modified_time.timepart())?; - writer.write_u16_le(file.last_modified_time.datepart())?; + writer.write_u16_le(last_modified_time.timepart())?; + writer.write_u16_le(last_modified_time.datepart())?; // crc-32 writer.write_u32_le(file.crc32)?; // compressed size and uncompressed size @@ -1193,7 +1194,7 @@ impl ZipWriter { { let mut options = SimpleFileOptions::default() .large_file(file.compressed_size().max(file.size()) > spec::ZIP64_BYTES_THR) - .last_modified_time(file.last_modified()) + .last_modified_time(file.last_modified().unwrap_or_else(DateTime::default_for_write)) .compression_method(file.compression()); if let Some(perms) = file.unix_mode() { options = options.unix_permissions(perms); @@ -1813,9 +1814,10 @@ fn write_central_directory_header(writer: &mut T, file: &ZipFileData) // compression method #[allow(deprecated)] writer.write_u16_le(file.compression_method.to_u16())?; + let last_modified_time = file.last_modified_time.unwrap_or_else(DateTime::default_for_write); // last mod file time + date - writer.write_u16_le(file.last_modified_time.timepart())?; - writer.write_u16_le(file.last_modified_time.datepart())?; + writer.write_u16_le(last_modified_time.timepart())?; + writer.write_u16_le(last_modified_time.datepart())?; // crc-32 writer.write_u32_le(file.crc32)?; // compressed size