传说中没有注释的代码
||
\/
import java.io.*;
import java.util.*;
public class HuffmanCodeDemo {
public static void main(String[] args) {
String srcFile = "C:\\Users\\Administrator\\Desktop\\lj.zip";
String dstFile = "C:\\Users\\Administrator\\Desktop\\ywl.bmp";
// zipFile(srcFile, dstFile);
decompressFile(srcFile, dstFile);
}
public static void zipFile(String srcFile, String dstFile) {
InputStream is = null;
OutputStream os = null;
ObjectOutputStream oos = null;
try {
is = new FileInputStream(srcFile);
byte[] b = new byte[is.available()];
is.read(b);
byte[] bytes = huffmanZip(b);
os = new FileOutputStream(dstFile);
oos = new ObjectOutputStream(os);
oos.writeObject(bytes);
oos.writeObject(huffmanCode);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
oos.close();
os.close();
is.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void decompressFile(String srcFile, String dstFile) {
InputStream is = null;
ObjectInputStream ois = null;
OutputStream os = null;
try {
is = new FileInputStream(srcFile);
ois = new ObjectInputStream(is);
byte[] bytes = (byte[]) ois.readObject();
HashMap<Byte, String> map = (HashMap<Byte, String>) ois.readObject();
byte[] decode = decode(map, bytes);
os = new FileOutputStream(dstFile);
os.write(decode);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
os.close();
ois.close();
is.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static byte[] huffmanZip(byte[] bytes) {
ArrayList<Node> nodes = getNodes(bytes);
Node huffmanTreeRoot = createHuffmanTree(nodes);
getCodes(huffmanTreeRoot);
byte[] zip = zip(bytes, huffmanCode);
return zip;
}
public static byte[] decode(Map<Byte, String> huffmanCodes, byte[] huffmanBytes) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < huffmanBytes.length; i++) {
byte b = huffmanBytes[i];
boolean flag = !(i == (huffmanBytes.length - 1));
stringBuilder.append(byteToBitString(flag, b));
}
Map<String, Byte> map = new HashMap<>();
for (Map.Entry<Byte, String> entry : huffmanCodes.entrySet()) {
map.put(entry.getValue(), entry.getKey());
}
List<Byte> list = new ArrayList<>();
for (int i = 0; i < stringBuilder.length(); ) {
int count = 1;
boolean flag = true;
Byte b = null;
while (flag) {
String key = stringBuilder.substring(i, i + count);
b = map.get(key);
if (b == null) {
count++;
} else {
flag = false;
}
}
list.add(b);
i += count;
}
int size = list.size();
byte[] b = new byte[size];
for (int i = 0; i < size; i++) {
b[i] = list.get(i);
}
return b;
}
public static String byteToBitString(boolean flag, byte b) {
int temp = b;
if (flag) {
temp |= 256;
}
String str = Integer.toBinaryString(temp);
if (flag) {
return str.substring(str.length() - 8);
} else {
return str;
}
}
public static ArrayList<Node> getNodes(byte[] bytes) {
ArrayList<Node> list = new ArrayList<>();
HashMap<Byte, Integer> counts = new HashMap<>();
int len = bytes.length;
for (int i = 0; i < len; i++) {
Integer count = counts.get(bytes[i]);
if (count == null) {
counts.put(bytes[i], 1);
} else {
counts.put(bytes[i], count + 1);
}
}
for (Map.Entry<Byte, Integer> entry : counts.entrySet()) {
list.add(new Node(entry.getKey(), entry.getValue()));
}
return list;
}
public static Node createHuffmanTree(ArrayList<Node> nodes) {
while (nodes.size() > 1) {
Collections.sort(nodes);
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);
nodes.remove(leftNode);
nodes.remove(rightNode);
Node parent = new Node(null, leftNode.weight + rightNode.weight);
parent.left = leftNode;
parent.right = rightNode;
nodes.add(parent);
}
return nodes.get(0);
}
static HashMap<Byte, String> huffmanCode = new HashMap<>();
public static void getCodes(Node huffmanTreeRoot) {
StringBuilder stringBuilder = new StringBuilder();
if (huffmanTreeRoot != null) {
getCodes(huffmanTreeRoot.left, "0", stringBuilder);
getCodes(huffmanTreeRoot.right, "1", stringBuilder);
} else {
System.out.println("赫夫曼树根节点为空~");
}
}
public static void getCodes(Node node, String code, StringBuilder builder) {
StringBuilder stringBuilder = new StringBuilder(builder);
if (node != null) {
stringBuilder.append(code);
if (node.data != null) {
huffmanCode.put(node.data, stringBuilder.toString());
}
getCodes(node.left, "0", stringBuilder);
getCodes(node.right, "1", stringBuilder);
}
}
public static byte[] zip(byte[] bytes, Map<Byte, String> huffmanCodes) {
StringBuilder builder = new StringBuilder();
for (byte b : bytes) {
builder.append(huffmanCodes.get(b));
}
int len = (builder.length() + 7) / 8;
byte[] zipByte = new byte[len];
int index = 0;
String str;
for (int i = 0; i < builder.length(); i += 8) {
if (i + 8 <= builder.length()) {
str = builder.substring(i, i + 8);
} else {
str = builder.substring(i);
}
byte b = (byte) Integer.parseInt(str, 2);
zipByte[index++] = b;
}
return zipByte;
}
}
class Node implements Comparable<Node> {
public Byte data;
public int weight;
public Node left;
public Node right;
public Node(Byte data, int weight) {
this.data = data;
this.weight = weight;
}
@Override
public int compareTo(Node o) {
return this.weight - o.weight;
}
}