-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompression.java
More file actions
114 lines (89 loc) · 2.6 KB
/
decompression.java
File metadata and controls
114 lines (89 loc) · 2.6 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
import java.util.*;
import java.io.*;
/**
*
* @author Nour Benmohamed
*this is very inspired by the file input demo I found in the day 13
*/
public class decompression {
protected BufferedBitWriter bitOutput;
protected String compressedPathName;
protected String decompressedPathName;
protected BufferedWriter output;
protected BinaryTree<typeE> node;
protected boolean bit;
protected String code;
protected BufferedBitReader bitInput;
// constructor
public decompression (BinaryTree<typeE> T,String name,String dename) {
compressedPathName= name;
node= T;
decompressedPathName = dename;
}
/**
* this method takes the huffman tree and decodes the compressed file
* @param T
*/
public void decompress(BinaryTree<typeE> T) {
char c;
try {
System.out.println(compressedPathName);
bitInput = new BufferedBitReader(compressedPathName);
System.out.println(decompressedPathName);
output = new BufferedWriter(new FileWriter(decompressedPathName));
}
catch (FileNotFoundException e) {
System.out.println("cannot open file");
}
catch (IOException e) {
System.out.println("IO exception");
}
//reading the file
try {
// this while loop will go thru the tree until it reaches a leaf and write out that character
node = T;
while (bitInput.hasNext()) {
bit = bitInput.readBit();
System.out.println(bit);
if (bit == true) {
//System.out.println("got here1");
//System.out.println(bit);
node=node.getRight();
if (node.isLeaf()) {
///System.out.println("got here2");
c = node.getData().getC();
//System.out.println("got here3");
output.write(c);
node=T;
System.out.println(c);
}
// System.out.println("got here4");
}
else if (bit == false) {
node=node.getLeft();
System.out.println("got here5");
if (node.isLeaf()) {
System.out.println("got here6");
c = node.getData().getC();
output.write(c);
node=T;
System.out.println(c);
}
}
}
}
catch (IOException e) {
System.out.println("IO error while reading");
}
//closing the file
finally {
try {
output.close();
bitInput.close();
}
catch (IOException e) {
System.out.println("cannot close file");
}
}
}
}