JAVA基础:用哈弗曼编码实现压缩软件(3)
最后就是把文件中的所有字节按照这种哈弗曼的编码方式保存到文件中,过程类似于上一步(在最后打入末尾补入的0的个数,主要是为了方便解压缩):Java代码//编码表输出完毕,将文件中的字节按照这种编码方式压缩InputStream ins = new FileInputStream(pathName_former);
InputStream buf = new BufferedInputStream(ins);//创建输入流
count = 0;
writeCode = "";
allCode = "";
while(buf.available()>0||count>=8){
if(count>=8){//满8
writeCode = allCode.substring(0,8);//前8位
count-=8;
allCode = allCode.substring(8);
bos.write(changeString(writeCode));//写入一个字节
}else{
int data = buf.read();
count+=Code.length();
allCode+=Code;
}
}
//如果不满8位的 来自www.Examw.com
if(allCode.length()>0){
int len = 8-allCode.length();//补零
for(int j=0;j
allCode+="0";
}
bos.write(changeString(allCode));
bos.write(len);//补入了几个0
}else{
bos.write(0);//补入了0个0
}
到这里,整个压缩过程基本完成了,解压缩就是压缩的逆过程,在此不再赘述,所有的操作都保存在附上源代码中。
页:
[1]