zoukankan      html  css  js  c++  java
  • java压缩解压zip文件,中文乱码还需要ant.jar包

    va] view plaincopyprint?

    1. package cn.cn;  
    2. import java.io.BufferedOutputStream;  
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.io.FileOutputStream;  
    6. import java.io.IOException;  
    7. import java.io.InputStream;  
    8. import java.util.Enumeration;  
    9. import java.util.zip.Deflater;  
    10.   
    11. import org.apache.tools.zip.ZipEntry;  
    12. import org.apache.tools.zip.ZipFile;  
    13. import org.apache.tools.zip.ZipOutputStream;  
    14.   
    15. public class ZipUtil {  
    16.     private ZipFile         zipFile;   
    17.     private ZipOutputStream zipOut;     //压缩Zip   
    18.     private  int            bufSize;    //size of bytes   
    19.     private byte[]          buf;   
    20.   
    21.     public ZipUtil(){  
    22.         //要构造函数中去初始化我们的缓冲区  
    23.         this.bufSize = 1024*4;   
    24.         this.buf = new byte[this.bufSize];   
    25.     }   
    26.        
    27.     /** 
    28.      * 对传入的目录或者是文件进行压缩 
    29.      * @param srcFile  需要 压缩的目录或者文件 
    30.      * @param destFile 压缩文件的路径 
    31.      */  
    32.     public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名  
    33.         File zipFile = new File(srcFile);  
    34.         try {  
    35.             //生成ZipOutputStream,会把压缩的内容全都通过这个输出流输出,最后写到压缩文件中去  
    36.             this.zipOut = new ZipOutputStream(new BufferedOutputStream(  
    37.                     new FileOutputStream(destFile)));  
    38.             //设置压缩的注释  
    39.             zipOut.setComment("comment");  
    40.             //设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码  
    41.             zipOut.setEncoding("GBK");  
    42.             //启用压缩   
    43.             zipOut.setMethod(ZipOutputStream.DEFLATED);   
    44.   
    45.             //压缩级别为最强压缩,但时间要花得多一点   
    46.             zipOut.setLevel(Deflater.BEST_COMPRESSION);   
    47.               
    48.             handleFile(zipFile, this.zipOut,"");  
    49.             //处理完成后关闭我们的输出流  
    50.             this.zipOut.close();  
    51.         } catch (IOException ioe) {  
    52.             ioe.printStackTrace();  
    53.         }  
    54.     }  
    55.   
    56.     /** 
    57.      *  由doZip调用,递归完成目录文件读取 
    58.      * @param zipFile 
    59.      * @param zipOut 
    60.      * @param dirName  这个主要是用来记录压缩文件的一个目录层次结构的 
    61.      * @throws IOException 
    62.      */  
    63.     private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException {  
    64.         System.out.println("遍历文件:"+zipFile.getName());  
    65.         //如果是一个目录,则遍历  
    66.         if(zipFile.isDirectory())  
    67.         {  
    68.             File[] files = zipFile.listFiles();  
    69.   
    70.             if (files.length == 0) {// 如果目录为空,则单独创建之.  
    71.                 //只是放入了空目录的名字  
    72.                 this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator));  
    73.                 this.zipOut.closeEntry();  
    74.             } else {// 如果目录不为空,则进入递归,处理下一级文件  
    75.                 for (File file : files) {  
    76.                     // 进入递归,处理下一级的文件  
    77.                     handleFile(file, zipOut, dirName+zipFile.getName()+File.separator);  
    78.                 }  
    79.             }  
    80.         }  
    81.         //如果是文件,则直接压缩  
    82.         else  
    83.         {  
    84.             FileInputStream fileIn  = new FileInputStream(zipFile);  
    85.             //放入一个ZipEntry  
    86.             this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()));  
    87.             int length = 0;  
    88.             //放入压缩文件的流  
    89.             while ((length = fileIn.read(this.buf)) > 0) {  
    90.                 this.zipOut.write(this.buf, 0, length);  
    91.             }  
    92.             //关闭ZipEntry,完成一个文件的压缩  
    93.             this.zipOut.closeEntry();  
    94.         }  
    95.           
    96.     }  
    97.   
    98.     /** 
    99.      * 解压指定zip文件 
    100.      * @param unZipfile 压缩文件的路径 
    101.      * @param destFile   解压到的目录  
    102.      */  
    103.     public void unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名  
    104.         FileOutputStream fileOut;  
    105.         File file;  
    106.         InputStream inputStream;  
    107.   
    108.         try {  
    109.             //生成一个zip的文件  
    110.             this.zipFile = new ZipFile(unZipfile);  
    111.             //遍历zipFile中所有的实体,并把他们解压出来  
    112.             for (@SuppressWarnings("unchecked")  
    113.             Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries  
    114.                     .hasMoreElements();) {  
    115.                 ZipEntry entry =  entries.nextElement();  
    116.                 //生成他们解压后的一个文件  
    117.                 file = new File(destFile+File.separator+entry.getName());  
    118.   
    119.                 if (entry.isDirectory()) {  
    120.                     file.mkdirs();  
    121.                 } else {  
    122.                     // 如果指定文件的目录不存在,则创建之.  
    123.                     File parent = file.getParentFile();  
    124.                     if (!parent.exists()) {  
    125.                         parent.mkdirs();  
    126.                     }  
    127.                     //获取出该压缩实体的输入流  
    128.                     inputStream = zipFile.getInputStream(entry);  
    129.   
    130.                     fileOut = new FileOutputStream(file);  
    131.                     int length = 0;  
    132.                     //将实体写到本地文件中去  
    133.                     while ((length = inputStream.read(this.buf)) > 0) {  
    134.                         fileOut.write(this.buf, 0, length);  
    135.                     }  
    136.                     fileOut.close();  
    137.                     inputStream.close();  
    138.                 }  
    139.             }  
    140.             this.zipFile.close();  
    141.         } catch (IOException ioe) {  
    142.             ioe.printStackTrace();  
    143.         }  
    144.     } 
  • 相关阅读:
    设计数据密集型应用(中英双语)
    缓存设计中的热点问题讨论
    Python Twisted
    Python-memcached的使用用法
    四款免费好用的Bootstrap ui编辑器
    8个强大的基于Bootstrap的CSS框架
    盘点国内网站常用的一些 CDN 公共库加速服务
    分布式缓存系统 Memcached 快速入门
    函数缓存 (Function caching)
    Golang新开发者要注意的陷阱和常见错误
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3871114.html
Copyright © 2011-2022 走看看