📄 decompress.java
字号:
package Huffman;
import java.io.*;
import java.util.*;
import javax.imageio.stream.*;
public class Decompress{
private byte [] output;
private byte []table;//symbol table
private int length,bytelength;//length of table
private int [] count;
private File inname,outname;
public Decompress(File in,File out){
inname=in;
outname=out;
}
private void readFile(){
try{
FileImageInputStream infile=new FileImageInputStream(inname);
length=infile.readInt();
//System.out.print(length+" ");
table=new byte [length+1];
count=new int [length+1];
infile.read(table,1,length);//read table[]
for(int i=1;i<=length;i++) //read count[]
{
count[i]=infile.readInt();
//System.out.print(table[i]+" "+count[i]+" ");
}
bytelength=infile.readInt();
//System.out.print(bytelength);
output=new byte[bytelength+1];
int n=length;//create hufftree
int m=2*n-1;
int i;
//System.out.print(n+" "+m+" ");
HTNode [] HT=new HTNode[m+1];
for(i=1;i<=n;i++) HT[i]=new HTNode(count[i],0,0,0);
for(;i<=m;i++) HT[i]=new HTNode(0,0,0,0);
for(i=n+1;i<=m;i++) {
int s1=0,s2=0;
int w1=10000,w2=10000;
for(int j=1;j<=i-1;j++)
{
if(HT[j].parent==0)
{
if(HT[j].weight<w1)
{
w2=w1;
w1=HT[j].weight;
s2=s1;
s1=j;
//System.out.print(j+" "+s1+","+w1+" ");
}
else if(HT[j].weight<w2)
{
s2=j;
w2=HT[j].weight;
//System.out.print("kk ");
}
}
}
//System.out.print(s1+","+s2+" ");
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}//create HuffTree
int r;
for(i=1;i<=bytelength;i++)
{
r=m;
do
{
int b=infile.readBit();
if(b==0)
r=HT[r].lchild;
else
r=HT[r].rchild;
}
while(HT[r].lchild!=0);
output[i]=table[r];
}
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
}
private void write(){
try{
FileImageOutputStream outfile=new FileImageOutputStream(outname);
outfile.write(output,1,bytelength);
}
catch(IOException e){
}
}
public void process(){
readFile();
write();
}
//public static void main(String [] args){
// Decompress c=new Decompress(new File("h.dat"),new File("sam2.txt"));
// c.process();
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -