diff --git a/CHANGELOG.md b/CHANGELOG.md index 361542c..c0f3166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Categories Used: - Support snappy format [\#215](https://github.com/ouch-org/ouch/pull/215) ([figsoda](https://github.com/figsoda)) - Allow ignoring hidden files and files matched by .gitignore files [\#245](https://github.com/ouch-org/ouch/pull/245) ([vrmiguel](https://github.com/vrmiguel)) - Automatically generate man pages with clap_mangen [\#273](https://github.com/ouch-org/ouch/pull/273) ([figsoda](https://github.com/figsoda)) +- Set last modified time during zip compression [\#279](https://github.com/ouch-org/ouch/pull/279) ([figsoda](https://github.com/figsoda)) ### Bug Fixes @@ -94,7 +95,7 @@ Categories Used: - Update dependencies [\#257](https://github.com/ouch-org/ouch/pull/257) ([Artturin](https://github.com/Artturin)) - Add pull request template [\#263](https://github.com/ouch-org/ouch/pull/263) ([figsoda](https://github.com/figsoda)) - Clean up the description for the `-d/--dir` argument to `decompress` [\#264](https://github.com/ouch-org/ouch/pull/264) ([hivehand](https://github.com/hivehand)) -- Show subcommand aliases on --help [\#275](https://github.com/ouch-org/ouch/pull/275) ([marcospb19](https://github.com/marcospb19)) +- Show subcommand aliases on --help [\#275](https://github.com/ouch-org/ouch/pull/275) ([marcospb19](https://github.com/marcospb19)) - Update dependencies [\#276](https://github.com/ouch-org/ouch/pull/276) ([figsoda](https://github.com/figsoda)) ### New Contributors diff --git a/src/archive/zip.rs b/src/archive/zip.rs index bae50a3..fd0ecfb 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -4,6 +4,7 @@ use std::os::unix::fs::PermissionsExt; use std::{ env, + fs::File, io::{self, prelude::*}, path::{Path, PathBuf}, sync::mpsc, @@ -12,7 +13,7 @@ use std::{ use filetime::{set_file_mtime, FileTime}; use fs_err as fs; -use zip::{self, read::ZipFile, ZipArchive}; +use zip::{self, read::ZipFile, DateTime, ZipArchive}; use crate::{ error::FinalError, @@ -203,9 +204,12 @@ where options }; - writer.start_file(path.to_str().unwrap().to_owned(), options)?; - let file_bytes = fs::read(entry.path())?; - writer.write_all(&file_bytes)?; + let mut file = File::open(entry.path())?; + writer.start_file( + path.to_str().unwrap(), + options.last_modified_time(get_last_modified_time(&file)), + )?; + io::copy(&mut file, &mut writer)?; } } @@ -233,6 +237,14 @@ fn display_zip_comment_if_exists(file: &ZipFile) { } } +fn get_last_modified_time(file: &File) -> DateTime { + file.metadata() + .and_then(|metadata| metadata.modified()) + .map_err(|_| ()) + .and_then(|time| DateTime::from_time(time.into())) + .unwrap_or_default() +} + fn set_last_modified_time(zip_file: &ZipFile, path: &Path) -> crate::Result<()> { let modification_time_in_seconds = zip_file .last_modified()