Add binary for file compression and decompression

This commit is contained in:
Lucas Schumacher 2024-09-10 21:10:15 -04:00
parent 96415385ef
commit 3fab061972
2 changed files with 51 additions and 1 deletions

View File

@ -76,7 +76,6 @@ impl<R: Read> BitReader<R> {
} else if self.repeat_bits <= 0 { } else if self.repeat_bits <= 0 {
return Err(io::Error::other("No more bits")); return Err(io::Error::other("No more bits"));
} else { } else {
println!("{}", self.repeat_bits);
self.repeat_bits -= 8; self.repeat_bits -= 8;
} }
self.next = 1 << 7; self.next = 1 << 7;

51
src/main.rs Normal file
View File

@ -0,0 +1,51 @@
use std::{
env,
fs::File,
io::{self, BufReader, Read},
path::Path,
};
use sac::modelA::ModelA;
enum Mode {
Compress,
Decompress,
}
type CodeValue = u32;
fn get_file<FilePath: AsRef<Path>>(filepath: FilePath) -> io::Result<Box<dyn Read>> {
let f = File::open(&filepath)?;
Ok(Box::new(BufReader::new(f)))
}
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let name = &args[0];
let (input, mode): (Box<dyn Read>, Mode) = match args.len() {
2 if args[1].to_lowercase() == "compress" => (Box::new(io::stdin()), Mode::Compress),
2 if args[1].to_lowercase() == "decompress" => (Box::new(io::stdin()), Mode::Decompress),
3 if args[1].to_lowercase() == "compress" => (get_file(&args[2])?, Mode::Compress),
3 if args[1].to_lowercase() == "decompress" => (get_file(&args[2])?, Mode::Decompress),
_ => {
eprintln!("Usage:");
eprintln!("{name} compress file > output # compress file and save to output");
eprintln!("{name} decompress file > output # decompress file and save to output");
return Err(io::Error::other(format!(
"Invalid command `{}`",
args.join(" ")
)));
}
};
let model: ModelA<CodeValue> = ModelA::default();
let mut output = io::stdout().lock();
match mode {
Mode::Compress => model.compress(input, &mut output)?,
Mode::Decompress => model.decompress(input, &mut output)?,
};
Ok(())
}