文件压缩与解压

来源:互联网 发布:mac安装什么应用 编辑:程序博客网 时间:2024/05/16 08:04

文件的压缩:

1.读取文件的内容

2.统计每个字符出现的次数

int read;while ((read = bis.read()) != -1)//直至读到文件结束{// 第一个读到a,read为97 allChar[read]++;//当read为97时allChar自加//System.out.println(allCode[read]);}// Arrays为操作数组的工具类(Collections为操作集合工具类)//System.out.println(Arrays.toString(allChar));bis.close();


3.构建哈弗曼树,生成哈夫曼编码

if(node.getLeftNode()==null&&node.getRightNode()==null){//如果是叶子节点int data=node.getData();allCode[data]=dict;//dict为字符串初始值为0}if(node.getLeftNode()!=null){createHfmDict(node.getLeftNode(),dict+"0");}if(node.getRightNode()!=null){createHfmDict(node.getRightNode(),dict+"1");}


4.向文件中写入每个哈夫曼编码的长度

for(int i=0;i<allCode.length;i++){int l=allCode[i].length();//哈夫曼编码的长度bos.write(l);//System.out.print("...."+l);}


5.将哈夫曼编码生成二进制代码加入压缩文件中{fileData为StringBuffer型,长度可变,可以增加数据和删除数据}

String strb = "";//哈夫曼编码的初始值for (int i = 0; i < allCode.length; i++) {strb = strb+allCode[i];}fileData.append(strb);//往压缩文件中加入哈夫曼编码的二进制

6.读取文件里的内容,将其转化为二进制代码加入压缩文件中

int read;
                StringBuffer sb =new StringBuffer();while((read=bis.read())!=-1){//读取文件中的内容String hfmCode=allCode[read];sb.append(hfmCode);//fileData.append(sb);}fileData.append(sb);


7.如果压缩文件中二进制位数不是8的倍数,在其后面补零,直至够8的倍数,在在其后面加入8位补零的个数。

int count=0;while(fileData.length()%8!=0){fileData.append("0");count++;}//将count转化为二进制String countBin=Integer.toBinaryString(count);//对countBin进行补零操作while(countBin.length()<8){countBin="0"+countBin;}fileData.append(countBin);


8.将压缩文件中的二进制代码写入压缩文件中

while(fileData.length()!=0){//截取前八个String subString=fileData.substring(0, 8);//转化为10进制int parseInt=Integer.parseInt(subString, 2);bos.write(parseInt);fileData.delete(0, 8);}


文件的解压:

1读取压缩文件中的二进制串

int read;int i = 0,j=0;int[] a=new int[(int) (file.length()-256)];while((read=bis.read())!=-1){i++;if(i<257){continue;}else{a[j]=read;j++;}}


2从文件的最后读取文件,获得补零的位数,移除补的零

for( i=0;i<a.length;i++){String data=Integer.toBinaryString(a[i]);//对转化的数进行补零,使其成为8位while((data.length()%8)!=0){data="0"+data;}//加入strstr.append(data);


3.截取哈夫曼编码,根据图中存储的一一对应关系翻译压缩文件,生成解压文件

for(int i=0;i<allCode.length;i++){if(allCode[i]==0){continue;}else{String subString=str.substring(0,allCode[i]);str.delete(0, allCode[i]);keyCode.put(subString, i);//比较字典}}


4.将解压后的文件写入解压文件中

//进行翻译int i=1;while(str.length()!=0){//根据字典生成解压文件String key=str.substring(0, i);if(keyCode.containsKey(key)){bos.write(keyCode.get(key));//向文件中写入内容str.delete(0, i);i=1;}else{i++;}}bos.flush();bos.close();



0 0
原创粉丝点击