refactor: Change type of last_modified_time
to Option<DateTime>
This commit is contained in:
parent
e08548392d
commit
7d61377503
3 changed files with 21 additions and 18 deletions
17
src/read.rs
17
src/read.rs
|
@ -242,7 +242,7 @@ pub(crate) fn find_content<'a>(
|
||||||
pub(crate) fn make_crypto_reader<'a>(
|
pub(crate) fn make_crypto_reader<'a>(
|
||||||
compression_method: CompressionMethod,
|
compression_method: CompressionMethod,
|
||||||
crc32: u32,
|
crc32: u32,
|
||||||
last_modified_time: DateTime,
|
mut last_modified_time: Option<DateTime>,
|
||||||
using_data_descriptor: bool,
|
using_data_descriptor: bool,
|
||||||
reader: io::Take<&'a mut dyn Read>,
|
reader: io::Take<&'a mut dyn Read>,
|
||||||
password: Option<&[u8]>,
|
password: Option<&[u8]>,
|
||||||
|
@ -269,7 +269,10 @@ pub(crate) fn make_crypto_reader<'a>(
|
||||||
vendor_version,
|
vendor_version,
|
||||||
},
|
},
|
||||||
(Some(password), None) => {
|
(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())
|
ZipCryptoValidator::InfoZipMsdosTime(last_modified_time.timepart())
|
||||||
} else {
|
} else {
|
||||||
ZipCryptoValidator::PkzipCrc32(crc32)
|
ZipCryptoValidator::PkzipCrc32(crc32)
|
||||||
|
@ -1009,8 +1012,7 @@ fn central_header_to_zip_file_inner<R: Read>(
|
||||||
CompressionMethod::from_u16(compression_method)
|
CompressionMethod::from_u16(compression_method)
|
||||||
},
|
},
|
||||||
compression_level: None,
|
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).ok(),
|
||||||
.unwrap_or_else(|_| DateTime::default()),
|
|
||||||
crc32,
|
crc32,
|
||||||
compressed_size: compressed_size as u64,
|
compressed_size: compressed_size as u64,
|
||||||
uncompressed_size: uncompressed_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
|
/// Get the time the file was last modified
|
||||||
pub fn last_modified(&self) -> DateTime {
|
pub fn last_modified(&self) -> Option<DateTime> {
|
||||||
self.data.last_modified_time
|
self.data.last_modified_time
|
||||||
}
|
}
|
||||||
/// Returns whether the file is actually a directory
|
/// 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<Opt
|
||||||
using_data_descriptor: false,
|
using_data_descriptor: false,
|
||||||
compression_method,
|
compression_method,
|
||||||
compression_level: None,
|
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).ok(),
|
||||||
.unwrap_or_else(|_| DateTime::default()),
|
|
||||||
crc32,
|
crc32,
|
||||||
compressed_size: compressed_size as u64,
|
compressed_size: compressed_size as u64,
|
||||||
uncompressed_size: uncompressed_size as u64,
|
uncompressed_size: uncompressed_size as u64,
|
||||||
|
@ -1525,7 +1526,7 @@ mod test {
|
||||||
let mut file1 = reader1.by_index(0).unwrap();
|
let mut file1 = reader1.by_index(0).unwrap();
|
||||||
let mut file2 = reader2.by_index(0).unwrap();
|
let mut file2 = reader2.by_index(0).unwrap();
|
||||||
|
|
||||||
let t = file1.last_modified();
|
let t = file1.last_modified().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(
|
(
|
||||||
t.year(),
|
t.year(),
|
||||||
|
|
|
@ -380,7 +380,7 @@ pub struct ZipFileData {
|
||||||
/// Compression level to store the file
|
/// Compression level to store the file
|
||||||
pub compression_level: Option<i64>,
|
pub compression_level: Option<i64>,
|
||||||
/// Last modified time. This will only have a 2 second precision.
|
/// Last modified time. This will only have a 2 second precision.
|
||||||
pub last_modified_time: DateTime,
|
pub last_modified_time: Option<DateTime>,
|
||||||
/// CRC32 checksum
|
/// CRC32 checksum
|
||||||
pub crc32: u32,
|
pub crc32: u32,
|
||||||
/// Size of the file in the ZIP
|
/// Size of the file in the ZIP
|
||||||
|
@ -615,7 +615,7 @@ mod test {
|
||||||
using_data_descriptor: false,
|
using_data_descriptor: false,
|
||||||
compression_method: crate::compression::CompressionMethod::Stored,
|
compression_method: crate::compression::CompressionMethod::Stored,
|
||||||
compression_level: None,
|
compression_level: None,
|
||||||
last_modified_time: DateTime::default(),
|
last_modified_time: None,
|
||||||
crc32: 0,
|
crc32: 0,
|
||||||
compressed_size: 0,
|
compressed_size: 0,
|
||||||
uncompressed_size: 0,
|
uncompressed_size: 0,
|
||||||
|
|
18
src/write.rs
18
src/write.rs
|
@ -573,7 +573,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
|
||||||
let mut options = FileOptions::<ExtendedFileOptions> {
|
let mut options = FileOptions::<ExtendedFileOptions> {
|
||||||
compression_method: src_data.compression_method,
|
compression_method: src_data.compression_method,
|
||||||
compression_level: src_data.compression_level,
|
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(),
|
permissions: src_data.unix_mode(),
|
||||||
large_file: src_data.large_file,
|
large_file: src_data.large_file,
|
||||||
encrypt_with: None,
|
encrypt_with: None,
|
||||||
|
@ -594,7 +594,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
|
||||||
let mut options = FileOptions::<()> {
|
let mut options = FileOptions::<()> {
|
||||||
compression_method: src_data.compression_method,
|
compression_method: src_data.compression_method,
|
||||||
compression_level: src_data.compression_level,
|
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(),
|
permissions: src_data.unix_mode(),
|
||||||
large_file: src_data.large_file,
|
large_file: src_data.large_file,
|
||||||
encrypt_with: None,
|
encrypt_with: None,
|
||||||
|
@ -782,6 +782,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
),
|
),
|
||||||
_ => (options.compression_method, None),
|
_ => (options.compression_method, None),
|
||||||
};
|
};
|
||||||
|
let last_modified_time = options.last_modified_time;
|
||||||
let file = ZipFileData {
|
let file = ZipFileData {
|
||||||
system: System::Unix,
|
system: System::Unix,
|
||||||
version_made_by: DEFAULT_VERSION,
|
version_made_by: DEFAULT_VERSION,
|
||||||
|
@ -789,7 +790,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
using_data_descriptor: false,
|
using_data_descriptor: false,
|
||||||
compression_method,
|
compression_method,
|
||||||
compression_level: options.compression_level,
|
compression_level: options.compression_level,
|
||||||
last_modified_time: options.last_modified_time,
|
last_modified_time: Some(options.last_modified_time),
|
||||||
crc32: raw_values.crc32,
|
crc32: raw_values.crc32,
|
||||||
compressed_size: raw_values.compressed_size,
|
compressed_size: raw_values.compressed_size,
|
||||||
uncompressed_size: raw_values.uncompressed_size,
|
uncompressed_size: raw_values.uncompressed_size,
|
||||||
|
@ -826,8 +827,8 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
writer.write_u16_le(file.compression_method.to_u16())?;
|
writer.write_u16_le(file.compression_method.to_u16())?;
|
||||||
// last mod file time and last mod file date
|
// last mod file time and last mod file date
|
||||||
writer.write_u16_le(file.last_modified_time.timepart())?;
|
writer.write_u16_le(last_modified_time.timepart())?;
|
||||||
writer.write_u16_le(file.last_modified_time.datepart())?;
|
writer.write_u16_le(last_modified_time.datepart())?;
|
||||||
// crc-32
|
// crc-32
|
||||||
writer.write_u32_le(file.crc32)?;
|
writer.write_u32_le(file.crc32)?;
|
||||||
// compressed size and uncompressed size
|
// compressed size and uncompressed size
|
||||||
|
@ -1193,7 +1194,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
{
|
{
|
||||||
let mut options = SimpleFileOptions::default()
|
let mut options = SimpleFileOptions::default()
|
||||||
.large_file(file.compressed_size().max(file.size()) > spec::ZIP64_BYTES_THR)
|
.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());
|
.compression_method(file.compression());
|
||||||
if let Some(perms) = file.unix_mode() {
|
if let Some(perms) = file.unix_mode() {
|
||||||
options = options.unix_permissions(perms);
|
options = options.unix_permissions(perms);
|
||||||
|
@ -1813,9 +1814,10 @@ fn write_central_directory_header<T: Write>(writer: &mut T, file: &ZipFileData)
|
||||||
// compression method
|
// compression method
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
writer.write_u16_le(file.compression_method.to_u16())?;
|
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
|
// last mod file time + date
|
||||||
writer.write_u16_le(file.last_modified_time.timepart())?;
|
writer.write_u16_le(last_modified_time.timepart())?;
|
||||||
writer.write_u16_le(file.last_modified_time.datepart())?;
|
writer.write_u16_le(last_modified_time.datepart())?;
|
||||||
// crc-32
|
// crc-32
|
||||||
writer.write_u32_le(file.crc32)?;
|
writer.write_u32_le(file.crc32)?;
|
||||||
// compressed size
|
// compressed size
|
||||||
|
|
Loading…
Add table
Reference in a new issue