This class compresses files and folders using Huffman Compression Algorithm. More...
#include <Compressor.h>
Public Member Functions | |
Compressor () | |
~Compressor () | |
void | compressFile (const std::string &infileName) |
Compresses a single input file. More... | |
void | compressFolder (const std::string &directoryName) |
Compresses a directory and its entire content recursively. More... | |
void | compressFiles (std::initializer_list< std::string > infileNames) |
Compresses multiple source files into single compressed(.huf) file. More... | |
Private Member Functions | |
BinNode * | createHuffmanTree () |
Creates a Huffman Tree form unique characters along with their frequency of occurrences. More... | |
void | generateHuffmanCode (BinNode *rootNode, std::string codeString) |
Generates prefix code for each unique characters in the source. More... | |
void | clear () |
Resets all the attributes for next compression operation. More... | |
void | deleteTree (BinNode *node) |
Frees all heap storage associated with the Huffman Tree. More... | |
void | readFrequency () |
Reads entire input file and finds frequency of each unique characters. More... | |
void | scanFile (const fs::path &infilePath) |
Validates input file path and proceeds on reading frequency. More... | |
void | writeTree (std::ofstream &writer, BinNode *head) |
Write the entire Huffman tree in to the file header section using a pre-order traversal algorithm. More... | |
void | writeHeader (const std::string &inputName, std::ofstream &writer) |
Write the header section to compressed file. More... | |
void | writeBody (char &chr, int &bufferSize, const std::string &infileName, std::ofstream &writer) |
Write the body section to compressed file. More... | |
fs::path | writeIntoFile (const std::string &infileName) |
Write the header and body section to compressed file. More... | |
void | compress (const std::string &infileName) |
Utility function to compress file. More... | |
Private Attributes | |
HashMap< char, int > | frequency |
Frequency of occurrence for each unique symbol in the source. More... | |
HashMap< char, std::string > | codeMap |
Prefix-free binary code for each value of the source symbol. More... | |
BinNode * | rootNode |
Root node for Huffman tree. More... | |
std::vector< fs::path > | inputFiles |
List of input files given by the user. More... | |
std::ifstream | infile |
Instance of ifstream class for reading characters from source. More... | |
This class compresses files and folders using Huffman Compression Algorithm.
It can compress single file or multiple files or an entire directory recursively into a compressed file (.huf file).
Definition at line 24 of file Compressor.h.
Compressor::Compressor | ( | ) |
Definition at line 3 of file Compressor.cpp.
Compressor::~Compressor | ( | ) |
Definition at line 5 of file Compressor.cpp.
|
private |
Resets all the attributes for next compression operation.
Definition at line 18 of file Compressor.cpp.
|
private |
Utility function to compress file.
Definition at line 152 of file Compressor.cpp.
void Compressor::compressFile | ( | const std::string & | infileName | ) |
Compresses a single input file.
infileName | source file path to be compressed. |
Definition at line 204 of file Compressor.cpp.
void Compressor::compressFiles | ( | std::initializer_list< std::string > | infileNames | ) |
Compresses multiple source files into single compressed(.huf) file.
infileNames | list of source files to be compressed. |
Definition at line 226 of file Compressor.cpp.
void Compressor::compressFolder | ( | const std::string & | directoryName | ) |
Compresses a directory and its entire content recursively.
directoryName | source directory path to be compressed. |
Definition at line 213 of file Compressor.cpp.
|
private |
Creates a Huffman Tree form unique characters along with their frequency of occurrences.
create the Huffman's tree out of frequency map
Definition at line 27 of file Compressor.cpp.
|
private |
Frees all heap storage associated with the Huffman Tree.
Definition at line 9 of file Compressor.cpp.
|
private |
Generates prefix code for each unique characters in the source.
rootNode | root node of the Huffman Tree. |
codeString | string which is recursively populated with prefix code. |
Create a CodeMap (Key/ Value pair) with character as key and its Huffman code as value by assigning each symbol with its path from root node to its node, with left being 0 and right being 1.
Definition at line 44 of file Compressor.cpp.
|
private |
Reads entire input file and finds frequency of each unique characters.
Populates HashMap<char, int> frequency.
Definition at line 55 of file Compressor.cpp.
|
private |
Validates input file path and proceeds on reading frequency.
Definition at line 62 of file Compressor.cpp.
|
private |
Write the body section to compressed file.
Body section of compressed file contains corresponding prefix code of each unique characters in the source. Prefix code are grouped into 8 and its corresponding character code is written to the file
Definition at line 104 of file Compressor.cpp.
|
private |
Write the header section to compressed file.
Header section of compressed file contains tree and other metaData required during decompression. Meta data includes number of input files, each file size and file path
Definition at line 81 of file Compressor.cpp.
|
private |
Write the header and body section to compressed file.
possibility of extra 8-bits
Definition at line 124 of file Compressor.cpp.
|
private |
Write the entire Huffman tree in to the file header section using a pre-order traversal algorithm.
When we visit a leaf node, write bit 1 followed by the symbol in 8 bits. And when we visit an internal node (or the root node), simply write bit 0 only.
Definition at line 70 of file Compressor.cpp.
|
private |
Prefix-free binary code for each value of the source symbol.
Definition at line 30 of file Compressor.h.
|
private |
Frequency of occurrence for each unique symbol in the source.
Definition at line 27 of file Compressor.h.
|
private |
Instance of ifstream class for reading characters from source.
Definition at line 39 of file Compressor.h.
|
private |
|
private |