-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decode.cpp
127 lines (122 loc) · 4.03 KB
/
Decode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/****************************************************************************************************************************************************************
Author:
Vishal Dhiman
Description:
This program decodes huffman encoded file
****************************************************************************************************************************************************************/
#include<iostream>
#include<fstream>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
struct Node //Structure of a Node of a Huffman tree for decoding
{
unsigned char character;
Node* left;
Node* right;
Node(char c,Node* l=NULL,Node* r=NULL)
{
character=c;
left=l;
right=r;
}
};
Node* Make_Huffman_tree(ifstream &input) //Make Tree using the compressed file
{
char ch;
input.get(ch);
if(ch=='1')
{
input.get(ch);
return (new Node(ch));
}
else
{
Node* left=Make_Huffman_tree(input);
Node* right=Make_Huffman_tree(input);
return(new Node(-1,left,right));
}
}
void decode(ifstream &input,string filename,Node* Root,long long int Total_Freq) //Decode each binary symbol according to the tree
{
ofstream output((filename.erase(filename.size()-4)).c_str(),ios::binary);
if(!output.good())
{
perror("Error:\t");
exit(-1);
}
bool eof_flag=false;
char bits_8;
Node* pointer=Root;
while(input.get(bits_8))
{
int counter=7;
while(counter>=0)
{
if(!pointer->left&&!pointer->right)
{
output<<pointer->character;
Total_Freq--;
if(!Total_Freq)
{
eof_flag=true;
break;
}
pointer=Root;
continue;
}
if((bits_8&(1<<counter)))
{
pointer=pointer->right;
counter--;
}
else
{
pointer=pointer->left;
counter--;
}
}
if(eof_flag)
break;
}
output.close();
}
int main()
{
string filename;
//cout<<"Enter the Filename:\t";
filename="";
//cin>>filename;
ifstream input_file(filename.c_str(),ios::binary); //Open File
if(!input_file.good()) //Check if stream is open
{
perror("Error:\t");
exit(-1);
}
if(filename.find(".huf")==string::npos) //Check if file has .huf extension
{
cout<<"Error: File is already decompressed\n\n";
exit(-1);
}
cout<<"\nDecompressing the file....";
clock_t start_time=clock();
long long int Total_freq=0;
char ch;
while(input_file.get(ch)) //read Original total frequency from file
{
if(ch==',')
break;
Total_freq*=10;
Total_freq+=ch-'0';
}
Node* Huffman_tree=Make_Huffman_tree(input_file); //Remake Huffman tree from file
input_file.get(ch); //Read Extra space between compressed data and tree from file
decode(input_file,filename,Huffman_tree,Total_freq);
input_file.close();
clock_t stop_time=clock();
if(remove(filename.c_str())!=0) //Delete compressed file
perror("Error deleting the compressed file:\t");
cout<<"\n\n****File Decompressed Successfully! :-)****\n\n";
cout<<"Time taken to Compress:\t"<<double(stop_time-start_time)/CLOCKS_PER_SEC<<" seconds\n\n"; //Display timer after Decoding
}