-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.cpp
More file actions
127 lines (97 loc) · 2.96 KB
/
Copy pathcompressor.cpp
File metadata and controls
127 lines (97 loc) · 2.96 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
#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
#include<string>
#include "huffman.h"
using namespace std;
void generateCode(Node* root,string code,string* huffmanCodes){
if(root == nullptr){
return;
}
if(root->left == nullptr && root->right == nullptr){
huffmanCodes[(unsigned char)root->ch] = code;
return;
}
generateCode(root->left,code+"0",huffmanCodes);
generateCode(root->right,code+"1",huffmanCodes);
}
void writeCompressedFile(const string& inputPath,const string& outputPath,int* frequency,string* huffmanCodes){
ifstream inFile(inputPath,ios::binary);
ofstream outFile(outputPath,ios::binary);
if(!inFile || !outFile){
cout << "Error opening files during the compression phase" << endl;
return;
}
outFile.write(reinterpret_cast<const char*>(frequency),256*sizeof(int));
unsigned char bitbuffer = 0;
int bitcount = 0;
char ch;
while(inFile.get(ch)){
string code = huffmanCodes[(unsigned char)ch];
for(char bit : code){
bitbuffer = bitbuffer << 1;
if(bit == '1'){
bitbuffer = bitbuffer | 1;
}
bitcount++;
if(bitcount == 8){
outFile.put(bitbuffer);
bitbuffer = 0;
bitcount = 0;
}
}
}
if(bitcount > 0){
bitbuffer = bitbuffer << (8-bitcount);
outFile.put(bitbuffer);
}
inFile.close();
outFile.close();
cout << "Compression complete. Binary file saved." << endl;
}
int main(void){
// Only txt files are supported
ifstream file("example.txt"); //Change the filename inside the brackets to compress that file
if(!file){
cout << "Unable to open file" << endl;
return 1;
}
char ch;
int frequency[256] = {0};
while(file.get(ch)){
frequency[(unsigned char)ch]++;
}
file.close();
priority_queue<Node*, vector<Node*>, CompareNodes> minHeap;
for(int i=0;i<256;i++){
if(frequency[i] > 0){
minHeap.push(new Node((char)i, frequency[i]));
}
}
if(minHeap.empty()){
cout << "The file is empty nothing to compress." << endl;
return 0;
}
while (minHeap.size() > 1){
Node* leftChild = minHeap.top();
minHeap.pop();
Node* rightChild = minHeap.top();
minHeap.pop();
int combinedFrequency = leftChild->freq + rightChild->freq;
Node* parentNode = new Node('\0',combinedFrequency);
parentNode->left = leftChild;
parentNode->right = rightChild;
minHeap.push(parentNode);
}
Node* root = minHeap.top();
string code[256];
if(root->left == nullptr && root->right == nullptr){
code[(unsigned char)root->ch] = "0";
}
else{
generateCode(root,"",code);
}
writeCompressedFile("example.txt","compressed.bin",frequency,code);
return 0;
}