├── 12mb.txt ├── Compress.cpp ├── Compress.exe ├── Decompress.cpp ├── Decompress.exe ├── Decompressed.txt ├── README.md └── Screenshot (52).png /Compress.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MayurDeshmukh10/Text-File-Compression-using-Huffman-Algorithm/a5e79f372f759dffe30d8638a0001c8dfb621b7a/Compress.cpp -------------------------------------------------------------------------------- /Compress.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MayurDeshmukh10/Text-File-Compression-using-Huffman-Algorithm/a5e79f372f759dffe30d8638a0001c8dfb621b7a/Compress.exe -------------------------------------------------------------------------------- /Decompress.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | string output; 9 | int count = 0; 10 | unsigned long int total_traversal = 0; 11 | //======================================================= 12 | class BitwiseRead { 13 | private: 14 | char buf; 15 | //int first; 16 | int nbits; //how many bits have been read from the buffer 17 | istream& in; 18 | public: 19 | 20 | /* Initialize a BitwiseRead that will use the given istream for input 21 | * */ 22 | BitwiseRead(istream & is); 23 | 24 | /* Fill the buffer from the input */ 25 | void fill(); 26 | 27 | /* Read the next bit from the bit buffer. Fill the buffer form the input 28 | * stream first if needed. 29 | * Return 1 if the bit read is 1, 0 if bit read is 0 30 | * */ 31 | int readBit(); 32 | }; 33 | 34 | BitwiseRead::BitwiseRead(istream & is) : in(is) { 35 | buf = 0; 36 | nbits = 8; 37 | } 38 | 39 | 40 | 41 | void BitwiseRead::fill() 42 | { 43 | buf = in.get(); 44 | nbits = 0; 45 | } 46 | 47 | int BitwiseRead::readBit() 48 | { 49 | if(nbits == 8) { 50 | fill(); 51 | } 52 | 53 | //uses a mask to extract the nbits'th bit 54 | unsigned char mask = 1; 55 | mask = mask << (7-nbits); 56 | mask = mask & buf; 57 | nbits++; 58 | if(mask == 0) { 59 | return 0; 60 | } 61 | else { 62 | return 1; 63 | } 64 | } 65 | //==================================================== 66 | class Huffman 67 | { 68 | private: 69 | /*** Node structure ***/ 70 | class node 71 | { 72 | public: 73 | char data; 74 | node * left,* right; 75 | 76 | node(char item) // node constructor 77 | { 78 | data = item; 79 | left = right = 0; 80 | } 81 | }; 82 | typedef node * nodePointer; 83 | public: 84 | Huffman(); 85 | void buildDecodingTree(ifstream & codeIn); 86 | void insert(char ch, string code); 87 | char decode(BitwiseRead &in); 88 | void printTree(ostream & out, nodePointer root, int indent); 89 | void displayDecodingTree(ostream & out); 90 | private: 91 | nodePointer root; 92 | }; 93 | inline Huffman::Huffman() 94 | { 95 | root = new node('*'); 96 | } 97 | inline void Huffman::displayDecodingTree(ostream & out) 98 | { 99 | printTree(out, root, 0); 100 | } 101 | //====================================================================================== 102 | void Huffman::buildDecodingTree(ifstream & codeIn) 103 | { 104 | char ch; 105 | string code; 106 | for (;;) 107 | { 108 | if ( codeIn.eof() ) return; 109 | codeIn >> ch >> code; 110 | cout<<"\n"<left == 0) // create node along path 129 | p->left = new Huffman::node('*'); 130 | p = p->left; 131 | break; 132 | case '1' : 133 | if (p->right == 0) // create node along path 134 | p->right = new Huffman::node('*'); 135 | p = p->right; 136 | break; 137 | default: 138 | cerr << "*** Illegal character in code ***\n"; 139 | exit(1); 140 | } 141 | } 142 | p->data = ch; 143 | } 144 | 145 | 146 | char Huffman::decode(BitwiseRead &in) //To decode from huffman tree 147 | { 148 | Huffman::nodePointer p; 149 | p = root; 150 | int bit; 151 | while(true) 152 | { 153 | bit = in.readBit(); 154 | //count++; 155 | if(bit == 1) 156 | { 157 | p = p -> right; 158 | } 159 | if(bit == 0) 160 | { 161 | p = p -> left; 162 | } 163 | if(p->right == NULL || p->left == NULL) 164 | { 165 | break; 166 | } 167 | } 168 | total_traversal++; 169 | return (p->data); 170 | } 171 | 172 | void Huffman::printTree(ostream & out, Huffman::nodePointer root,int indent) //To print huffman tree 173 | { 174 | if (root != 0) 175 | { 176 | printTree(out, root->right, indent + 4); 177 | out << setw(indent) << " " << root->data << endl; 178 | printTree(out, root->left, indent + 4); 179 | } 180 | } 181 | 182 | string str; 183 | 184 | 185 | int main() 186 | { 187 | int total_bits_written; 188 | int current_bits = 0; 189 | char data; 190 | char filename[32]; 191 | cout<<"\n\t===================================="; 192 | cout<<"\n\t\t Text File Decompressor\n"; 193 | cout<<"\t===================================="; 194 | cout << "\n\nEnter name of code file: "; 195 | cin >> filename; 196 | ifstream codestream(filename); 197 | if (!codestream.is_open()) 198 | { 199 | cout <<"Error !!! Cannot open code file.\n"; 200 | exit(1); 201 | } 202 | Huffman h; 203 | h.buildDecodingTree(codestream); 204 | cout << "Here is the Huffman decoding tree:\n"; 205 | h.displayDecodingTree(cout); 206 | cout << endl; 207 | cout << "\nEnter Name of Compressed file : "; 208 | cin >> filename; 209 | ifstream in; 210 | ofstream fout; 211 | fout.open("Decompressed.txt"); 212 | in.open(filename,ios::binary); 213 | BitwiseRead os(in); 214 | if (!in.is_open()) 215 | { 216 | cout << "Error !!! Cannot open Compressed file.\n"; 217 | exit(1); 218 | } 219 | cout<<"\nProcessing..... Plz Wait"; 220 | char ch3; 221 | ch3=char(129); 222 | while(true) 223 | { 224 | 225 | data = h.decode(os); 226 | if( data == ch3) 227 | { 228 | break; 229 | } 230 | current_bits++; 231 | fout << data; 232 | } 233 | //cout<<"\nCount = "<>l; 239 | return(0); 240 | } 241 | -------------------------------------------------------------------------------- /Decompress.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MayurDeshmukh10/Text-File-Compression-using-Huffman-Algorithm/a5e79f372f759dffe30d8638a0001c8dfb621b7a/Decompress.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Text File Compression using Huffman Algorithm 2 | It is a text file, script file Compression and Decompression Program which uses Huffman Algorithm for Compression and Decompression 3 | 4 | For Compression -
5 | 1.Run compress.exe or compile source code
6 | 2.Enter name of file to compress
7 | 8 | **Note - Compressed File generated after compression will have .cmp extension 9 | 10 | For Decompression -
11 | 1.Run decompress.exe or compile source code
12 | 2.Enter same file name with .cmp extension e.g. - abc.cmp
13 | 3.Decompressed file will be generated with name Decompressed.txt
14 | 15 | Result -
16 | On average every text file get compressed upto 50% of total size. 17 | -------------------------------------------------------------------------------- /Screenshot (52).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MayurDeshmukh10/Text-File-Compression-using-Huffman-Algorithm/a5e79f372f759dffe30d8638a0001c8dfb621b7a/Screenshot (52).png --------------------------------------------------------------------------------