Skip to content

Commit

Permalink
add way of saving the encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
lxaw committed Sep 5, 2023
1 parent 01f1fdc commit 11a5763
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cargo run [input_file.txt] [output_file.bin]

`Old file size: 2026200 (in bytes)`

`After file size:1107887 (in bytes)`
`After file size: 1107887 (in bytes)`

# Notes:
This can surely be optimized. It was one of my first projects in Rust, so I am sure I will look back in agony at how poorly it was done. However, that is a problem for future me.
Binary file modified out.bin
Binary file not shown.
4 changes: 4 additions & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A 0
B 10
C 110
D 111
1 change: 1 addition & 0 deletions src/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fn convert_to_code_str(original_msg: &String,map: &BTreeMap<char,Vec<bool>>) ->V
ret
}


fn get_hash_char_freq(msg:String) -> BTreeMap<char,usize> {
/*
Return a hashmap of characters and their respective frequencies.
Expand Down
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::fs;
use std::io::{Read, Result,Write};
Expand Down Expand Up @@ -61,6 +62,28 @@ fn write_str_to_file(filename: &str, data: &Vec<bool>) -> std::io::Result<()>{
file.write(&[bit_buffer])?;
}

file.flush();

Ok(())
}

fn print_hash_to_file(map: &BTreeMap<char,Vec<bool>>, filename: &str) -> std::io::Result<()>{
let mut file = File::create(filename)?;

for (key,value) in map{
file.write(&[key.to_owned() as u8])?;
file.write(&[' ' as u8])?;
for bool_val in value{
if *bool_val{
file.write(&['1' as u8])?;
}else{
file.write(&['0' as u8])?;
}
}
file.write(&['\n' as u8])?;
}
file.flush()?;

Ok(())
}

Expand Down Expand Up @@ -117,4 +140,6 @@ fn main() {
println!("Old file size: {prior_file_size} (in bytes)");
println!("After file size: {after_file_size} (in bytes)");

print_hash_to_file(&hash_code,"out.txt");

}

0 comments on commit 11a5763

Please sign in to comment.