Added a fancy progressbar.
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -1,5 +1,6 @@
|
||||
extern crate getopts;
|
||||
extern crate byteorder;
|
||||
extern crate pbr;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
@@ -10,6 +11,7 @@ use std::io::Cursor;
|
||||
|
||||
use getopts::Options;
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian};
|
||||
use pbr::{ProgressBar, Units};
|
||||
|
||||
|
||||
fn main() {
|
||||
@@ -51,9 +53,14 @@ fn compress(input: &String, output: &String) {
|
||||
let mut output_file = fs::File::create(output).unwrap();
|
||||
|
||||
let input_file = fs::File::open(input).unwrap();
|
||||
let n_bytes = input_file.metadata().unwrap().len() as u64;
|
||||
|
||||
let input_buf = BufReader::new(input_file);
|
||||
|
||||
let mut i : i32 = 0;
|
||||
let mut pb = ProgressBar::new(n_bytes);
|
||||
|
||||
pb.set_units(Units::Bytes);
|
||||
|
||||
for b in input_buf.bytes() {
|
||||
let b = b.unwrap();
|
||||
@@ -70,14 +77,21 @@ fn compress(input: &String, output: &String) {
|
||||
index.write_i32::<LittleEndian>(i).unwrap();
|
||||
|
||||
output_file.write_all(&index).unwrap();
|
||||
|
||||
pb.inc();
|
||||
}
|
||||
}
|
||||
|
||||
fn extract(input: &String, output: &String) {
|
||||
let mut output_file = fs::File::create(output).unwrap();
|
||||
let mut input_file = fs::File::open(input).unwrap();
|
||||
let n_bytes = input_file.metadata().unwrap().len() as u64;
|
||||
|
||||
let mut output_file = fs::File::create(output).unwrap();
|
||||
|
||||
let mut buf = vec![0; 4];
|
||||
let mut pb = ProgressBar::new(n_bytes);
|
||||
|
||||
pb.set_units(Units::Bytes);
|
||||
|
||||
loop {
|
||||
let size = input_file.read(&mut buf).unwrap();
|
||||
@@ -90,6 +104,8 @@ fn extract(input: &String, output: &String) {
|
||||
let index = c.read_i32::<LittleEndian>().unwrap();
|
||||
|
||||
output_file.write_all(&[get_byte(index)]).unwrap();
|
||||
|
||||
pb.add(size as u64);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user