-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHunzipping.java
More file actions
357 lines (315 loc) · 9.42 KB
/
Copy pathHunzipping.java
File metadata and controls
357 lines (315 loc) · 9.42 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
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.util.PriorityQueue;
public class Hunzipping {
static PriorityQueue<TREE> pq1 = new PriorityQueue<TREE>();
static int[] freq1 = new int[300];
static String[] ss1 = new String[300]; // INT TO CODE
static String[] btost = new String[300]; // INT TO BIN
static String bigone; // THE BIG STRING
static String temp; // TEMPORARY STRING
static int exbits1; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP
// CODE MULTIPLE OF 8
static int putit; //
static int cntu; // NUMBER OF freqs available
static class TREE implements Comparable<TREE> {
TREE Lchild;
TREE Rchild;
public String deb;
public int Bite;
public int freq1nc;
public int compareTo(TREE T) {
if (this.freq1nc < T.freq1nc)
return -1;
if (this.freq1nc > T.freq1nc)
return 1;
return 0;
}
}
static TREE Root;
/**********************************************************************************
* freing the memory
*********************************************************************************/
public static void initHunzipping() {
int i;
if (Root != null)
fredfs1(Root);
for (i = 0; i < 300; i++)
freq1[i] = 0;
for (i = 0; i < 300; i++)
ss1[i] = "";
pq1.clear();
bigone = ""; // THE BIG STRING
temp = ""; // TEMPORARY STRING
exbits1 = 0; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP CODE
// MULTIPLE OF 8
putit = 0; //
cntu = 0;
}
/**********************************************************************************/
/**********************************************************************************
* dfs1 to free memory
*********************************************************************************/
public static void fredfs1(TREE now) {
if (now.Lchild == null && now.Rchild == null) {
now = null;
return;
}
if (now.Lchild != null)
fredfs1(now.Lchild);
if (now.Rchild != null)
fredfs1(now.Rchild);
}
/**********************************************************************************/
/**********************************************************************************
* dfs1 to make the codes
*********************************************************************************/
// public static void dfs1(TREE now,int code,String st)
public static void dfs1(TREE now, String st) {
now.deb = st;
if ((now.Lchild == null) && (now.Rchild == null)) {
ss1[now.Bite] = st;
return;
}
if (now.Lchild != null)
dfs1(now.Lchild, st + "0");
if (now.Rchild != null)
dfs1(now.Rchild, st + "1");
}
/**********************************************************************************/
/*******************************************************************************
* Making all the nodes in a priority Q making the tree
*******************************************************************************/
public static void MakeNode1() {
int i;
cntu = 0;
for (i = 0; i < 300; i++) {
if (freq1[i] != 0) {
TREE Temp = new TREE();
Temp.Bite = i;
Temp.freq1nc = freq1[i];
Temp.Lchild = null;
Temp.Rchild = null;
pq1.add(Temp);
cntu++;
}
}
TREE Temp1, Temp2;
if (cntu == 0) {
return;
} else if (cntu == 1) {
for (i = 0; i < 300; i++)
if (freq1[i] != 0) {
ss1[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 (pq1.size() != 1) {
TREE Temp = new TREE();
Temp1 = pq1.poll();
Temp2 = pq1.poll();
Temp.Lchild = Temp1;
Temp.Rchild = Temp2;
Temp.freq1nc = Temp1.freq1nc + Temp2.freq1nc;
pq1.add(Temp);
}
Root = pq1.poll();
}
/*******************************************************************************/
/*******************************************************************************
* reading the freq1 from "codes.txt"//updating ss
******************************************************************************/
public static void readfreq1(String cc) {
File filei = new File(cc);
int fey, i;
Byte baital;
try {
FileInputStream file_input = new FileInputStream(filei);
DataInputStream data_in = new DataInputStream(file_input);
cntu = data_in.readInt();
for (i = 0; i < cntu; i++) {
baital = data_in.readByte();
fey = data_in.readInt();
freq1[to(baital)] = fey;
}
data_in.close();
file_input.close();
} catch (IOException e) {
System.out.println("IO exception = " + e);
}
MakeNode1(); // makeing corresponding nodes
if (cntu > 1)
dfs1(Root, ""); // dfs1 to make the codes
for (i = 0; i < 256; i++) {
if (ss1[i] == null)
ss1[i] = "";
}
filei = null;
}
/*******************************************************************************/
/***********************************************************************************
* int to bin string conversion code creating
***********************************************************************************/
public static void createbin() {
int i, j;
String t;
for (i = 0; i < 256; i++) {
btost[i] = "";
j = i;
while (j != 0) {
if (j % 2 == 1)
btost[i] += "1";
else
btost[i] += "0";
j /= 2;
}
t = "";
for (j = btost[i].length() - 1; j >= 0; j--) {
t += btost[i].charAt(j);
}
btost[i] = t;
// System.out.println(btost[i]);
}
btost[0] = "0";
}
/****************************************************************************/
/******************************************************************************
* got yes means temp is a valid code and putit is the code's corresponding
* val
******************************************************************************/
public static int got() {
int i;
for (i = 0; i < 256; i++) {
if (ss1[i].compareTo(temp) == 0) {
putit = i;
return 1;
}
}
return 0;
}
/********************************************************************************/
/***********************************************************************************
* byte to int 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;
}
/***********************************************************************************/
/***********************************************************************************
* convert any string into eight digit string
***********************************************************************************/
public static String makeeight(String b) {
String ret = "";
int i;
int len = b.length();
for (i = 0; i < (8 - len); i++)
ret += "0";
ret += b;
return ret;
}
/***********************************************************************************/
/***********************************************************************************
* unzipping function
**************************************************************************************/
public static void readbin(String zip, String unz) {
File f1 = null, f2 = null;
int ok, bt;
Byte b;
int j, i;
bigone = "";
f1 = new File(zip);
f2 = new File(unz);
try {
FileOutputStream file_output = new FileOutputStream(f2);
DataOutputStream data_out = new DataOutputStream(file_output);
FileInputStream file_input = new FileInputStream(f1);
DataInputStream data_in = new DataInputStream(file_input);
try {
cntu = data_in.readInt();
System.out.println(cntu);
for (i = 0; i < cntu; i++) {
b = data_in.readByte();
j = data_in.readInt();
// System.out.println(ss[to(b)]);
}
exbits1 = data_in.readInt();
System.out.println(exbits1);
} catch (EOFException eof) {
System.out.println("End of File");
}
while (true) {
try {
b = data_in.readByte();
bt = to(b);
bigone += makeeight(btost[bt]);
// System.out.println(bigone);
while (true) {
ok = 1;
temp = "";
for (i = 0; i < bigone.length() - exbits1; i++) {
temp += bigone.charAt(i);
// System.out.println(temp);
if (got() == 1) {
data_out.write(putit);
ok = 0;
String s = "";
for (j = temp.length(); j < bigone.length(); j++) {
s += bigone.charAt(j);
}
bigone = s;
break;
}
}
if (ok == 1)
break;
}
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
file_output.close();
data_out.close();
file_input.close();
data_in.close();
} catch (IOException e) {
System.out.println("IO Exception =: " + e);
}
f1 = null;
f2 = null;
}
/************************************************************************************/
// again improve the to function
// error if only <=one character in the input file
public static void beginHunzipping(String arg1) {
initHunzipping();
readfreq1(arg1);
createbin();
int n = arg1.length();
String arg2 = arg1.substring(0, n - 6);
readbin(arg1, arg2);
initHunzipping();
}
/*
* public static void main (String arg[]) { initHunzipping(); String
* arg1="in.txt.huffz"; int n=arg1.length(); readfreq1(arg1); createbin();
* String arg2=arg1.substring(0,n-6); //mame of the zipped file,name
* afterextracting readbin(arg1,arg2); initHunzipping(); }
*/
}