use std::{
env, fs,
path::{Path, PathBuf},
};
use preflate_rs::{compress_zstd, decompress_zstd, CompressionStats};
fn enumerate_directory_recursively(path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
let mut results = Vec::new();
if path.is_dir() {
let entries = fs::read_dir(path)?;
for entry in entries {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if let Ok(sub_dir_results) = enumerate_directory_recursively(&path) {
results.extend(sub_dir_results);
}
} else {
results.push(path);
}
}
} else {
results.push(path.to_path_buf());
}
Ok(results)
}
fn main() {
let current_dir = env::args().nth(1).unwrap_or_else(|| String::from("."));
let mut totalseen = 0u64;
let mut totalzstd = 0u64;
for entry in enumerate_directory_recursively(Path::new(¤t_dir)).unwrap() {
println!("Processing file: {:?}", entry);
let file = std::fs::read(entry).unwrap();
let zstdlen = zstd::bulk::compress(&file, 9).unwrap();
let mut stats = CompressionStats::default();
let preflatecompressed = compress_zstd(&file, 1, &mut stats).unwrap();
totalseen += zstdlen.len() as u64;
totalzstd += preflatecompressed.len() as u64;
let original = decompress_zstd(&preflatecompressed, 1024 * 1024 * 128).unwrap();
assert!(original == file);
println!("total seen ratio {}", totalzstd as f64 / totalseen as f64);
}
}