From 09d390608fd98659973bdb4cbee039f88388fa1c Mon Sep 17 00:00:00 2001 From: figsoda Date: Tue, 11 Oct 2022 21:52:57 -0400 Subject: [PATCH] set last modified time during zip compression --- src/archive/zip.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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()