📄 decompressfile.java
字号:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
//解压缩类
public class DecompressFile {
private int fileLength;//中文文件byte长度
//读出头文件的频率
private int[] inputFrequencies(DataInputStream fin) throws IOException{
int []freq=new int[256];
for(int j=0; j<256; j++){
freq[j]=fin.readInt();
fileLength+=freq[j];}
System.out.print(fileLength);
return freq;
}
//根据压缩编码寻找原文件对应字符并输出
private void outputChar(DataInputStream fin,DataOutputStream fout,HuffmanNode root)throws IOException{
HuffmanNode hn=root;
int intreader,m,i;
try{
while(fileLength>0){//如果超过文件长度就不做循环
intreader=fin.readInt();
i=31;
while(i>=0){
if(hn.left==null&&hn.right==null){//对叶子节点写入对应字符
fout.writeByte(hn.sign+256);
fileLength--;//文件长度减一
hn=root;//节点指向头节点
}
else{
m=(intreader>>i)&1;//每次取出一个bit
i--;//决定是向节点的左或者右寻找叶子
if(m==1) hn=hn.left;
else hn=hn.right;
}
}
}
}catch(IOException e){
}
fout.close();//关闭文件流
fin.close();
JOptionPane.showMessageDialog(null, "解压缩成功", "解压提示",JOptionPane.INFORMATION_MESSAGE);
}
//处理数据
public void decompress(String output,String input){
try{
DataInputStream fin=new DataInputStream(new BufferedInputStream(new FileInputStream(input)));
DataOutputStream fout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(output)));
HuffmanCoding hc=new HuffmanCoding();
HuffmanNode hn=hc.createHuffmanTree(inputFrequencies(fin));
hc.createCode(hn, 0, 0);
outputChar(fin,fout,hn);
}catch(IOException i){
JOptionPane.showMessageDialog(null, "解压缩路径错误", "错误提示",JOptionPane.ERROR_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -