Huffman File Compression

A light weight C++ library implementation of huffman coding compression algorithm

Download as .zip Download as .tar.gz View on GitHub

HCL (Huffman Compression Library)

HCL a custom C++ library that allows file compression using Huffman Compression algorithm.

Codacy Badge GitHub repo size GitHub code size in bytes GitHub top language

Table of Contents

Features

Benchmarks

Below is the output for huffmantool.benchmark() method output.png

Usage

There are 2 ways to try the HCL library

1. Try Online

Try the HCL library online on my custom repl.

2. Try Locally

  1. Clone this repo to your project folder
    git clone https://github.com/bhumijgupta/huffman-compression-library.git hcl

  2. Import Huffman tool header and create new huffmantool object

// Filename main.cpp
#include "hcl/huffmantool.h"

int main(){
    huffmantool ht;
    .
    .
    .
}
  1. Compile and run file
g++ main.cpp && ./a.out

Methods

compressFile

compressFile(string sourceFile, string compressedFile = "") : string

Returns the destination of compressed file.
If the sourceFile provided is invalid or cannot be opened, it returns an empty string.

If no compressedFile is provided, compressedFile is present at the same dir as sourcefile with prefix compressed_

decompressFile

decompressFile(string compressedFile, string retrievedFile = "") : string

Returns the destination of decompressed file.
If the compressedFile provided is invalid or cannot be opened, it reurns an empty string.

If no retrievedFile is provided, retrievedFile is present at the same dir as compressedFile with prefix decompressed_ and removed compressed_ prefix (if present)

benchmark

benchmark(string sourcefile) : void

This method performs compression and decompression on the sourcefile provided and prints the following stats to the stdout.

--------------------------------------------------------------------------------
                              B E N C H M A R K
--------------------------------------------------------------------------------

Filetype                      Filename                      Filesize in bytes

Original
Compressed
Decompressed

--------------------------------------------------------------------------------
Time taken to compress file: XYZ microseconds
Time taken to decompress file: XYZ microseconds
Compression: XY.Z%

Note: For detailed understanding of code refer to Wiki.

Performance Comparison

The newfile.txt is populated using lorem ipsum data from lipsum.com and the following data is noticed after executing main.cpp.

Number of characters Original file size (in bytes) Compressed file size (in bytes)
10 10 36
100 100 116
1000 1000 642
10000 10000 5445
100000 100000 53354

Conclusion

The compression ratio and performance of the Huffman coding depends on the size of input text and the frequency of distinct characters in the file. From the above comparison we can see that the current implementation of huffman coding does not produce positive compression ratio for small files due to the overhead of storing Huffman Tree in the compressed file, which is useful at the time of decompression.

But we notice, as the size of file increases (>=1000 bytes), the compression ratio stays at almost 50%.

Examples

Following are the list of examples

Run tests

The tests are stored in tests/ directory. You need to install googletest framework to run test.

cd tests/
cmake CMakeLists.txt
make
./runTests

License

GitHub

This library is licensed under MIT License. The license file can be found here

Author

forthebadge

Bhumij Gupta

GitHub followers LinkedIn Twitter URL


if(repo.isAwesome || repo.isHelpful){
    StarRepo();
}