This commit is contained in:
2025-10-17 13:05:11 +03:00
parent ac8b4d6f81
commit e5f238a126
2 changed files with 22 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ mod utils;
use clap::Parser;
use std::collections::HashMap;
use std::io::Write;
use std::{fs, io};
use std::{fs, io, time};
use utils::{Metadata, Zsdiff, get_hash};
use walkdir::WalkDir;
@@ -77,10 +77,25 @@ pub async fn zsdiff(
let new_hashes = walk_dir(new).await;
let compare_hashes = compare_hashes(old_hashes, new_hashes).await;
let parts = compare_hashes.to_vec().await;
let mut size_before = 0;
for p in &parts {
size_before += p.len();
}
let now = time::Instant::now();
utils::compress_parts(parts, &fs::File::create(output_filename)?, level).await;
let output_hash = get_hash(fs::read(output_filename)?).await;
let output_data = fs::read(output_filename)?;
let size_after = output_data.len();
let output_hash = get_hash(output_data).await;
fs::File::create(format!("{}.md5", output_filename))?.write_all(output_hash.as_bytes())?;
let elapsed = now.elapsed();
println!("Zsdiff hash: {}", output_hash);
println!("Size before: {:.1?}KB", size_before / 1024);
println!("Size after: {:.1?}KB", size_after / 1024);
println!(
"Compress ratio: {:.2?}%",
size_after as f64 / size_before as f64 * 100.0
);
print!("Time elapsed: {:.2?}s", elapsed);
Ok(())
}

View File

@@ -4,7 +4,7 @@ use clap::Parser;
use std::fs::read;
use std::io::Write;
use std::path::Path;
use std::{fs, io};
use std::{fs, io, time};
use utils::Zsdiff;
async fn create_tmp_dir(dir_name: String) -> Result<String, io::Error> {
@@ -33,6 +33,7 @@ async fn check_hash(filename: String) -> Result<(), io::Error> {
if !String::from_utf8(hash_file).unwrap().eq(&hash) {
return Err(io::Error::new(io::ErrorKind::Other, "Hash mismatch"));
}
println!("Zsdiff hash: {}", hash);
Ok(())
}
@@ -41,6 +42,7 @@ async fn zspatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
let parts = utils::decompress_parts(read(filename)?).await?;
let diff = Zsdiff::from_vec(parts).await?;
let tmp_dir_name = extract_files(&diff, filename).await?;
let now = time::Instant::now();
for name in diff.content.keys().collect::<Vec<&String>>() {
let from_path = Path::new(&tmp_dir_name).join(name);
let to_path = Path::new(&dest_dir).join(name);
@@ -62,6 +64,8 @@ async fn zspatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
}
}
fs::remove_dir_all(tmp_dir_name).ok();
println!("Patching done!");
println!("Elapsed time: {:.2?}", now.elapsed());
Ok(())
}
@@ -78,7 +82,6 @@ struct Args {
#[tokio::main]
async fn main() -> io::Result<()> {
let args = Args::parse();
println!("{}", args.hash_check);
if args.hash_check {
check_hash(args.filename.clone()).await?;
}