`

Java-文件压缩解压工具--(仅供自己收藏)

    博客分类:
  • java
阅读更多

以前为了了解哈夫曼树,而做的一个文件压缩的东东,,实现给文件压缩,与解压

话不多多说先上图,当然界面不是非常的亮,有关界面的问题,上面两篇博客已经写得很清楚了!



关键压缩技术是哈弗曼树的压缩,所谓哈弗曼树详细猛戳此网址
http://baike.baidu.com/view/127820.htm

我定义的文件格式如下

编码数 出现的字节 字节编码的长度 字节的编码 码表

代码

这个是哈弗曼节点类

一个节点有数据,权值(也就是出现的频率),左子树,右子树

Java代码 复制代码
  1. package hfyasuo;
  2. publicclass HfmNode {
  3. publicint Data;
  4. publicint Times;
  5. public HfmNode Lc;
  6. public HfmNode Rc;
  7. public HfmNode(int Data,int Times){
  8. this.Data=Data;
  9. this.Times=Times;
  10. }
  11. }
Java代码  收藏代码
  1. package hfyasuo;    
  2.     
  3. public class HfmNode {    
  4. public int Data;    
  5. public int Times;    
  6. public HfmNode Lc;    
  7. public HfmNode Rc;    
  8. public HfmNode(int Data,int Times){    
  9.     this.Data=Data;    
  10.     this.Times=Times;    
  11. }    
  12. }  

 

首先你得得到你要压缩文件的里面每个字节出现的个数

Java代码 复制代码
  1. //统计文件各字节数字数
  2. publicint[] getByteCounts() throws IOException{
  3. int[] bc=newint[256];
  4. FileInputStream in=new FileInputStream(file);
  5. while(in.available()>0){
  6. bc[in.read()]++;
  7. }
  8. in.close();
  9. return bc;
  10. }
Java代码  收藏代码
  1. //统计文件各字节数字数    
  2. public int[] getByteCounts() throws IOException{    
  3.     int[] bc=new int[256];    
  4.     FileInputStream in=new FileInputStream(file);    
  5.     while(in.available()>0){    
  6.         bc[in.read()]++;    
  7.     }    
  8.     in.close();    
  9.     return bc;    
  10. }    

用一个数组存储起来,如果存在某个字节那么就次数+1

之后就可以根据各字节出现的数字开始构造哈夫曼树

Java代码 复制代码
  1. publicint HfmCounts=0;
  2. //构建哈弗曼树,返回树的根节点
  3. public HfmNode CreatHfmNode() throws IOException{
  4. //构建队列,将节点加入队列
  5. PriorityQueue<HfmNode> hfmshu=new PriorityQueue<HfmNode>(1,new Mycomparator());
  6. int[] bc=getByteCounts();
  7. for(int i=0;i<256;i++){
  8. if(bc[i]!=0){
  9. HfmNode h=new HfmNode(i,bc[i]);
  10. hfmshu.add(h);
  11. HfmCounts++;
  12. }
  13. }
  14. //System.out.println("产生了"+HfmCounts+"个节点");
  15. //构建哈弗曼树
  16. while(hfmshu.size()>1){
  17. HfmNode h1=hfmshu.poll();
  18. HfmNode h2=hfmshu.poll();
  19. HfmNode hh=new HfmNode(h1.Data+h2.Data,h1.Times+h2.Times);
  20. hh.Lc=h1;
  21. hh.Rc=h2;
  22. hfmshu.add(hh);
  23. }
  24. //获取根节点
  25. HfmNode Root=hfmshu.peek();
  26. return Root;
  27. }
Java代码  收藏代码
  1. public int HfmCounts=0;    
  2. //构建哈弗曼树,返回树的根节点    
  3. public HfmNode CreatHfmNode() throws IOException{    
  4.     //构建队列,将节点加入队列    
  5.     PriorityQueue<HfmNode> hfmshu=new PriorityQueue<HfmNode>(1,new  Mycomparator());    
  6.     int[] bc=getByteCounts();    
  7.     for(int i=0;i<256;i++){    
  8.         if(bc[i]!=0){    
  9.             HfmNode h=new HfmNode(i,bc[i]);    
  10.             hfmshu.add(h);    
  11.             HfmCounts++;    
  12.         }    
  13.     }    
  14.     //System.out.println("产生了"+HfmCounts+"个节点");    
  15.     //构建哈弗曼树    
  16.     while(hfmshu.size()>1){    
  17.     HfmNode h1=hfmshu.poll();    
  18.     HfmNode h2=hfmshu.poll();    
  19.     HfmNode hh=new HfmNode(h1.Data+h2.Data,h1.Times+h2.Times);    
  20.     hh.Lc=h1;    
  21.     hh.Rc=h2;    
  22.     hfmshu.add(hh);    
  23.     }    
  24.     //获取根节点    
  25.     HfmNode Root=hfmshu.peek();    
  26.     return Root;    
  27. }    

利用优先队列PriorityQueue<HfmNode> hfmshu=new PriorityQueue<HfmNode>(1,new Mycomparator());

存储节点,然后在根据哈夫曼数的特点来构造哈夫曼树

其中优先队列重写他的比较方法,这里应该是比较节点的次数

Java代码 复制代码
  1. package hfyasuo;
  2. import java.util.Comparator;
  3. publicclass Mycomparator implements Comparator<HfmNode>{
  4. @Override
  5. publicint compare(HfmNode n1, HfmNode n2) {
  6. // TODO Auto-generated method stub
  7. if(n1.Times<n2.Times){
  8. return -1;
  9. }elseif(n1.Times>n2.Times){
  10. return1;
  11. }else{
  12. return0;}
  13. }
  14. }
Java代码  收藏代码
  1. package hfyasuo;    
  2.     
  3. import java.util.Comparator;    
  4.     
  5.     
  6.     
  7.     
  8. public class Mycomparator implements Comparator<HfmNode>{    
  9.     
  10.     @Override    
  11.     public int compare(HfmNode n1, HfmNode n2) {    
  12.         // TODO Auto-generated method stub    
  13.         if(n1.Times<n2.Times){    
  14.             return -1;    
  15.         }else if(n1.Times>n2.Times){    
  16.             return 1;    
  17.         }else{    
  18.         return 0;}    
  19.     }    
  20.     
  21.         
  22.     
  23. }    

树构建好后,现在开始获取编码

Java代码 复制代码
  1. //构造,获取各字节的编码
  2. publicvoid getCode(HfmNode Root,String s){
  3. if(Root.Lc!=null){
  4. getCode(Root.Lc,s+'0');
  5. }
  6. if(Root.Rc!=null){
  7. getCode(Root.Rc,s+'1');
  8. }
  9. if(Root.Lc==null&&Root.Rc==null){
  10. Code[Root.Data]=s;
  11. //System.out.println("得到了一个编码"+Root.data+' '+s);
  12. }
  13. }
Java代码  收藏代码
  1. //构造,获取各字节的编码    
  2. public void getCode(HfmNode Root,String s){    
  3.     if(Root.Lc!=null){    
  4.           getCode(Root.Lc,s+'0');    
  5.     }    
  6.     if(Root.Rc!=null){    
  7.          getCode(Root.Rc,s+'1');    
  8.     }    
  9.     if(Root.Lc==null&&Root.Rc==null){    
  10.          Code[Root.Data]=s;    
  11.          //System.out.println("得到了一个编码"+Root.data+' '+s);    
  12.     }       
  13.     }    

 

接下来,就是要按照自己的定义的格式将编码写入到文件当中,写入的信息必须要保证能将其按照规则解压

那么要实现到压缩,就必得动些手脚,,写入最少是一个字节一个字节的写入(一个字节是八位),就是byte为最小单位,现在我们是要写入编码,比如说得到的三个编码是010 110 01100一般写入是要是三个字节,就是写入00000010

00000110 0001100这三个字节,但是如果我们将这三个字节作为一个字节存入,就是写入01011001100,这岂不是就达到了压缩嘛,也就是将不足八位的编码凑成八位写入

Java代码 复制代码
  1. //写入文件的总数,写入节点数,写入出现的字节,每个字节的编码长度,写入码表,写入文件的编码
  2. publicvoid writeBytesCodes() throws IOException{
  3. FileInputStream in=new FileInputStream(file);//获取文件的输入流
  4. FileOutputStream ou=new FileOutputStream(Pressedfile);
  5. DataOutputStream Daou=new DataOutputStream(ou);
  6. Daou.writeInt(in.available());//写入文件的总数
  7. ou.write(HfmCounts);//写入节点总数
  8. //System.out.println("写入的字节总数是"+HfmCounts);
  9. //写入出现过的字节
  10. int[] bc=getByteCounts();
  11. for(int i=0;i<256;i++){
  12. if(bc[i]!=0){
  13. ou.write(i);
  14. // System.out.println("写入的码表字节是"+i);
  15. }
  16. }
  17. //写入每个编码的长度
  18. for(int i=0;i<256;i++){
  19. if(Code[i]!=null){
  20. ou.write(Code[i].length());
  21. //System.out.println("写入的编码长度是"+Code[i].length());
  22. }
  23. }
  24. //写入码表
  25. int counts=0;
  26. int i=0;
  27. String s="";
  28. String writes;
  29. //首先对码表进行处理
  30. for(int l=0;l<256;l++){
  31. if(Code[l]==null){
  32. Code[l]="";
  33. }
  34. }
  35. //凑成八位写入
  36. while(counts>=8||i<256){
  37. if(counts<8){
  38. s+=Code[i];
  39. counts=s.length();
  40. i++;
  41. }else{
  42. writes=s.substring(0,8);
  43. ou.write(changeString(writes));
  44. counts=counts-8;
  45. s=s.substring(8);
  46. //System.out.println(s);
  47. }
  48. }
  49. if(counts>0){
  50. //System.out.println("此时的s"+s);
  51. int len=8-counts;
  52. for(int j=0;j<len;j++){
  53. s=s+"0";
  54. }
  55. ou.write(changeString(s));
  56. //System.out.println(len+"写入的编码是k"+s);
  57. }
  58. //写入文件的编码
  59. FileInputStream In=new FileInputStream(file);
  60. int fcounts=0;
  61. //int fi=0;
  62. String fs="";
  63. String fwrites;
  64. while(fcounts>=8||In.available()>0){
  65. if(fcounts<8){
  66. int ii=In.read();
  67. fs+=Code[ii];
  68. fcounts=fs.length();
  69. //System.out.println(ii+" "+Code[ii]);
  70. }else{
  71. fwrites=fs.substring(0,8);
  72. ou.write(changeString(fwrites));
  73. fcounts=fcounts-8;
  74. fs=fs.substring(8);
  75. }
  76. }
  77. if(fcounts>0){
  78. int len=8-fcounts;
  79. for(int j=0;j<len;j++){
  80. fs=fs+"0";
  81. }
  82. ou.write(changeString(fs));
  83. //System.out.println("写入的文件编码是k"+fs);
  84. }
  85. //压缩结束
  86. in.close();
  87. In.close();
  88. Daou.close();
  89. }
  90. //将01字符串转换为01编码
  91. publicint changeString(String s){
  92. return (((int)s.charAt(0)-48)*128+((int)s.charAt(1)-48)*64+((int)s.charAt(2)-48)*32+((int)s.charAt(3)-48)*16+((int)s.charAt(4)-48)*8+((int)s.charAt(5)-48)*4+((int)s.charAt(6)-48)*2+((int)s.charAt(7)-48));
  93. }
Java代码  收藏代码
  1. //写入文件的总数,写入节点数,写入出现的字节,每个字节的编码长度,写入码表,写入文件的编码    
  2. public void writeBytesCodes() throws IOException{    
  3.     FileInputStream in=new FileInputStream(file);//获取文件的输入流    
  4.     FileOutputStream ou=new FileOutputStream(Pressedfile);    
  5.     DataOutputStream Daou=new DataOutputStream(ou);    
  6.     Daou.writeInt(in.available());//写入文件的总数    
  7.     ou.write(HfmCounts);//写入节点总数    
  8.     //System.out.println("写入的字节总数是"+HfmCounts);    
  9.     //写入出现过的字节    
  10.     int[] bc=getByteCounts();    
  11.     for(int i=0;i<256;i++){    
  12.         if(bc[i]!=0){    
  13.          ou.write(i);    
  14.         // System.out.println("写入的码表字节是"+i);    
  15.         }    
  16.     }    
  17.     //写入每个编码的长度    
  18.     for(int i=0;i<256;i++){    
  19.         if(Code[i]!=null){    
  20.             ou.write(Code[i].length());    
  21.            //System.out.println("写入的编码长度是"+Code[i].length());    
  22.         }    
  23.     }    
  24.     //写入码表    
  25.     int counts=0;    
  26.     int i=0;    
  27.     String s="";    
  28.     String writes;    
  29.     //首先对码表进行处理    
  30.     for(int l=0;l<256;l++){    
  31.         if(Code[l]==null){    
  32.             Code[l]="";    
  33.         }    
  34.     }    
  35.     //凑成八位写入    
  36.     while(counts>=8||i<256){    
  37.         if(counts<8){    
  38.             s+=Code[i];    
  39.             counts=s.length();    
  40.             i++;    
  41.         }else{    
  42.             writes=s.substring(0,8);    
  43.             ou.write(changeString(writes));    
  44.             counts=counts-8;    
  45.             s=s.substring(8);    
  46.             //System.out.println(s);    
  47.         }    
  48.         }    
  49.     if(counts>0){    
  50.         //System.out.println("此时的s"+s);    
  51.         int len=8-counts;    
  52.         for(int j=0;j<len;j++){    
  53.             s=s+"0";    
  54.         }    
  55.         ou.write(changeString(s));    
  56.         //System.out.println(len+"写入的编码是k"+s);    
  57.     }    
  58.     //写入文件的编码    
  59.     FileInputStream In=new FileInputStream(file);    
  60.     int fcounts=0;    
  61.     //int fi=0;    
  62.     String fs="";    
  63.     String fwrites;    
  64.     while(fcounts>=8||In.available()>0){    
  65.         if(fcounts<8){    
  66.             int ii=In.read();    
  67.             fs+=Code[ii];    
  68.             fcounts=fs.length();    
  69.             //System.out.println(ii+" "+Code[ii]);    
  70.         }else{    
  71.             fwrites=fs.substring(0,8);    
  72.             ou.write(changeString(fwrites));    
  73.             fcounts=fcounts-8;    
  74.             fs=fs.substring(8);    
  75.         }    
  76.     }    
  77.     if(fcounts>0){    
  78.         int len=8-fcounts;    
  79.         for(int j=0;j<len;j++){    
  80.             fs=fs+"0";    
  81.         }    
  82.         ou.write(changeString(fs));    
  83.         //System.out.println("写入的文件编码是k"+fs);    
  84.     }    
  85.     //压缩结束    
  86.     in.close();    
  87.     In.close();    
  88.     Daou.close();    
  89.     }    
  90. //将01字符串转换为01编码    
  91.     public int changeString(String s){    
  92.         return (((int)s.charAt(0)-48)*128+((int)s.charAt(1)-48)*64+((int)s.charAt(2)-48)*32+((int)s.charAt(3)-48)*16+((int)s.charAt(4)-48)*8+((int)s.charAt(5)-48)*4+((int)s.charAt(6)-48)*2+((int)s.charAt(7)-48));    
  93.     }    

/********************************************下面是解压**********************************************************************/

解压就是逆过程,不多介绍,看代码

Java代码 复制代码
  1. /************************开始解压*****************************/
  2. public FileInputStream Fp;
  3. public DataInputStream Dafp;
  4. publicint Hfmcounts=0;
  5. publicvoid k(String Pressedfile) throws IOException{
  6. Fp=new FileInputStream(Pressedfile);
  7. Dafp=new DataInputStream(Fp);
  8. }
  9. //得到文件总数
  10. publicint getFileCounts() throws Exception{
  11. int Filecounts;
  12. Filecounts=Dafp.readInt();
  13. return Filecounts;
  14. }
  15. //得到节点数
  16. publicint getHfmcounts() throws IOException{
  17. Hfmcounts=Fp.read();
  18. return Hfmcounts;
  19. }
  20. //得到的码表中的字节
  21. publicbyte[] getBytes() throws IOException{
  22. //System.out.println("========>"+Hfmcounts);
  23. byte[] by=newbyte[Hfmcounts];
  24. Fp.read(by,0,Hfmcounts);
  25. return by ;
  26. }
  27. //得到字节的长度
  28. publicbyte[] getCodeLenths() throws IOException{
  29. byte[] by=newbyte[Hfmcounts];
  30. Fp.read(by,0,Hfmcounts);
  31. return by ;
  32. }
  33. //得到码表编码
  34. public String[] getCodes() throws IOException{
  35. byte[] By=getCodeLenths();
  36. int count=0;
  37. for(int i=0;i<Hfmcounts;i++){
  38. count+=By[i];
  39. }
  40. String[] sCode=new String[Hfmcounts];
  41. int c1=count/8;
  42. int c2=count%8;
  43. int c=8-c2;
  44. //System.out.println("------>zhengza"+c1);
  45. String s="";
  46. if(c2!=0){
  47. c1+=1;
  48. int[] Codes=newint[c1];
  49. //Codes[0]= Fp.read();
  50. for(int i=0;i<c1;i++){
  51. Codes[i]=Fp.read();
  52. s+=changeint(Codes[i]);
  53. //System.out.println("++++"+s);
  54. }
  55. if(c1!=0){
  56. s=s.substring(0,s.length()-c);
  57. }
  58. }elseif(c2==0){
  59. int[] Codes=newint[c1];
  60. for(int i=0;i<c1;i++){
  61. Codes[i]=Fp.read();
  62. s+=changeint(Codes[i]);
  63. //System.out.println("----"+s);
  64. }
  65. }
  66. int k=0;
  67. int y=0;
  68. for(int i=0;i<Hfmcounts;i++){
  69. y=k+By[i];
  70. sCode[i]=s.substring(k,y);
  71. //System.out.println(y+"hhh"+sCode[i]);
  72. k=y;
  73. }
  74. return sCode;
  75. }
  76. //得到文件编码
  77. public String getFileCodes() throws Exception{
  78. //
  79. String s="";
  80. while(Fp.available()>0){
  81. s+=changeint(Fp.read());
  82. }
  83. return s;
  84. }
  85. //将01编码转换成01串
  86. public String changeint(int k){
  87. char [] st=newchar[8];
  88. String s="";
  89. int i=7;
  90. //String sk="";
  91. while(k!=0){
  92. st[i]=(char)(k%2+48);
  93. k=k/2;
  94. i--;
  95. }
  96. for(int j=i+1;j<8;j++){
  97. s+=st[j];
  98. }
  99. for(int ks=7-i;ks<8;ks++){
  100. s="0"+s;
  101. }
  102. return s;
  103. }
  104. //解压文件
  105. publicvoid ReleaseFile() throws Exception{
  106. k(Pressedfile.getAbsolutePath());
  107. FileOutputStream fo=new FileOutputStream(Releasedfile);
  108. int count=1;
  109. int c=0;
  110. int Filecounts=getFileCounts();
  111. getHfmcounts();
  112. //System.out.println(Filecounts);
  113. byte[] CodeBytes=getBytes();
  114. //System.out.println(CodeBytes[0]+""+CodeBytes[1]+""+CodeBytes[2]);
  115. String[] Codes=getCodes();
  116. //System.out.println(Codes[0]+""+Codes[1]+""+Codes[2]);
  117. String FileCodes=getFileCodes();
  118. //System.out.println(FileCodes+" "+Filecounts);
  119. int i=0;
  120. //int k=0;
  121. while(i<Filecounts&&count<=FileCodes.length()){
  122. String s=FileCodes.substring(c,count);
  123. //System.out.println(s);
  124. for(int j=0;j<Codes.length;j++){
  125. if(s.equals(Codes[j])){
  126. fo.write(CodeBytes[j]);
  127. c=count;
  128. //System.out.println("pppp"+CodeBytes[j]);
  129. i++;
  130. }
  131. }
  132. count++;
  133. }
  134. fo.close();
  135. }
  136. }
Java代码  收藏代码
  1. /************************开始解压*****************************/    
  2.     public FileInputStream Fp;    
  3.     public DataInputStream Dafp;    
  4.     public int Hfmcounts=0;    
  5.     public void k(String Pressedfile) throws IOException{    
  6.         Fp=new FileInputStream(Pressedfile);    
  7.         Dafp=new DataInputStream(Fp);    
  8.     }    
  9.     //得到文件总数    
  10.     public int getFileCounts() throws Exception{    
  11.         int Filecounts;    
  12.             
  13.         Filecounts=Dafp.readInt();    
  14.             
  15.         return Filecounts;    
  16.     }    
  17.     //得到节点数    
  18.     public int getHfmcounts() throws IOException{    
  19.          Hfmcounts=Fp.read();    
  20.         return Hfmcounts;    
  21.     }    
  22.     //得到的码表中的字节    
  23.     public byte[] getBytes() throws IOException{    
  24.         //System.out.println("========>"+Hfmcounts);    
  25.         byte[] by=new byte[Hfmcounts];    
  26.         Fp.read(by,0,Hfmcounts);    
  27.         return by ;    
  28.     }    
  29.     //得到字节的长度    
  30.     public byte[] getCodeLenths() throws IOException{    
  31.         byte[] by=new byte[Hfmcounts];    
  32.         Fp.read(by,0,Hfmcounts);    
  33.         return by ;    
  34.     }    
  35.     //得到码表编码    
  36.     public String[] getCodes() throws IOException{    
  37.         byte[] By=getCodeLenths();    
  38.         int count=0;    
  39.         for(int i=0;i<Hfmcounts;i++){    
  40.             count+=By[i];    
  41.         }    
  42.         
  43.         String[] sCode=new String[Hfmcounts];    
  44.         int c1=count/8;    
  45.         int c2=count%8;    
  46.         int c=8-c2;    
  47.         //System.out.println("------>zhengza"+c1);    
  48.         String s="";    
  49.         if(c2!=0){    
  50.             c1+=1;    
  51.             int[] Codes=new int[c1];    
  52.             //Codes[0]= Fp.read();    
  53.             for(int i=0;i<c1;i++){    
  54.                 Codes[i]=Fp.read();    
  55.                 s+=changeint(Codes[i]);    
  56.                 //System.out.println("++++"+s);    
  57.             }    
  58.             if(c1!=0){    
  59.             s=s.substring(0,s.length()-c);    
  60.             }    
  61.         }else if(c2==0){    
  62.             int[] Codes=new int[c1];    
  63.             for(int i=0;i<c1;i++){    
  64.                 Codes[i]=Fp.read();    
  65.                 s+=changeint(Codes[i]);    
  66.                 //System.out.println("----"+s);    
  67.             }    
  68.         }    
  69.         int k=0;    
  70.         int y=0;    
  71.         for(int i=0;i<Hfmcounts;i++){    
  72.             y=k+By[i];    
  73.             sCode[i]=s.substring(k,y);    
  74.             //System.out.println(y+"hhh"+sCode[i]);    
  75.             k=y;    
  76.         }    
  77.         return sCode;    
  78.     }    
  79.     //得到文件编码    
  80.     public String getFileCodes() throws Exception{    
  81.         //    
  82.         String s="";    
  83.         while(Fp.available()>0){    
  84.             s+=changeint(Fp.read());    
  85.         }    
  86.         return s;    
  87.     }    
  88.     //将01编码转换成01串    
  89.     public String changeint(int k){    
  90.         char [] st=new char[8];    
  91.         String s="";    
  92.         int i=7;    
  93.         //String sk="";    
  94.         while(k!=0){    
  95.             st[i]=(char)(k%2+48);    
  96.             k=k/2;    
  97.             i--;    
  98.         }    
  99.         for(int j=i+1;j<8;j++){    
  100.             s+=st[j];    
  101.         }    
  102.         for(int ks=7-i;ks<8;ks++){    
  103.             s="0"+s;    
  104.         }    
  105.         return s;    
  106.     }    
  107.     //解压文件    
  108.         public void ReleaseFile() throws Exception{    
  109.              k(Pressedfile.getAbsolutePath());    
  110.             FileOutputStream fo=new FileOutputStream(Releasedfile);    
  111.             int count=1;    
  112.             int c=0;    
  113.             int Filecounts=getFileCounts();    
  114.             getHfmcounts();    
  115.             //System.out.println(Filecounts);    
  116.             byte[] CodeBytes=getBytes();    
  117.             //System.out.println(CodeBytes[0]+""+CodeBytes[1]+""+CodeBytes[2]);    
  118.             String[] Codes=getCodes();    
  119.             //System.out.println(Codes[0]+""+Codes[1]+""+Codes[2]);    
  120.             String FileCodes=getFileCodes();    
  121.             //System.out.println(FileCodes+"  "+Filecounts);    
  122.             int i=0;    
  123.             //int k=0;    
  124.             while(i<Filecounts&&count<=FileCodes.length()){    
  125.                 String s=FileCodes.substring(c,count);    
  126.                 //System.out.println(s);    
  127.                 for(int j=0;j<Codes.length;j++){    
  128.                     if(s.equals(Codes[j])){    
  129.                         fo.write(CodeBytes[j]);    
  130.                         c=count;    
  131.                         //System.out.println("pppp"+CodeBytes[j]);    
  132.                         i++;    
  133.                     }    
  134.                 }    
  135.                 count++;    
  136.             }    
  137.             fo.close();    
  138.         }    
  139.     
  140.         }    

主要方法就是这些,下面就是界面了

Java代码 复制代码
  1. publicclass Jframe extends JFrame implements ActionListener{
  2. public Cop c;
  3. public Jframe(Cop c){
  4. this.c=c;
  5. }
  6. publicvoid UI(){
  7. this.setTitle("黄飞压缩工具");
  8. this.setSize(400200);
  9. this.setLocationRelativeTo(null);//让窗体居中
  10. this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("头像.png")));
  11. this.setDefaultCloseOperation(3);
  12. //this.setAlwaysOnTop(true);
  13. //构建一个JPanel面板
  14. JPanel panel=new JPanel();
  15. BoxLayout layout=new BoxLayout(panel, BoxLayout.Y_AXIS);//箱式布局,按照Y轴往下排列
  16. panel.setLayout(layout);
  17. //按钮,标签
  18. JButton jb1=new JButton(" 压缩 ");
  19. jb1.setActionCommand("压缩");
  20. JLabel jl=new JLabel("选择你要执行的功能");
  21. JButton jb2=new JButton(" 解压 ");
  22. jb2.setActionCommand("解压");
  23. //居中排列
  24. jb1.setAlignmentX(Component.CENTER_ALIGNMENT);
  25. jl.setAlignmentX(Component.CENTER_ALIGNMENT);
  26. jb2.setAlignmentX(Component.CENTER_ALIGNMENT);
  27. //把组件加到面板上
  28. panel.add(jb1);
  29. panel.add(jl);
  30. panel.add(jb2);
  31. this.add(panel);
  32. //加入监听器
  33. jb1.addActionListener(this);
  34. jb2.addActionListener(this);
  35. this.setVisible(true);
  36. }
  37. /**
  38. * @param args
  39. */
  40. publicstaticvoid main(String[] args) {
  41. // TODO Auto-generated method stub
  42. Cop c=new Cop();
  43. Jframe j=new Jframe(c);
  44. j.UI();
  45. }
  46. @Override
  47. publicvoid actionPerformed(ActionEvent e) {
  48. // 压缩过程
  49. if(e.getActionCommand().equals("压缩")){
  50. //压缩选择,跳出文件选择器,选择要压缩的文件
  51. JOptionPane.showMessageDialog(null,"选择要压缩的文件");
  52. JFileChooser chooser = new JFileChooser();
  53. chooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG );//打开型
  54. chooser.setApproveButtonText("压缩");//设置一个压缩按钮
  55. chooser.showDialog(nullnull);//弹出选择器
  56. c.file=chooser.getSelectedFile();//得到要压缩的文件
  57. //保存过程
  58. JOptionPane.showMessageDialog(null,"选择要保存的路径");
  59. JFileChooser ch = new JFileChooser();
  60. ch.setFileSelectionMode(JFileChooser.SAVE_DIALOG );//保存类型
  61. ch.setApproveButtonText("保存");//保存按钮
  62. ch.showDialog(nullnull);//弹出窗口
  63. //得到出去扩展名的文件名
  64. int ii=c.file.getName().lastIndexOf(".");
  65. String str=c.file.getName().substring(0,ii);
  66. //得到压缩后的文件
  67. File f=new File(ch.getSelectedFile(),str+".hf");
  68. c.Pressedfile=f;
  69. try {
  70. f.createNewFile();//创建压缩后的文件
  71. c.Compressfile();//压缩开始
  72. JOptionPane.showMessageDialog(null,"压缩完毕");
  73. catch (IOException e1) {
  74. // TODO Auto-generated catch block
  75. e1.printStackTrace();
  76. }
  77. }
  78. //解压过程
  79. elseif(e.getActionCommand().equals("解压")){
  80. //解压过程,同上
  81. JOptionPane.showMessageDialog(null,"选择要解缩的文件");
  82. JFileChooser chooser = new JFileChooser("wenjian");
  83. chooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG );
  84. chooser.setApproveButtonText("解压");
  85. FileNameExtensionFilter filter = new FileNameExtensionFilter("hf","hf");
  86. chooser.setFileFilter(filter);
  87. chooser.showDialog(nullnull);
  88. c.Pressedfile=chooser.getSelectedFile();
  89. //保存
  90. JOptionPane.showMessageDialog(null"选择解压文件的保存路径");
  91. JFileChooser ch = new JFileChooser("wenjian");
  92. ch.setFileSelectionMode(JFileChooser.SAVE_DIALOG );
  93. ch.setApproveButtonText("保存");
  94. ch.showDialog(nullnull);
  95. int ii=c.Pressedfile.getName().lastIndexOf(".");
  96. String str=c.Pressedfile.getName().substring(0,ii);
  97. File f=new File(ch.getSelectedFile(),str+".txt");
  98. try {
  99. f.createNewFile();
  100. c.Releasedfile=f;
  101. System.out.println(f.getAbsolutePath());
  102. c.ReleaseFile();
  103. JOptionPane.showMessageDialog(null,"解压完毕");
  104. // c.ReleaseFile();
  105. catch (IOException e1) {
  106. // TODO Auto-generated catch block
  107. e1.printStackTrace();
  108. catch (Exception e1) {
  109. // TODO Auto-generated catch block
  110. e1.printStackTrace();
  111. }
  112. };
  113. }
  114. }
Java代码  收藏代码
  1. public class Jframe extends JFrame implements ActionListener{    
  2.     public Cop c;    
  3.     public Jframe(Cop c){    
  4.         this.c=c;    
  5.     }    
  6. public void UI(){    
  7.     this.setTitle("黄飞压缩工具");    
  8.     this.setSize(400200);    
  9.     this.setLocationRelativeTo(null);//让窗体居中    
  10.     this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("头像.png")));    
  11.     this.setDefaultCloseOperation(3);    
  12.     //this.setAlwaysOnTop(true);    
  13.     //构建一个JPanel面板    
  14.      JPanel panel=new JPanel();     
  15.      BoxLayout layout=new BoxLayout(panel, BoxLayout.Y_AXIS);//箱式布局,按照Y轴往下排列     
  16.      panel.setLayout(layout);    
  17.      //按钮,标签    
  18.      JButton jb1=new JButton("    压缩           ");    
  19.      jb1.setActionCommand("压缩");    
  20.      JLabel jl=new JLabel("选择你要执行的功能");    
  21.      JButton jb2=new JButton("    解压           ");    
  22.      jb2.setActionCommand("解压");    
  23.      //居中排列    
  24.      jb1.setAlignmentX(Component.CENTER_ALIGNMENT);    
  25.      jl.setAlignmentX(Component.CENTER_ALIGNMENT);    
  26.      jb2.setAlignmentX(Component.CENTER_ALIGNMENT);    
  27.      //把组件加到面板上    
  28.      panel.add(jb1);    
  29.      panel.add(jl);    
  30.      panel.add(jb2);    
  31.      this.add(panel);    
  32.      //加入监听器    
  33.      jb1.addActionListener(this);    
  34.      jb2.addActionListener(this);    
  35.      this.setVisible(true);    
  36. }    
  37.     /**  
  38.      * @param args  
  39.      */    
  40.     public static void main(String[] args) {    
  41.         // TODO Auto-generated method stub    
  42.        Cop c=new Cop();    
  43.        Jframe j=new Jframe(c);    
  44.        j.UI();    
  45.     }    
  46.     @Override    
  47.     public void actionPerformed(ActionEvent e) {    
  48.         // 压缩过程    
  49.         if(e.getActionCommand().equals("压缩")){    
  50.             //压缩选择,跳出文件选择器,选择要压缩的文件    
  51.            JOptionPane.showMessageDialog(null,"选择要压缩的文件");    
  52.            JFileChooser chooser = new JFileChooser();    
  53.            chooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG );//打开型    
  54.            chooser.setApproveButtonText("压缩");//设置一个压缩按钮    
  55.            chooser.showDialog(nullnull);//弹出选择器    
  56.            c.file=chooser.getSelectedFile();//得到要压缩的文件    
  57.           //保存过程    
  58.            JOptionPane.showMessageDialog(null,"选择要保存的路径");    
  59.            JFileChooser ch = new JFileChooser();    
  60.            ch.setFileSelectionMode(JFileChooser.SAVE_DIALOG );//保存类型    
  61.            ch.setApproveButtonText("保存");//保存按钮    
  62.            ch.showDialog(nullnull);//弹出窗口    
  63.            //得到出去扩展名的文件名    
  64.            int ii=c.file.getName().lastIndexOf(".");    
  65.            String str=c.file.getName().substring(0,ii);    
  66.            //得到压缩后的文件    
  67.            File f=new File(ch.getSelectedFile(),str+".hf");    
  68.            c.Pressedfile=f;    
  69.            try {    
  70.             f.createNewFile();//创建压缩后的文件    
  71.             c.Compressfile();//压缩开始    
  72.              JOptionPane.showMessageDialog(null,"压缩完毕");    
  73.         } catch (IOException e1) {    
  74.             // TODO Auto-generated catch block    
  75.             e1.printStackTrace();    
  76.         }    
  77.         }    
  78.         //解压过程    
  79.         else if(e.getActionCommand().equals("解压")){    
  80.              //解压过程,同上    
  81.               JOptionPane.showMessageDialog(null,"选择要解缩的文件");    
  82.               JFileChooser chooser = new JFileChooser("wenjian");    
  83.                chooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG );    
  84.                chooser.setApproveButtonText("解压");    
  85.                FileNameExtensionFilter filter = new FileNameExtensionFilter("hf","hf");    
  86.                chooser.setFileFilter(filter);    
  87.                chooser.showDialog(nullnull);    
  88.                c.Pressedfile=chooser.getSelectedFile();    
  89.                //保存    
  90.                JOptionPane.showMessageDialog(null"选择解压文件的保存路径");    
  91.                JFileChooser ch = new JFileChooser("wenjian");    
  92.                ch.setFileSelectionMode(JFileChooser.SAVE_DIALOG );    
  93.                ch.setApproveButtonText("保存");    
  94.                ch.showDialog(nullnull);    
  95.                int ii=c.Pressedfile.getName().lastIndexOf(".");    
  96.                String str=c.Pressedfile.getName().substring(0,ii);    
  97.                File f=new File(ch.getSelectedFile(),str+".txt");    
  98.                try {    
  99.                 f.createNewFile();    
  100.                 c.Releasedfile=f;    
  101.                  System.out.println(f.getAbsolutePath());    
  102.                  c.ReleaseFile();    
  103.                  JOptionPane.showMessageDialog(null,"解压完毕");    
  104.                 // c.ReleaseFile();     
  105.             } catch (IOException e1) {    
  106.                 // TODO Auto-generated catch block    
  107.                 e1.printStackTrace();    
  108.             } catch (Exception e1) {    
  109.                 // TODO Auto-generated catch block    
  110.                 e1.printStackTrace();    
  111.             }    
  112.                   
  113.                    
  114.         };    
  115.     }    
  116.     
  117. }    

界面主要就是一个文件选择器JFileChooser 的应用,查看API就能具体了解

分享到:
评论

相关推荐

    java源码包---java 源码 大量 实例

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    JAVA解压ZIP格式的压缩包_java解压缩_zip_

    java 中压缩ZIP格式源码,仅供参考。

    哈夫曼树实现的压缩工具Java源代码

    哈夫曼树实现的压缩工具源代码,用netbeans导入可以看见源代码,仅供学习参考

    JAVA上百实例源码以及开源项目

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    java源码包4

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java数据压缩与传输实例 1个目标文件 摘要:Java源码,文件操作,数据压缩,文件传输 Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、...

    JAVA上百实例源码以及开源项目源代码

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    java源码包3

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    java源码包2

     Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Java数据压缩与传输实例 1个目标文件 摘要:Java源码,文件操作,数据压缩,文件传输 Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、...

    哈弗曼树压缩程序

    5.郑重声明:本软件仅供学习交流之用,但下载者不要被局限于此,还应探究更好的算法和实现。更不要抄袭以应付考试等,否则后果自负! 6.体验过程中若发现问题或不足,请与我联系,我会继续优化,谢谢!

    Java代码混淆工具 Proguard4.10(官方免费下载)

    8、《testjava.pro》是我自己的配置文档(一个helloWorld),仅供参考 9、日记《success-log.txt》 混淆有利有弊,混淆须谨慎 1、混淆后的代码出错,如何精确快速定位?如果对系统架构,编程,数据配置等非常清楚...

    Java Web应用开发:Tomcat和eclipse for javaee配置.docx

    下载tomcat8后,解压缩,下面截图的解压后路径仅供参考(这是我电脑里的路径)。 强烈建议不要乱放,放到D:\Program Files\中,或者C:\Program Files\ 3.2 测试 (1) 打开tomcat解压后根目录下的bin文件夹,例如(C...

    易语言程序免安装版下载

     支持静态链接其它编程语言(如C/C++、汇编等)编译生成的静态库(.LIB或.OBJ),但仅限于COFF格式,支持cdecl和stdcall两种函数调用约定。  使用说明如下:函数声明和调用方法与DLL命令一致;“库文件名”以.lib...

    JDK ZipEntry压缩中文文件名乱码解决

    项目中碰到问题.jdk zipEntry 压缩中文文件名乱码 ... 如果仅用到压缩,就加入ant.jar 就导致引入了一些不必要的文件,所以我找到Ant1.8的源码,然后只把需要的那一部分编译打包成了一个jar文件,供大家使用

    网管教程 从入门到精通软件篇.txt

    JAR:Java档案文件(一种用于applet和相关文件的压缩文件) JAVA:Java源文件 JAR:Java档案文件(一种用于applet和相关文件的压缩文件) JAVA:Java源文件 JFF,JFIF,JIF:JPEG文件 JPE,JPEG,JPG:JPEG图形...

    基于框架的Web开发-Tomcat和eclipseforjavaee配置.docx

    下载tomcat8后,解压缩,下面截图的解压后路径仅供参考(下图是我电脑里的路径)。 强烈建议不要乱放,几个注意点: 由于win10的C盘(系统盘)可能会拒绝访问,建议解压到D:\Program Files\中,或者E,F等非系统盘中...

    基于JAVA局域网的聊天室系统ChatClient+ChatServer软件源代码+设计文档资料说明.7z

    基于JAVA局域网的聊天室系统ChatClient+ChatServer软件源代码+设计文档资料说明,仅供学习及设计参考。 视频聊天系统作为一种新型的通信和交流工具,突破了地域的限制,可以提供更为便捷、灵活、全面的音、视频信息...

    高级java笔试题-data_pirate:数据海盗

    本测试代码仅供安全测试以及网络协议学习之用,由本代码所造成的一切后果,本人概不负责 本代码初始为简单学习tcp协议之用,实现劫持功能并非最简单方式 代码写作到后期已经如同搬砖,兴趣索然,由于笔者精力以及...

Global site tag (gtag.js) - Google Analytics