diff --git a/Cargo.lock b/Cargo.lock index d8775a3..417f538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bit-io" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc20c4b4a315d582b092c173c5dd26462cc6c49fe80c158b6a1a2d1216f85c5a" - [[package]] name = "compression_tool" version = "0.1.0" -dependencies = [ - "bit-io", -] diff --git a/Cargo.toml b/Cargo.toml index cb4f9ae..c879891 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] -bit-io = "0.1.0" [[bin]] -name = "compression_tool" +name = "cczip" +path = "src/cczip.rs" + +[[bin]] +name = "ccunzip" +path = "src/ccunzip.rs" \ No newline at end of file diff --git a/README.md b/README.md index 57888da..e15c4df 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,56 @@ -Steps to create a compression tool Unix Command (compression_tool) +# Compression Tool -- Compile the program -```bash +This is a simple file compression and decompression tool that uses Huffman coding to compress files. The tool supports two primary operations: + +- **Compression**: Compresses an input file and stores the result in an output file. +- **Decompression**: Decompresses a compressed file and stores the result in an output file. + +The tool is used through the command line with the following commands: + +- `cczip` for compression +- `ccunzip` for decompression + +## Steps to Create and Use the Compression Tool + +### 1. Compile the Program + +First, build the program by running the following command: + +```bash cargo build --release ``` -- Move the executable to a directory in your PATH +### 2. Move the executables to a directory in your PATH + +To make the tool accessible from anywhere in your terminal, move the compiled executables to a directory that is included in your system's PATH. + ```bash -sudo cp ~/projects/compression_tool/target/release/compression_tool /usr/local/bin/ +sudo cp target/release/cczip /usr/local/bin/ +sudo cp target/release/ccunzip /usr/local/bin/ ``` -- Verify the installation +### 3. Verify the installation + +To verify the installation, check if the executables are accessible from anywhere in your terminal: + ```bash -compression_tool test.txt +which cczip +which ccunzip ``` -- Optional: Create a symbolic link + +## Usage + +### 1. Compress a file using cczip + ```bash -sudo ln -s ~/projects/compression_tool/target/release/compression_tool /usr/local/bin/compression_tool +cczip test.txt [test.zip] ``` -- Check if it is working +### 2. Decompress a file using ccunzip + ```bash -which compression_tool -``` \ No newline at end of file +cczip test.dat [test.txt] +``` + + diff --git a/src/ccunzip.rs b/src/ccunzip.rs new file mode 100644 index 0000000..61f5600 --- /dev/null +++ b/src/ccunzip.rs @@ -0,0 +1,30 @@ +use std::{fs::File, io::{self, BufReader, BufWriter}}; +use compression_tool::decompression::DecompressionTool; + +fn main() -> io::Result<()> { + // Get the arguments (compressed file and decompressed output file) + let args: Vec = std::env::args().collect(); + if args.len() < 2 || args.len() > 3 { + println!("Usage: ccunzip [output_file]"); + return Ok(()); + } + + let input_file_path = &args[1]; // Path to the compressed file + let output_file_path = if args.len() == 3 { + &args[2] + } else { + &format!("{}.decompressed", input_file_path) + }; + + let input_file = File::open(input_file_path)?; + let mut reader = BufReader::new(input_file); + + let output_file = File::create(output_file_path)?; + let mut writer = BufWriter::new(&output_file); + + let decompression_tool = DecompressionTool::new(); + decompression_tool.decompress(&mut reader, &mut writer); + + println!("Decompression completed successfully!"); + Ok(()) +} diff --git a/src/cczip.rs b/src/cczip.rs new file mode 100644 index 0000000..3bf8ad3 --- /dev/null +++ b/src/cczip.rs @@ -0,0 +1,33 @@ +use std::fs::File; +use std::io; +use std::io::{BufReader, BufWriter, Write}; +use compression_tool::compression::CompressionTool; + +fn main() -> io::Result<()> { + // Get the arguments (input file and compressed output file) + let args: Vec = std::env::args().collect(); + if args.len() < 2 || args.len() > 3 { + println!("Usage: cczip [output_file]"); + return Ok(()); + } + + let input_file_path = &args[1]; // Path to the input file + let output_file_path = if args.len() == 3 { + &args[2] + } else { + &format!("{}.compressed", input_file_path) + }; + + let input_file = File::open(input_file_path)?; + let mut reader = BufReader::new(input_file); + + let mut compression_tool = CompressionTool::new(); + let output_file = File::create(output_file_path)?; + let mut writer = BufWriter::new(&output_file); + + compression_tool.compress(&mut reader, &mut writer); + writer.flush()?; + + println!("Compression completed successfully!"); + Ok(()) +} \ No newline at end of file diff --git a/src/compression.rs b/src/compression.rs index a88be2a..07bb35c 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -1,7 +1,6 @@ use std::{io::{Write}}; use std::collections::{BTreeMap, HashMap}; use std::io::{Read, Seek, SeekFrom}; -//use bit_io::Writer as BitWriter; use std::collections::BinaryHeap; use crate::huffman::{HuffmanInternalNode, HuffmanLeafNode, HuffmanNode}; @@ -114,5 +113,4 @@ impl CompressionTool { // Write the result to the writer (file or other output) writer.write_all(&result).unwrap(); } - } diff --git a/src/lib.rs b/src/lib.rs index c99ba2a..bc08927 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ pub mod compression; pub mod decompression; -pub mod huffman; +pub mod huffman; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 2a15524..0000000 --- a/src/main.rs +++ /dev/null @@ -1,51 +0,0 @@ -use std::{fs::File, io::{self, Write}}; -use std::io::{BufReader, BufWriter}; -use compression_tool::compression::CompressionTool; -use compression_tool::decompression::DecompressionTool; - -fn main() -> io::Result<()> { - // Get the arguments (input path, compressed output path, and decompressed output path) - //let args: Vec = env::args().collect(); - let arr: [String; 4] = [ - String::from("-"), - String::from("hello.txt"), - String::from("hello.dat"), - String::from("hello2.txt"), - ]; - let args: Vec = Vec::from(arr); - if args.len() != 4 { - println!("Usage: cargo run "); - return Ok(()); - } - - let input_file_path = &args[1]; // Path to the input file - let compressed_file_path = &args[2]; // Path to save the compressed file - let decompressed_file_path = &args[3]; // Path to save the decompressed file - - let input_file = File::open(input_file_path)?; - let mut reader = BufReader::new(input_file); - - // Step 2: Compress the content and save to the compressed file - let mut compression_tool = CompressionTool::new(); - let compressed_file = File::create(compressed_file_path)?; - let mut compressed_writer = BufWriter::new(&compressed_file); - - compression_tool.compress(&mut reader, &mut compressed_writer); - - compressed_writer.flush()?; // Flush the buffer to disk - drop(compressed_writer); // This is important to ensure that the file is finalized - - - // Step 3: Decompress the compressed file and save to the decompressed file - let compressed_file = File::open(compressed_file_path)?; // Reopen the compressed file for reading - let mut reader = BufReader::new(&compressed_file); - let decompressed_file = File::create(decompressed_file_path)?; - let mut decompressed_writer = BufWriter::new(&decompressed_file); - let decompression_tool = DecompressionTool::new(); - - decompression_tool.decompress(&mut reader, &mut decompressed_writer); - - println!("Compression and decompression completed successfully!"); - - Ok(()) -} diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 4b438e9..5738ed1 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -6,7 +6,7 @@ use std::io::{BufReader, BufWriter, Read, Write}; // Test compression and decompression of a file #[test] fn test_compress_decompress() { - let input_file_path = "hello.txt"; + let input_file_path = "tests/test.txt"; let compressed_file_path = "compressed_output.dat"; let decompressed_file_path = "decompressed_output.txt";