Merge pull request #279 from figsoda/compress-zip-modified

set last modified time during zip compression
This commit is contained in:
figsoda 2022-10-12 13:22:19 -04:00 committed by GitHub
commit ee082e1367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -31,6 +31,7 @@ Categories Used:
- Support snappy format [\#215](https://github.com/ouch-org/ouch/pull/215) ([figsoda](https://github.com/figsoda)) - 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)) - 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)) - 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 ### Bug Fixes
@ -94,7 +95,7 @@ Categories Used:
- Update dependencies [\#257](https://github.com/ouch-org/ouch/pull/257) ([Artturin](https://github.com/Artturin)) - 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)) - 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)) - 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)) - Update dependencies [\#276](https://github.com/ouch-org/ouch/pull/276) ([figsoda](https://github.com/figsoda))
### New Contributors ### New Contributors

View File

@ -4,6 +4,7 @@
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::{ use std::{
env, env,
fs::File,
io::{self, prelude::*}, io::{self, prelude::*},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::mpsc, sync::mpsc,
@ -12,7 +13,7 @@ use std::{
use filetime::{set_file_mtime, FileTime}; use filetime::{set_file_mtime, FileTime};
use fs_err as fs; use fs_err as fs;
use zip::{self, read::ZipFile, ZipArchive}; use zip::{self, read::ZipFile, DateTime, ZipArchive};
use crate::{ use crate::{
error::FinalError, error::FinalError,
@ -203,9 +204,12 @@ where
options options
}; };
writer.start_file(path.to_str().unwrap().to_owned(), options)?; let mut file = File::open(entry.path())?;
let file_bytes = fs::read(entry.path())?; writer.start_file(
writer.write_all(&file_bytes)?; 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<()> { fn set_last_modified_time(zip_file: &ZipFile, path: &Path) -> crate::Result<()> {
let modification_time_in_seconds = zip_file let modification_time_in_seconds = zip_file
.last_modified() .last_modified()