-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
266 lines (206 loc) · 5.94 KB
/
Copy pathmain.cpp
File metadata and controls
266 lines (206 loc) · 5.94 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<fstream>
#include<algorithm>
using namespace std;
struct Node{
char ch;
int freq;
Node* left;
Node* right;
Node(char c,int f){
ch=c;
freq=f;
left=right=NULL;
}
};
struct Compare{
bool operator()(Node* a,Node* b){
return a->freq>b->freq;
}
};
Node* buildTree(unordered_map<char ,int>& freq);
void generateCodes(Node* root,string code,unordered_map<char,string>& huff);
string encode(string text,unordered_map<char,string>& huff);
int writeCompressed(string encoded,ofstream& out);
void writeFreqMap(ofstream& out,unordered_map<char,int>& freq);
unordered_map<char,int> readFreqMap(ifstream& in);
string decode(string& bits,Node* root);
double convert_bytes_to_mb(long long bytes);
void find_file_size(ifstream& file);
void find_file_size(ofstream& file);
int main() {
int choice;
cout << "1. Compress File\n2. Decompress File\nEnter choice: ";
cin >> choice;
string filePath;
cout << "Enter file path: ";
cin.ignore();
getline(cin, filePath);
string dir = "";
string filename = filePath;
int pos = filePath.find_last_of("/\\");
if (pos != string::npos) {
dir = filePath.substr(0, pos + 1);
filename = filePath.substr(pos + 1);
}
string nameOnly = filename;
int dotPos = filename.find_last_of('.');
if (dotPos != string::npos) {
nameOnly = filename.substr(0, dotPos);
}
if (choice == 1) {
ifstream in(filePath);
if (!in) {
cout << "Error opening file!\n";
return 1;
}
string text((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
unordered_map<char, int> freq;
for (char c : text) freq[c]++;
Node* root = buildTree(freq);
unordered_map<char, string> huff;
generateCodes(root, "", huff);
string encodedText = encode(text, huff);
string outPath = dir + nameOnly + "Compressed.bin";
ofstream out(outPath, ios::binary);
writeFreqMap(out, freq);
out << encodedText.size() << "\n";
writeCompressed(encodedText, out);
cout << "Compressed file saved at: " << outPath << endl;
find_file_size(in);
cout<<"Compressed ";
find_file_size(out);
in.close();
out.close();
}
else if (choice == 2) {
ifstream in(filePath, ios::binary);
if (!in) {
cout << "Error opening file!\n";
return 1;
}
auto freq = readFreqMap(in);
int totalBits;
in >> totalBits;
in.get();
Node* root = buildTree(freq);
string bits = "";
char byte;
while (in.get(byte)) {
for (int i = 7; i >= 0; i--) {
if (bits.size() == totalBits) break;
bits += ((byte >> i) & 1) ? '1' : '0';
}
}
string decodedText = decode(bits, root);
string finalName = nameOnly;
string suffix = "Compressed";
if (finalName.size() >= suffix.size() &&
finalName.substr(finalName.size() - suffix.size()) == suffix) {
finalName = finalName.substr(0, finalName.size() - suffix.size());
}
string outPath = dir + filename + ".txt";
ofstream out(outPath);
out << decodedText;
cout << "Decompressed file saved at: " << outPath << endl;
find_file_size(out);
in.close();
out.close();
}
else {
cout << "Invalid choice!\n";
}
return 0;
}
Node* buildTree(unordered_map<char,int>& freq){
vector<pair<char,int>> arr(freq.begin(), freq.end());
sort(arr.begin(), arr.end());
priority_queue<Node*,vector<Node*>,Compare> pq;
for(auto &p:arr){
pq.push(new Node(p.first,p.second));
}
while(pq.size()>1){
Node* l=pq.top(); pq.pop();
Node* r=pq.top(); pq.pop();
Node* m=new Node('\0',l->freq+r->freq);
m->left=l;
m->right=r;
pq.push(m);
}
return pq.top();
}
void generateCodes(Node* root,string code,unordered_map<char,string>& huff){
if(!root) return;
if(!root->left && !root->right) huff[root->ch]=code;
generateCodes(root->left,code+"0",huff);
generateCodes(root->right,code+"1",huff);
}
string encode(string text,unordered_map<char,string>& huff){
string res="";
for(char c:text) res+=huff[c];
return res;
}
int writeCompressed(string encoded,ofstream& out){
char byte=0;
int bitCount=0;
for(char bit:encoded){
byte<<=1;
if(bit=='1') byte|=1;
bitCount++;
if(bitCount==8){
out.put(byte);
byte=0;
bitCount=0;
}
}
if(bitCount>0){
byte<<=(8-bitCount);
out.put(byte);
}
return encoded.size();
}
void writeFreqMap(ofstream& out,unordered_map<char,int>& freq){
out<<freq.size()<<"\n";
for(auto &p:freq){
out<<(int)(unsigned char)p.first<<" "<<p.second<<"\n";
}
}
unordered_map<char,int> readFreqMap(ifstream& in){
int n; in>>n;
unordered_map<char,int> freq;
for(int i=0;i<n;i++){
int ascii,f;
in>>ascii>>f;
freq[(char)ascii]=f;
}
return freq;
}
string decode(string& bits,Node* root){
string result="";
Node* curr=root;
for(char bit:bits){
if(bit=='0') curr=curr->left;
else curr=curr->right;
if(!curr->left && !curr->right){
result+=curr->ch;
curr=root;
}
}
return result;
}
double convert_bytes_to_mb(long long bytes){
return ((double)bytes / 1000000);
}
void find_file_size(ifstream& file){
file.seekg(0 , ios::end);
long long size = file.tellg();
cout<<"File size : "<<(convert_bytes_to_mb(size))<<endl;
}
void find_file_size(ofstream& file){
file.seekp(0 , ios::end);
long long size = file.tellp();
cout<<"File size : "<<(convert_bytes_to_mb(size))<<endl;
}