-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDecompressor.java
More file actions
34 lines (24 loc) · 1.02 KB
/
Copy pathFileDecompressor.java
File metadata and controls
34 lines (24 loc) · 1.02 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
import java.io.*;
import java.nio.file.*;
import java.util.*;
@SuppressWarnings("unchecked")
public class FileDecompressor {
public static void decompress(String inputFile, String outputFile) throws Exception {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(inputFile))) {
Map<Byte, Integer> freqMap = (Map<Byte, Integer>) ois.readObject();
HuffmanNode root = HuffmanTree.buildTree(freqMap);
BitInputStream bis = new BitInputStream(ois);
ByteArrayOutputStream output = new ByteArrayOutputStream();
HuffmanNode current = root;
int bit;
while ((bit = bis.readBit()) != -1) {
current = (bit == 0) ? current.left : current.right;
if (current.left == null && current.right == null) {
output.write(current.data);
current = root;
}
}
Files.write(Paths.get(outputFile), output.toByteArray());
}
}
}