-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanCompressor.java
More file actions
75 lines (70 loc) · 2.51 KB
/
HuffmanCompressor.java
File metadata and controls
75 lines (70 loc) · 2.51 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
import java.util.*;
import java.io.*;
public class HuffmanCompressor
{
public static int[] getFrequency(String fileName) {
int[] freq = new int[256];
BufferedReader myFile = null;
try {
myFile = new BufferedReader(new FileReader(fileName));
}
catch (IOException e) { System.out.println("Could not find file:" + fileName); }
try {
int charValue;
while (myFile.ready()) {
charValue = myFile.read();
freq[charValue]+=1;
}
} catch (IOException e) { System.out.println("Error reading the file" + e); }
return freq;
}
public static void compress(String txtFile, String nameOfShortFileToBeCreated, String codeFileToBeCreated)
{
HuffmanTree tree = new HuffmanTree(getFrequency(txtFile));
tree.write(codeFileToBeCreated);
BufferedReader myFile = null;
try {
myFile = new BufferedReader(new FileReader(txtFile));
}
catch (IOException e) { System.out.println("Could not find file:" + txtFile); }
try {
BitOutputStream stream = new BitOutputStream(nameOfShortFileToBeCreated);
int charValue;
while (myFile.ready()) {
charValue = myFile.read();
String code = tree.getCode(charValue);
for(int i = 0; i < code.length();)
{
if(code.charAt(0) == '1')
{
stream.writeBit(1);
}
else if(code.charAt(0) == '0')
{
stream.writeBit(0);
}
code = code.substring(1);
}
}
String end = tree.getCode(256);
for(int i = 0; i < end.length();)
{
if(end.charAt(0) == '1')
{
stream.writeBit(1);
}
else if(end.charAt(0) == '0')
{
stream.writeBit(0);
}
end = end.substring(1);
}
stream.close();
} catch (IOException e) { System.out.println("Error reading the file" + e); }
}
public static void expand(String shortFile, String codeFile, String nameOfNewFileToBeCreated)
{
HuffmanTree tree = new HuffmanTree(codeFile);
tree.decode(new BitInputStream(shortFile), nameOfNewFileToBeCreated);
}
}