赫夫曼编码是无损压缩,可以完整恢复
赫夫曼树排序方法不同,则赫夫曼编码不同。
package calculate;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HuffmanCode {
public static void main(String[] args) throws IOException {
String str = "i like like like java do you like a java";
byte[] contentBytes = str.getBytes();
/*
System.out.println(contentBytes.length); //40
List<Node2> nodes = getNodes(contentBytes);
System.out.println(getNodes(contentBytes));
//测试创建的二叉树
System.out.println("哈夫曼树");
Node2 huffmanTreeRoot = createHuffmanTree(nodes);
System.out.println("前序遍历");
huffmanTreeRoot.preOrder();
//测试是否生成对应的哈夫曼编码
System.out.println("哈夫曼编码");
getCodes(huffmanTreeRoot);
System.out.println(huffmanCodes);
//测试压缩文件
System.out.println("压缩文件");
byte[] huffmanCodeBytes = zip(contentBytes,huffmanCodes);
System.out.println(Arrays.toString(huffmanCodeBytes));
//输出
40
[Node2 [data=32weight=9], Node2 [data=97weight=5], Node2 [data=100weight=1], Node2 [data=101weight=4], Node2 [data=117weight=1], Node2 [data=118weight=2], Node2 [data=105weight=5], Node2 [data=121weight=1], Node2 [data=106weight=2], Node2 [data=107weight=4], Node2 [data=108weight=4], Node2 [data=111weight=2]]
哈夫曼树
前序遍历
Node2 [data=nullweight=40]
Node2 [data=nullweight=17]
Node2 [data=nullweight=8]
Node2 [data=108weight=4]
Node2 [data=nullweight=4]
Node2 [data=106weight=2]
Node2 [data=111weight=2]
Node2 [data=32weight=9]
Node2 [data=nullweight=23]
Node2 [data=nullweight=10]
Node2 [data=97weight=5]
Node2 [data=105weight=5]
Node2 [data=nullweight=13]
Node2 [data=nullweight=5]
Node2 [data=nullweight=2]
Node2 [data=100weight=1]
Node2 [data=117weight=1]
Node2 [data=nullweight=3]
Node2 [data=121weight=1]
Node2 [data=118weight=2]
Node2 [data=nullweight=8]
Node2 [data=101weight=4]
Node2 [data=107weight=4]
哈夫曼编码
{32=01, 97=100, 100=11000, 117=11001, 101=1110, 118=11011, 105=101, 121=11010, 106=0010, 107=1111, 108=000, 111=0011}
压缩文件
stringBuilder:
1010100010111111110010001011111111001000101111111100100101001101110001110000011011101000111100101000101111111100110001001010011011100
[-88, -65, -56, -65, -56, -65, -55, 77, -57, 6, -24, -14, -117, -4, -60, -90, 28]
*/
byte[] huffmanCodeBytes = huffmanZip(contentBytes);
System.out.println("压缩后的结果:"+Arrays.toString(huffmanCodeBytes));
//解压
System.out.println("解压");
byte[] sourceBytes = decode(huffmanCodes, huffmanCodeBytes);
System.out.println("原来的字符串="+new String(sourceBytes));
//压缩文件
// System.out.println("压缩文件");
// String srcFile = "F:\\Study\\java\\eclipse\\eclipse2020basic\\basic\\1.jpg";
// String dstFile = "F:\\Study\\java\\eclipse\\eclipse2020basic\\basic\\1.zip";
// zipFile(srcFile, dstFile);
// System.out.println("压缩文件OK");
//文件解压
// String zipFile = "F:\\Study\\java\\eclipse\\eclipse2020basic\\basic\\1.zip";
// String dstFile2 = "F:\\Study\\java\\eclipse\\eclipse2020basic\\basic\\11.jpg";
// unZipFile(zipFile, dstFile2);
// System.out.println("解压成功");
}
//把主方法中的封装起来,便于调用
/**
* @param bytes:原始的字符串对应的字节数组
* @return:经过huffman编码处理后的字节数组
*/
public static byte[] huffmanZip(byte[] bytes) {
List<Node2> nodes = getNodes(bytes);
Node2 huffmanTreeRoot = createHuffmanTree(nodes);
Map<Byte,String> huffmanCodes = getCodes(huffmanTreeRoot);
byte[] huffmanCodeBytes = zip(bytes,huffmanCodes);
return huffmanCodeBytes;
}
//完成数据解压
//1.将huffmanCodeBytes[-88, -65, -56, -65, -56, -65, -55, 77, -57, 6, -24, -14, -117, -4, -60, -90, 28]
//转成赫夫曼编码对应的二进制字符串:10001011...
//2.将字符串对照赫夫曼编码重新转成字符串"i like like like java do you like a java"
/**将一个byte转成二进制的字符串
* @param flag:标识是否需要补高位,如果是true需要补,false不需要补,如果是最后一个字节,无需补高位
* @param b:传入的byte
* @return:b对应的二进制字符串(按补码返回)
*/
private static String byteToBitString(boolean flag,byte b) {
//使用变量保存b
int temp = b; //将b转成int,因为Integer有toBinaryString()方法转成字符串
//如果是正数要补高位
if (flag) {
temp |= 256; //按位或256 1 0000 0000 | 0000 0001 => 1 0000 0001
}
String str = Integer.toBinaryString(temp); //返回的是temp对应的二进制的补码
if (flag) {
return str.substring(str.length()-8);
}else {
return str;
}
}
//编写方法,完成对压缩数据的解码
/**
* @param huffmanCodes:哈夫曼编码表
* @param huffmanBytes:哈夫曼编码得到的字节数组
* @return就是原来字符串对应的数组
*/
private static byte[] decode(Map<Byte, String> huffmanCodes,byte[] huffmanBytes) {
//1.先得到huffmanBytes对应的二进制的字符串,形式:10001011...
StringBuilder stringBuilder = new StringBuilder();
//将byte数组转成二进制的字符串
for (int i = 0; i < huffmanBytes.length; i++) {
byte b =huffmanBytes[i];
//判断是不是最后一个字节,最后一个字节不用补高位
boolean flag = (i == huffmanBytes.length - 1);
stringBuilder.append(byteToBitString(!flag, b));
}
System.out.println("哈夫曼字节数组对应的二进制字符串="+stringBuilder.toString());
//2.把字符串按照指定的哈夫曼编码进行解码
//把赫夫曼编码表进行调换,因为反向查询97—100 => 100-97
Map<String, Byte> map = new HashMap<String, Byte>();
for (Map.Entry<Byte, String> entry : huffmanCodes.entrySet()) {
map.put(entry.getValue(),entry.getKey());
}
System.out.println("map="+ map );
//创建集合存放Byte
List<Byte> list = new ArrayList<Byte>();
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);//让i不动,让count移动,直到匹配到一个字符
b = map.get(key);
if (b == null) { //没有匹配到
count++;
}else { //匹配到
flag = false;
}
}
list.add(b);
i += count; //让i直接移动到count
}
//当for循环结束后,list就存放了所有的字符"i like like like java do you like a java"
System.out.println("list="+ list);
//把list中的数据放入到byte[]并返回
byte[] b = new byte[list.size()];
for (int i = 0; i < b.length; i++) {
b[i] =list.get(i);
}
return b;
}
//接收字节数组返回List
private static List<Node2> getNodes(byte[] bytes){
//1.创建一个ArrayList
ArrayList<Node2> nodes = new ArrayList<Node2>();
//2.遍历bytes,统计每一个byte出现的次数=>map[key,value]
Map<Byte, Integer> counts = new HashMap<Byte, Integer>();
for (byte b : bytes) {
Integer count = counts.get(b);
if (count == null) { //Map还没有这个字符数据,第一次
counts.put(b, 1);
}else {
counts.put(b, count+1);
}
}
//3.把每个键值对转成一个Node2对象,并加入nodes集合
//遍历map
for (Map.Entry<Byte, Integer> entry : counts.entrySet()) {
nodes.add(new Node2(entry.getKey(),entry.getValue()));
}
return nodes;
}
//通过List,创建对应的哈夫曼树
private static Node2 createHuffmanTree(List<Node2> nodes) {
while(nodes.size()>1) {
//排序(从小到大)
Collections.sort(nodes);
//取出第一棵最小的二叉树
Node2 leftNode2 = nodes.get(0);
//取出第二棵最小的二叉树
Node2 rightNode2 = nodes.get(1);
//创建一颗新的二叉树,它的根节点没有data,只有权值
Node2 parent = new Node2(null, leftNode2.weight+rightNode2.weight);
parent.left = leftNode2;
parent.right = rightNode2;
//将已经处理的二叉树移除
nodes.remove(leftNode2);
nodes.remove(rightNode2);
//将新的二叉树加入到nodes
nodes.add(parent);
}
//返回的Nodes最后的节点就是哈夫曼树的根节点
return nodes.get(0);
}
//前序遍历
public static void preOrder(Node2 root) {
if (root != null) {
root.preOrder();
}else {
System.out.println("赫夫曼树为空");
}
}
//生成哈夫曼树对应的哈夫曼编码
//1.将赫夫曼编码表存放在Map<Byte,String>,例如32->01,97->100
static Map<Byte, String> huffmanCodes = new HashMap<Byte, String>();
//2,生成哈夫曼编码表时,需要拼接路径定义一个StringBuilder,存储叶子节点的路径
static StringBuilder stringBuilder = new StringBuilder();
/**
* 功能;将传入的Node节点的所有叶子结点的赫夫曼编码得到,并放入到huffmanCodes集合
* @param node传入节点
* @param code路径(左子节点是0,右子节点是1)
* @param stringBuiilder用于拼接路径
*/
private static void getCodes(Node2 node,String code,StringBuilder stringBuilder) {
StringBuilder stringBuilder2 = new StringBuilder(stringBuilder);
//将code加入到stringBuilder2
stringBuilder2.append(code);
if (node != null) { //node为空则不做处理
//判断当前节点是叶子节点还是非叶子节点
if (node.data == null) { //非叶子节点
//递归处理
//向左递归
getCodes(node.left, "0", stringBuilder2);
getCodes(node.right, "1", stringBuilder2);
}else { //说明是叶子节点
//就表示找到某个叶子结点的最后
huffmanCodes.put(node.data,stringBuilder2.toString());
}
}
}
//为了调用方便生成哈夫曼编码,重载getCodes
private static Map<Byte,String> getCodes(Node2 root){
if (root == null) {
return null;
}
//处理左子树
getCodes(root.left,"0",stringBuilder);
//处理右子树
getCodes(root.right, "1", stringBuilder);
return huffmanCodes;
}
//将字符串对应的byte[]数组,通过生成的哈夫曼编码表,返回一个赫夫曼编码压缩后的byte[]
/**
* @param bytes原始的字符串对应的数组
* @param huffmanCodes生成的赫夫曼编码Map
* @return返回赫夫曼编码处理后的byte[]数组
* 返回的byte[] huffmanCodeBytes,即8位对应一个byte,放入到huffmanCodeBytes
* huffmanCodeBytes[0] = 10101000(补码) => byte[反码:10101000-1=10100111,原码:11011000=-88]
*/
private static byte[] zip(byte[] bytes,Map<Byte, String> huffmanCodes) {
//1.利用huffmanCodes将bytes转成赫夫曼编码对应的字符串
StringBuilder stringBuilder = new StringBuilder();
//遍历bytes数组
for (byte b : bytes) {
stringBuilder.append(huffmanCodes.get(b));
}
System.out.println("stringBuilder:");
System.out.println(stringBuilder.toString());//10101000...共133个字符
//将上面转完的133个字符转化为byte[]数组
//统计返回byte[] huffmanCodeBytes长度
int len;
if (stringBuilder.length()%8 == 0) {
len = stringBuilder.length() /8;
}else {
len = stringBuilder.length() /8+1;
}
//创建存储压缩后的byte[]数组
byte[] huffmanCodeBytes = new byte[len];
int index = 0; //记录第几个byte
for (int i = 0; i < stringBuilder.length(); i+=8) { //因为每8位对应一个byte,所以步长加8
String strByte;
if (i+8 > stringBuilder.length() ) { //不够8位
strByte = stringBuilder.substring(i); //从i取到最后
}else {
strByte = stringBuilder.substring(i, i+8);
}
//将strByte转成一个byte,放入到huffmanCodeBytes
huffmanCodeBytes[index] = (byte)Integer.parseInt(strByte, 2);
index++;
}
return huffmanCodeBytes;
}
//编写方法对文件进行压缩
/**
* @param srcFile 传入的希望压缩文件全路径
* @param dstFile 压缩后将压缩文件放到哪个目录
* @throws IOException
*/
public static void zipFile(String srcFile,String dstFile) throws IOException {
//创建输出流
OutputStream os = null;
ObjectOutputStream oos = null;
//创建文件输入流
FileInputStream fis = null;
try {
//创建文件输入流,读取文件
fis = new FileInputStream(srcFile);
//创建一个和原文件一样大的byte[]
byte[] b = new byte[fis.available()];
//读取文件
fis.read(b);
//获取文件对应的哈夫曼编码表,直接对原文件进行压缩
byte[] huffmanBytes = huffmanZip(b);
//创建文件的输出流存放压缩文件
os = new FileOutputStream(dstFile);
//创建一个和文件输出流关联的ObjectOutputStream
oos = new ObjectOutputStream(os);
//把赫夫曼编码后的字节数组写入压缩文件
oos.writeObject(huffmanBytes); //可以直接写huffmanBytes对象
//以对象流的方式写入赫夫曼编码,是为了以后恢复原文件使用
//一定要把赫夫曼文件写入压缩文件
oos.writeObject(huffmanCodes);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}finally {
try {
fis.close();
os.close();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
//对文件解压
/**
* @param zipFile 准备解压的文件
* @param dstFile 将文件解压的路径
*/
public static void unZipFile(String zipFile,String dstFile) {
//定义文件输入流
InputStream is = null;
//定义一个对象输入流
ObjectInputStream ois = null;
//定义文件的输出流
OutputStream os = null;
try {
//创建文件输入流
is = new FileInputStream(zipFile);
//创建一个和is关联的对象输入流
ois = new ObjectInputStream(is);
//读取byte数组 huffmanBytes
byte[] huffmanBytes = (byte[])ois.readObject();
//读取赫夫曼编码表
Map<Byte,String> huffmanCodes = (Map<Byte,String>)ois.readObject();
//解码
byte[] bytes = decode(huffmanCodes, huffmanBytes);
//将bytes数组写入到目标文件
os = new FileOutputStream(dstFile);
//写数据到dstFile文件中去
os.write(bytes);
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
try {
os.close();
ois.close();
is.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
//创建Node,带数据和权值
class Node2 implements Comparable<Node2>{
Byte data; //存放数据本身 a就是97,空格就是32
int weight; //权值,字符串中字符出现的次数
Node2 left;
Node2 right;
public Node2(Byte data, int weight) {
super();
this.data = data;
this.weight = weight;
}
@Override
public int compareTo(Node2 o) {
// TODO Auto-generated method stub
return this.weight - o.weight; //从小到大排
}
@Override
public String toString() {
return "Node2 [data=" + data + "weight="+weight+"]";
}
//前序遍历
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
}
//输出
stringBuilder:
1010100010111111110010001011111111001000101111111100100101001101110001110000011011101000111100101000101111111100110001001010011011100
压缩后的结果:[-88, -65, -56, -65, -56, -65, -55, 77, -57, 6, -24, -14, -117, -4, -60, -90, 28]
解压
哈夫曼字节数组对应的二进制字符串=1010100010111111110010001011111111001000101111111100100101001101110001110000011011101000111100101000101111111100110001001010011011100
map={000=108, 01=32, 100=97, 101=105, 11010=121, 0011=111, 1111=107, 11001=117, 1110=101, 11000=100, 11011=118, 0010=106}
list=[105, 32, 108, 105, 107, 101, 32, 108, 105, 107, 101, 32, 108, 105, 107, 101, 32, 106, 97, 118, 97, 32, 100, 111, 32, 121, 111, 117, 32, 108, 105, 107, 101, 32, 97, 32, 106, 97, 118, 97]
原来的字符串=i like like like java do you like a java