-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHzipping.java
More file actions
361 lines (310 loc) · 9.29 KB
/
Copy pathHzipping.java
File metadata and controls
361 lines (310 loc) · 9.29 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package prog;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.PriorityQueue;
//if the frequency of a byte is more than 2^32 then there will be problem
public class Hzipping {
static PriorityQueue<TREE> pq = new PriorityQueue<TREE>();
static int[] freq = new int[300];
static String[] ss = new String[300];
static int exbits;
static byte bt;
static int cnt; // number of different characters
// for keeping frequncies of all the bytes
// main tree class
static class TREE implements Comparable<TREE> {
TREE Lchild;
TREE Rchild;
public String deb;
public int Bite;
public int Freqnc;
public int compareTo(TREE T) {
if (this.Freqnc < T.Freqnc)
return -1;
if (this.Freqnc > T.Freqnc)
return 1;
return 0;
}
}
static TREE Root;
/*******************************************************************************
* calculating frequence of file fname
******************************************************************************/
public static void CalFreq(String fname) {
File file = null;
Byte bt;
file = new File(fname);
try {
FileInputStream file_input = new FileInputStream(file);
DataInputStream data_in = new DataInputStream(file_input);
while (true) {
try {
bt = data_in.readByte();
freq[to(bt)]++;
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
file_input.close();
data_in.close();
} catch (IOException e) {
System.out.println("IO Exception =: " + e);
}
file = null;
}
/************************************** ============ ************************/
/***********************************************************************************
* byte to binary conversion
***********************************************************************************/
public static int to(Byte b) {
int ret = b;
if (ret < 0) {
ret = ~b;
ret = ret + 1;
ret = ret ^ 255;
ret += 1;
}
return ret;
}
/***********************************************************************************/
/**********************************************************************************
* freing the memory
*********************************************************************************/
public static void initHzipping() {
int i;
cnt = 0;
if (Root != null)
fredfs(Root);
for (i = 0; i < 300; i++)
freq[i] = 0;
for (i = 0; i < 300; i++)
ss[i] = "";
pq.clear();
}
/**********************************************************************************/
/**********************************************************************************
* dfs to free memory
*********************************************************************************/
public static void fredfs(TREE now) {
if (now.Lchild == null && now.Rchild == null) {
now = null;
return;
}
if (now.Lchild != null)
fredfs(now.Lchild);
if (now.Rchild != null)
fredfs(now.Rchild);
}
/**********************************************************************************/
/**********************************************************************************
* dfs to make the codes
*********************************************************************************/
public static void dfs(TREE now, String st) {
now.deb = st;
if ((now.Lchild == null) && (now.Rchild == null)) {
ss[now.Bite] = st;
return;
}
if (now.Lchild != null)
dfs(now.Lchild, st + "0");
if (now.Rchild != null)
dfs(now.Rchild, st + "1");
}
/**********************************************************************************/
/*******************************************************************************
* Making all the nodes in a priority Q making the tree
*******************************************************************************/
public static void MakeNode() {
int i;
pq.clear();
for (i = 0; i < 300; i++) {
if (freq[i] != 0) {
TREE Temp = new TREE();
Temp.Bite = i;
Temp.Freqnc = freq[i];
Temp.Lchild = null;
Temp.Rchild = null;
pq.add(Temp);
cnt++;
}
}
TREE Temp1, Temp2;
if (cnt == 0) {
return;
} else if (cnt == 1) {
for (i = 0; i < 300; i++)
if (freq[i] != 0) {
ss[i] = "0";
break;
}
return;
}
// will there b a problem if the file is empty
// a bug is found if there is only one character
while (pq.size() != 1) {
TREE Temp = new TREE();
Temp1 = pq.poll();
Temp2 = pq.poll();
Temp.Lchild = Temp1;
Temp.Rchild = Temp2;
Temp.Freqnc = Temp1.Freqnc + Temp2.Freqnc;
pq.add(Temp);
}
Root = pq.poll();
}
/*******************************************************************************/
/*******************************************************************************
* encrypting
*******************************************************************************/
public static void encrypt(String fname) {
File file = null;
file = new File(fname);
try {
FileInputStream file_input = new FileInputStream(file);
DataInputStream data_in = new DataInputStream(file_input);
while (true) {
try {
bt = data_in.readByte();
freq[bt]++;
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
file_input.close();
data_in.close();
} catch (IOException e) {
System.out.println("IO Exception =: " + e);
}
file = null;
}
/*******************************************************************************/
/*******************************************************************************
* fake zip creates a file "fakezip.txt" where puts the final binary codes
* of the real zipped file
*******************************************************************************/
public static void fakezip(String fname) {
File filei, fileo;
int i;
filei = new File(fname);
fileo = new File("fakezipped.txt");
try {
FileInputStream file_input = new FileInputStream(filei);
DataInputStream data_in = new DataInputStream(file_input);
PrintStream ps = new PrintStream(fileo);
while (true) {
try {
bt = data_in.readByte();
ps.print(ss[to(bt)]);
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
file_input.close();
data_in.close();
ps.close();
} catch (IOException e) {
System.out.println("IO Exception =: " + e);
}
filei = null;
fileo = null;
}
/*******************************************************************************/
/*******************************************************************************
* real zip according to codes of fakezip.txt (fname)
*******************************************************************************/
public static void realzip(String fname, String fname1) {
File filei, fileo;
int i, j = 10;
Byte btt;
filei = new File(fname);
fileo = new File(fname1);
try {
FileInputStream file_input = new FileInputStream(filei);
DataInputStream data_in = new DataInputStream(file_input);
FileOutputStream file_output = new FileOutputStream(fileo);
DataOutputStream data_out = new DataOutputStream(file_output);
data_out.writeInt(cnt);
for (i = 0; i < 256; i++) {
if (freq[i] != 0) {
btt = (byte) i;
data_out.write(btt);
data_out.writeInt(freq[i]);
}
}
long texbits;
texbits = filei.length() % 8;
texbits = (8 - texbits) % 8;
exbits = (int) texbits;
data_out.writeInt(exbits);
while (true) {
try {
bt = 0;
byte ch;
for (exbits = 0; exbits < 8; exbits++) {
ch = data_in.readByte();
bt *= 2;
if (ch == '1')
bt++;
}
data_out.write(bt);
} catch (EOFException eof) {
int x;
if (exbits != 0) {
for (x = exbits; x < 8; x++) {
bt *= 2;
}
data_out.write(bt);
}
exbits = (int) texbits;
System.out.println("extrabits: " + exbits);
System.out.println("End of File");
break;
}
}
data_in.close();
data_out.close();
file_input.close();
file_output.close();
System.out.println("output file's size: " + fileo.length());
} catch (IOException e) {
System.out.println("IO exception = " + e);
}
filei.delete();
filei = null;
fileo = null;
}
/*******************************************************************************/
/*
* public static void main (String[] args) { initHzipping();
* CalFreq("in.txt"); // calculate the frequency of each digit MakeNode();
* // makeing corresponding nodes if(cnt>1) dfs(Root,""); // dfs to make the
* codes fakezip("in.txt"); // fake zip file which will have the binary of
* the input to fakezipped.txt file
* realzip("fakezipped.txt","in.txt"+".huffz"); // making the real zip
* according the fakezip.txt file initHzipping();
*
* }
*/
public static void beginHzipping(String arg1) {
initHzipping();
CalFreq(arg1); // calculate the frequency of each digit
MakeNode(); // makeing corresponding nodes
if (cnt > 1)
dfs(Root, ""); // dfs to make the codes
fakezip(arg1); // fake zip file which will have the binary of the input
// to fakezipped.txt file
realzip("fakezipped.txt", arg1 + ".huffz"); // making the real zip
// according the fakezip.txt
// file
initHzipping();
}
}