Created
January 17, 2023 23:10
-
-
Save kauly/8d18f13f95edbed3e4aaf2c18015219a to your computer and use it in GitHub Desktop.
Compress a folder content to gzip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use flate2::write::GzEncoder; | |
use flate2::Compression; | |
use std::env::args; | |
use std::fs::{create_dir, metadata, read_dir, File}; | |
use std::io::{copy, BufReader}; | |
use std::path::{Path, PathBuf}; | |
use std::time::Instant; | |
fn main() { | |
if args().len() < 3 { | |
println!("Usage: cargo run --release <source> <target>"); | |
return; | |
} | |
let source = args().nth(1).unwrap(); | |
let target = args().nth(2).unwrap(); | |
// create target directory if it doesn't exist | |
if metadata(target.clone()).is_err() { | |
create_dir(target.clone()).unwrap(); | |
} | |
// list all files in directory | |
let dir = read_dir(source).unwrap(); | |
for entry in dir { | |
let entry = entry.unwrap(); | |
let path = entry.path(); | |
if path.is_file() { | |
let output_path = Path::new(&target).join(path.file_name().unwrap()); | |
convert_to_gz(path, output_path); | |
} | |
} | |
} | |
fn convert_to_gz(path: PathBuf, output: PathBuf) { | |
let mut input = BufReader::new(File::open(path.clone()).unwrap()); | |
let output_file_name = format!("{}.gz", output.display()); | |
let output = File::create(output_file_name.clone()).unwrap(); | |
let mut encoder = GzEncoder::new(output, Compression::default()); | |
let start = Instant::now(); | |
copy(&mut input, &mut encoder).unwrap(); | |
let output = encoder.finish().unwrap(); | |
println!( | |
"Converted {:?}({}) to {:?}({}) in {:?}", | |
path, | |
input.get_ref().metadata().unwrap().len(), | |
output_file_name, | |
output.metadata().unwrap().len(), | |
start.elapsed() | |
); | |
} |
Only gltf
and bin
files have relevant compression.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: