简单的解压小程序

 
  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import java.io.*; 
  4. import java.nio.CharBuffer;
  5. import java.nio.charset.Charset;
  6. import java.nio.charset.CharsetDecoder;
  7. import javax.swing.*; 
  8. import javax.swing.filechooser.FileFilter;
  9. import java.util.Vector; 
  10. import java.util.zip.*; 
  11. public class unZip extends JFrame{ 
  12.     private File file; 
  13.     private JTextField field; 
  14.     private JButton buttonSD; 
  15.     private JButton buttonOK; 
  16.     private Container container; 
  17.     private ZipOutputStream zipos; 
  18.    private  ZipInputStream zin ;
  19.     public unZip() { 
  20.         super("unZip"); 
  21.         container=getContentPane(); 
  22.         container.setLayout(new FlowLayout()); 
  23.         field=new JTextField(30); 
  24.         buttonSD=new JButton("..."); 
  25.         buttonOK=new JButton("OK"); 
  26.         
  27.         buttonSD.addActionListener(new ActionListener(){ 
  28.             public void actionPerformed(ActionEvent arg0) { 
  29.                 ShowOpenDialog(); 
  30.             } 
  31.              
  32.         }); 
  33.         buttonOK.addActionListener(new ActionListener(){ 
  34.             public void actionPerformed(ActionEvent arg0) { 
  35.                 startUnzip();; 
  36.             } 
  37.              
  38.         }); 
  39.         container.add(field); 
  40.         container.add(buttonSD); 
  41.         container.add(buttonOK); 
  42.         setSize(400,150); 
  43.         setVisible(true); 
  44.          
  45.     } 
  46.     private void startUnzip() { 
  47.         
  48.         File zfp=new File(field.getText()); 
  49.        
  50.     
  51.             try {
  52.                 zin = new ZipInputStream(new FileInputStream(zfp));
  53.             } catch (FileNotFoundException e) {
  54.                 JOptionPane.showMessageDialog(this"文件无法读取","提示",JOptionPane.INFORMATION_MESSAGE ); 
  55.             }
  56.             ZipEntry outZipEntry = null;
  57.             try {
  58.                 outZipEntry = zin.getNextEntry();
  59.             } catch (IOException e) {
  60.                 JOptionPane.showMessageDialog(this"无法取得条目","提示",JOptionPane.INFORMATION_MESSAGE ); 
  61.             }
  62.             int r=0;
  63.             byte []b=new byte[4096];
  64.             FileOutputStream out = null;
  65.             while(outZipEntry!=null){
  66.                 try {
  67.                     File file=new File(zfp.getParent()+"//"+outZipEntry.getName());
  68.                     File parentPath=new File(file.getParent());
  69.                     if(!parentPath.exists()){
  70.                         parentPath.mkdirs();
  71.                     }
  72.                     
  73.                     out=new FileOutputStream(file);
  74.                     System.err.println(System.getProperties().getProperty("user.dir")+"//"+outZipEntry.getName());
  75.                 } catch (FileNotFoundException e) {
  76.                     JOptionPane.showMessageDialog(this, System.getProperties().getProperty("user.dir")+"//"+outZipEntry.getName(),"a cue",JOptionPane.INFORMATION_MESSAGE ); 
  77.                 }
  78.                 try {
  79.                     while((r=zin.read(b, 04096))>-1){
  80.                         out.write(b, 0, r);
  81.                     }
  82.                     out.close();
  83.                 } catch (IOException e) {
  84.                     JOptionPane.showMessageDialog(this"读取文件失败","提示",JOptionPane.INFORMATION_MESSAGE ); 
  85.                 }
  86.                 try {
  87.                     outZipEntry=zin.getNextEntry();
  88.                 } catch (IOException e) {
  89.                     JOptionPane.showMessageDialog(this"无法取得条目","提示",JOptionPane.INFORMATION_MESSAGE ); 
  90.                 }
  91.             }
  92.      
  93.             JOptionPane.showMessageDialog(this"解压完成","提示",JOptionPane.INFORMATION_MESSAGE ); 
  94.        
  95.     } 
  96.     protected void ShowOpenDialog() { 
  97.         JFileChooser filechooser=new JFileChooser(); 
  98.         filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
  99.         int result=filechooser.showOpenDialog(this); 
  100.         if(result==JFileChooser.CANCEL_OPTION)return
  101.         field.setText(filechooser.getSelectedFile().toString()); 
  102.     } 
  103.     public static void main(String[] args) { 
  104.         unZip app=new unZip(); 
  105.         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  106.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jcompress 是一款基于哈夫曼编码和最小堆的无损压缩、解压小程序,支持任何格式的文件的压缩与解压缩。Jcompress 的源代码位于 Utility 的 repository 分类下的 Jcompress 目录,后续会在 Utility 下面增加其他一些实用的小程序,比如基于 socket 的文件断点下载小程序等等。Jcompress代码实现1. 最小堆代码实现最小堆排序算法基本上是按照严蔚敏版的算法来实现的,其具体功能这里不再赘述,仅列出代码,读者可以参考课本自行分析之。首先是heap_min_adjust,也就是调整堆,代码如下所示:int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end){       /**       ** check error for argument       */       if(huffman_node_array==NULL || data_start<0 || data_end<0 || data_end<data_start){           printf("heap_min_adjust: argument error\n");           exit(0);       }          if(data_end==data_start) return 1;       /**       ** the top of heap-min indicated by index data_start is the only element       ** which need to be adjusted to make a min-heap       */       HuffmanNode * current_data_tobe_adjust=huffman_node_array[data_start];       long current_indexof_data=data_start;          for(long cur=2*data_start;cur<=data_end;cur=2*cur){           if(curdata_8bit_count)>((huffman_node_array[cur 1])->data_8bit_count)){                   cur =1;               }           }           if((current_data_tobe_adjust->data_8bit_count)data_8bit_count))               break;           huffman_node_array[current_indexof_data]=huffman_node_array[cur];           current_indexof_data=cur;       }       huffman_node_array[current_indexof_data]=current_data_tobe_adjust;       /**       ** return 1 means everything is ok       */       return 1;   }接下来是heap_min_construct()的代码,此函数是通过不断的调用上面的调整堆的函数来达到堆排序的目的。int heap_min_construct(HuffmanNode **huffman_node_array, long array_size){       /** valid data start from index 1 not 0 */          /**       ** check error for argument       */        if(huffman_node_array==NULL || array_size256){           printf("heap_min_construct: argument error\n");           exit(0);        }           for(long HeapRoot=array_size/2; HeapRoot>=1;HeapRoot--){           heap_min_adjust(huffman_node_array,HeapRoot,array_size);        }        return 1;   }最后是heap_min_get2min()函数。int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size){          /**       ** check argument       **/        if(huffman_node_array==NULL){           printf("heap_min_get2min: argument error\n");           exit(0);        }          *min_first=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;       /** after we get the min data, we should again adjust the heap to make a min-heap */       heap_min_adjust(huffman_node_array,1,*heap_size);          *min_second=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;          /** the same as above*/       if(*heap_size>0){           heap_min_adjust(huffman_node_array,1,*heap_size);       }          return 1;   } 标签:Jcompress
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值