Skip to content

Commit

Permalink
cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Murat Yildirim committed Dec 11, 2024
1 parent 8fbda47 commit f03825d
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 78 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
54 changes: 42 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
cczip test.dat [test.txt]
```


30 changes: 30 additions & 0 deletions src/ccunzip.rs
Original file line number Diff line number Diff line change
@@ -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<String> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 {
println!("Usage: ccunzip <input_file> [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(())
}
33 changes: 33 additions & 0 deletions src/cczip.rs
Original file line number Diff line number Diff line change
@@ -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<String> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 {
println!("Usage: cczip <input_file> [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(())
}
2 changes: 0 additions & 2 deletions src/compression.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -114,5 +113,4 @@ impl CompressionTool {
// Write the result to the writer (file or other output)
writer.write_all(&result).unwrap();
}

}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod compression;
pub mod decompression;
pub mod huffman;
pub mod huffman;
51 changes: 0 additions & 51 deletions src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit f03825d

Please sign in to comment.