ZipArchive reuse

This commit is contained in:
Dmitry Belyaev 2022-09-13 11:49:44 +03:00
parent eedcf9a1b1
commit f7fa686057
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -42,7 +42,10 @@ fn write_file<T: Seek + Write>(
Ok(()) Ok(())
} }
fn process_files(files: &[PathBuf]) -> Result<(), Box<dyn std::error::Error>> { fn process_files<R: Read + Seek>(
archive: &mut zip::ZipArchive<R>,
files: &[PathBuf],
) -> Result<(), Box<dyn std::error::Error>> {
if files.is_empty() { if files.is_empty() {
return Ok(()); return Ok(());
} }
@ -59,10 +62,6 @@ fn process_files(files: &[PathBuf]) -> Result<(), Box<dyn std::error::Error>> {
trap: EncoderTrap::Ignore, trap: EncoderTrap::Ignore,
}; };
let zip_file = fs::File::open(INPUT_FILENAME)?;
let zip_reader = io::BufReader::new(zip_file);
let mut archive = zip::ZipArchive::new(zip_reader)?;
let mut outfile = fs::File::create(OUTPUT_FILENAME)?; let mut outfile = fs::File::create(OUTPUT_FILENAME)?;
let mut zip_writer = ZipWriter::new(&mut outfile); let mut zip_writer = ZipWriter::new(&mut outfile);
@ -105,14 +104,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
}) })
.collect(); .collect();
drop(archive);
println!("processing {} files...", source_files.len()); println!("processing {} files...", source_files.len());
source_files.sort(); source_files.sort();
let source_files = source_files; let source_files = source_files;
process_files(&source_files)?; process_files(&mut archive, &source_files)?;
println!("done"); println!("done");
Ok(()) Ok(())