資源簡(jiǎn)介
java實(shí)現(xiàn)霍夫曼(huffman)樹(shù)的壓縮和解壓縮,支持對(duì)文檔的壓縮和解壓縮
代碼片段和文件信息
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.InputStream;
import?java.io.OutputStream;
import?java.io.OutputStreamWriter;
import?java.text.DecimalFormat;
import?java.io.*;
public?class?huffman?
{
// int?splitlable=0;//劃分邊界的個(gè)數(shù)
public?huffman()
{
???//默認(rèn)符號(hào)順序是????0-9?.?#?其中#用來(lái)作為浮點(diǎn)數(shù)之間的間隔
int[]?count=new?int[12];?//統(tǒng)計(jì)個(gè)數(shù)
int[]?count2=new?int[12];//通過(guò)讀取文件來(lái)統(tǒng)計(jì)個(gè)數(shù),是另一種統(tǒng)計(jì)方法
double[]?percent=new?double[12];//百分比
double[]?sortpercent=new?double[12];//排序后的百分比從大到小
double[]?treesortpercent=new?double[12];//排序后的百分比?,從小到大,構(gòu)建霍夫曼樹(shù)需要的排序方式
int[]?sortlable=new?int[12];//sortpercent數(shù)組對(duì)應(yīng)percent數(shù)組的下標(biāo)
double[]?sortpercentapp=new?double[12+12-1];//構(gòu)建霍夫曼樹(shù)時(shí),需要多增加長(zhǎng)度-1個(gè)中間節(jié)點(diǎn)
String[]?treecode=new?String[12+12-1];//包括在構(gòu)建霍夫曼樹(shù)的過(guò)程中有中間節(jié)點(diǎn)的編碼
String[]?code=new?String[12];//各個(gè)數(shù)字和?.?#的編碼,已經(jīng)排序
//int?[]?split=new?int[12];//分組的情況,有效的個(gè)數(shù)等于splitlable+1
byte[]?simbal=new?byte[12];//各個(gè)符號(hào)的位置,已經(jīng)排序
char[]?chsimbal=new?char[12];//各個(gè)符號(hào)的位置,已經(jīng)排序;
//String[]?code=new?String[12+12-1];//編碼
for(int?i=0;i<12;i++)
{
count[i]=0;
count2[i]=0;
percent[i]=0.0;
sortpercent[i]=0.0;
sortlable[i]=0;
?//???split[i]=0;
???//?code[i]=““;
}
for(int?i=0;i treecode[i]=““;
make(count);//產(chǎn)生隨機(jī)數(shù),并將加密結(jié)果寫(xiě)入文件
readfile(count2);//讀取加密數(shù)據(jù)的文件
getpercent(count2percentsortpercent);//獲得百分比
????sort(sortpercentsortlable);//百分比排序,從大到小
????//百分比從小到大排序
????for(int?i=0;i ???? treesortpercent[i]=sortpercent[sortpercent.length-i-1];
????
????for(int?i=0;i ???? sortpercentapp[i]=treesortpercent[i];
????maketree(sortpercentapptreecode);//構(gòu)建霍夫曼樹(shù)并編碼
????//code是從大到下排序的,treecode是從小到大排序的,所以要轉(zhuǎn)換過(guò)來(lái)
?????for(int?i=0;i ???? ?code[i]=treecode[code.length-1-i];
?????sortsimbal(simbalchsimbalsortlable);//符號(hào)對(duì)應(yīng)的排序
??/*
?????for(int?i=0;i ??System.out.println(“編碼“+chsimbal[i]+“:“+code[i]);??
????*/?
?????ysandwrite(countsimbalcode);//壓縮并寫(xiě)入文件
?????jieyasuo();
}
//*********************************************************************************************************
//解壓縮并寫(xiě)入文件
public?void?jieyasuo()//(String?code[]char?chsimbal[])
{
try
{
????String[]?code=new?String[12];//編碼的值
????char[]?chsimbal=new?char[12];//符號(hào)的類型
InputStream??isr?=?new?FileInputStream(“D:/yasuo.doc“);??
OutputStreamWriter?osw??=?new?OutputStreamWriter(new?FileOutputStream(“D:/stringyasuo2.doc“));??
OutputStreamWriter?osw2??=?new?OutputStreamWriter(new?FileOutputStream(“D:/jieya.doc“));??
String?st=““; //將int轉(zhuǎn)化為String,寫(xiě)入文件中stringyasuo2
String?st2=““;//解壓縮后寫(xiě)入文件中jieya.doc
String?tmpinttobin=““;//int轉(zhuǎn)化為2進(jìn)制的形式最多只有32位;
int?intobinlen=0;//int轉(zhuǎn)化為2進(jìn)制的形式時(shí)字符串的長(zhǎng)度,需要考慮正整數(shù)前面自動(dòng)缺0的情況
String?inttobin=““;//由上一個(gè)String剩下的和tmpinttobin的連接
????int?codelen=code.length;//符號(hào)的種類;
// char?ch;//inttobin的字符
// String?code=““;//解壓縮后的編碼
????byte[]?read=new?byte[4];?
????int?len=0;//一次讀取的
評(píng)論
共有 條評(píng)論