Skip to content

Commit

Permalink
哈夫曼压缩&大球吃小球
Browse files Browse the repository at this point in the history
  • Loading branch information
leo6033 authored Jul 20, 2019
1 parent 31e86c4 commit 63d0fc1
Show file tree
Hide file tree
Showing 77 changed files with 731 additions and 0 deletions.
Binary file added 哈夫曼压缩/bin/test/test.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/test1/Compare.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/test1/HufTree.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/test1/HuffmanCompress.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/test1/Test.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/test1/TmpNode.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼/Compare.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼/HufTree.class
Binary file not shown.
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼/TmpNode.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼/test.class
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼压缩/Code.class
Binary file not shown.
Binary file not shown.
Binary file added 哈夫曼压缩/bin/哈夫曼压缩/hfmNode.class
Binary file not shown.
16 changes: 16 additions & 0 deletions 哈夫曼压缩/src/哈夫曼/Compare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ¹þ·òÂü;

import java.util.Comparator;

public class Compare implements Comparator<HufTree>{

@Override
public int compare(HufTree o1, HufTree o2) {
if(o1.weight < o2.weight)
return -1;
else if(o1.weight > o2.weight)
return 1;
return 0;
}

}
28 changes: 28 additions & 0 deletions 哈夫曼压缩/src/哈夫曼/HufTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package 哈夫曼;

public class HufTree{
public byte Byte; //以8位为单元的字节
public int weight;//该字节在文件中出现的次数
public String code; //对应的哈夫曼编码
public HufTree lchild,rchild;

/**调试的时候添加的**/
public String toString(){
return "Byte:" + Byte + ",weight:" + weight + ",code:" + code + ",+ lchild:" + lchild + ",rchild:" + rchild;
}
}

//统计字符频度的临时节点
class TmpNode implements Comparable<TmpNode>{
public byte Byte;
public int weight;

@Override
public int compareTo(TmpNode arg0) {
if(this.weight < arg0.weight)
return 1;
else if(this.weight > arg0.weight)
return -1;
return 0;
}
}
223 changes: 223 additions & 0 deletions 哈夫曼压缩/src/哈夫曼/HuffmanCompress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package 哈夫曼;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;

public class HuffmanCompress {
private PriorityQueue<HufTree> queue = null;

public void compress(File inputFile, File outputFile) {
Compare cmp = new Compare();
queue = new PriorityQueue<HufTree>(12, cmp);

// 映射字节及其对应的哈夫曼编码
HashMap<Byte, String> map = new HashMap<Byte, String>();

int i, char_kinds = 0;
int char_tmp, file_len = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
DataOutputStream oos = null;

HufTree root = new HufTree();
String code_buf = null;

// 临时储存字符频度的数组
TmpNode[] tmp_nodes = new TmpNode[256];

for (i = 0; i < 256; i++) {
tmp_nodes[i] = new TmpNode();
tmp_nodes[i].weight = 0;
tmp_nodes[i].Byte = (byte) i;
}

try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
oos = new DataOutputStream(fos);

/*
* 统计字符频度,计算文件长度
*/
while ((char_tmp = fis.read()) != -1) {
tmp_nodes[char_tmp].weight++;
file_len++;
}
fis.close();
// 排序,将频度为0的字节放在最后,同时计算除字节的种类,即有多少个不同的字节
Arrays.sort(tmp_nodes);
for (i = 0; i < 256; i++) {
if (tmp_nodes[i].weight == 0) {
break;
}
HufTree tmp = new HufTree();
tmp.Byte = tmp_nodes[i].Byte;
tmp.weight = tmp_nodes[i].weight;
queue.add(tmp);
}
char_kinds = i;

if (char_kinds == 1) {
oos.writeInt(char_kinds);
oos.writeByte(tmp_nodes[0].Byte);
oos.writeInt(tmp_nodes[0].weight);
} else {
// 建树
createTree(queue);
root = queue.peek();
// 生成哈夫曼编码
hufCode(root, "", map);
// 写入字节种类
oos.writeInt(char_kinds);
for (i = 0; i < char_kinds; i++) {
oos.writeByte(tmp_nodes[i].Byte);
oos.writeInt(tmp_nodes[i].weight);
}
oos.writeInt(file_len);
fis = new FileInputStream(inputFile);
code_buf = "";
while ((char_tmp = fis.read()) != -1) {
code_buf += map.get((byte) char_tmp);
while (code_buf.length() >= 8) {
char_tmp = 0;
for (i = 0; i < 8; i++) {
char_tmp <<= 1;
if (code_buf.charAt(i) == '1')
char_tmp |= 1;
}
oos.writeByte((byte) char_tmp);
code_buf = code_buf.substring(8);
}
}
// 最后编码长度不够8位的时候,用0补齐
if (code_buf.length() > 0) {
char_tmp = 0;
for (i = 0; i < code_buf.length(); ++i) {
char_tmp <<= 1;
if (code_buf.charAt(i) == '1')
char_tmp |= 1;
}
char_tmp <<= (8 - code_buf.length());
oos.writeByte((byte) char_tmp);
}
oos.close();
fis.close();
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void extract(File inputFile, File outputFile) {
Compare cmp = new Compare();
queue = new PriorityQueue<HufTree>(12, cmp);

int i;
int file_len = 0;
int writen_len = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
DataInputStream ois = null;

int char_kinds = 0;
HufTree root=new HufTree();
byte code_tmp;
try {
fis = new FileInputStream(inputFile);
ois = new DataInputStream(fis);
fos = new FileOutputStream(outputFile);

char_kinds = ois.readInt();
// 字节只有一种
if (char_kinds == 1) {
code_tmp = ois.readByte();
file_len = ois.readInt();
while ((file_len--) != 0) {
fos.write(code_tmp);
}
} else {
for (i = 0; i < char_kinds; i++) {
HufTree tmp = new HufTree();
tmp.Byte = ois.readByte();
tmp.weight = ois.readInt();
System.out.println("Byte: "+tmp.Byte+" weight: "+tmp.weight);
queue.add(tmp);
}

createTree(queue);

file_len = ois.readInt();
root = queue.peek();
while (true) {
code_tmp = ois.readByte();
for (i = 0; i < 8; i++) {
if ((code_tmp&128)==128) {
root = root.rchild;
} else {
root = root.lchild;
}
if (root.lchild == null && root.rchild == null) {
fos.write(root.Byte);
++writen_len;
if (writen_len == file_len)
break;
root = queue.peek();
}
code_tmp <<= 1;
}
if (writen_len == file_len)
break;
}
}
fis.close();
fos.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void createTree(PriorityQueue<HufTree> queue) {
while (queue.size() > 1) {
HufTree min1 = queue.poll();
HufTree min2 = queue.poll();
System.out.print(min1.weight + " " + min2.weight + " ");

HufTree NodeParent = new HufTree();
NodeParent.weight = min1.weight + min2.weight;
NodeParent.lchild = min1;
NodeParent.rchild = min2;

queue.add(NodeParent);
}
}

public void hufCode(HufTree root, String s, HashMap<Byte, String> map) {
if (root.lchild == null && root.rchild == null) {
root.code = s;
System.out.println("节点" + root.Byte + "编码" + s);
map.put(root.Byte, root.code);

return;
}
if (root.lchild != null) {
hufCode(root.lchild, s + '0', map);
}
if (root.rchild != null) {
hufCode(root.rchild, s + '1', map);
}

}

}
18 changes: 18 additions & 0 deletions 哈夫曼压缩/src/哈夫曼/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 哈夫曼;

import java.io.File;

public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
HuffmanCompress sample = new HuffmanCompress();
// File inputFile = new File("C:\\Users\\long452a\\Desktop\\opencv链接文档.txt");
// File outputFile = new File("C:\\Users\\long452a\\Desktop\\opencv链接文档.rar");
// sample.compress(inputFile, outputFile);
File inputFile = new File("C:\\Users\\long452a\\Desktop\\opencv链接文档.rar");
File outputFile = new File("C:\\Users\\long452a\\Desktop\\opencv链接文档1.txt");
sample.extract(inputFile, outputFile);
}

}
Binary file added 大球吃小球/bin/Lottery/Lottery.class
Binary file not shown.
Binary file added 大球吃小球/bin/Lottery/LotteryListener.class
Binary file not shown.
Binary file added 大球吃小球/bin/Lottery0/Lottery.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/Lottery1/Lottery.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/Lottery1/LotteryThread.class
Binary file not shown.
Binary file added 大球吃小球/bin/Test1V1/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/Test1V1/TestUI.class
Binary file not shown.
Binary file added 大球吃小球/bin/Test1V1/a1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 大球吃小球/bin/com/draw1/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw1/DrawLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw2/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw2/DrawLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw3/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw3/DrawLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/draw3/DrawThread.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV1/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV1/DrawLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV1/DrawThread.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/ChangeThread.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/Draw.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/DrawLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/DrawThread.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/IShape.class
Binary file not shown.
Binary file added 大球吃小球/bin/com/drawV2/ShapeBall.class
Binary file not shown.
Binary file added 大球吃小球/bin/particv06/MainUI$1.class
Binary file not shown.
Binary file added 大球吃小球/bin/particv06/MainUI.class
Binary file not shown.
Binary file added 大球吃小球/bin/particv06/Particle.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/particv06/Vec2f.class
Binary file not shown.
Binary file added 大球吃小球/bin/test/manager.class
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球/Ball.class
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球/BallLis.class
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球/BallUI.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球1/Ball.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球1/BallUI.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球2/Ball.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球2/BallUI.class
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球3/Ball.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球3/BallUI.class
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球3/Bool.class
Binary file not shown.
Binary file added 大球吃小球/bin/大球吃小球3/Myball.class
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 63d0fc1

Please sign in to comment.