zip压缩文件

/**
 * f-road.com Inc.
 * Copyright (c) 2013 All Rights Reserved.
 */
package com.froad.utils;
import java.io.BufferedOutputStream;  
import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
 
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.util.zip.Adler32;  
import java.util.zip.CheckedOutputStream;  
 
import org.apache.tools.zip.ZipEntry;  
import org.apache.tools.zip.ZipOutputStream;  

/**
 * <pre>
 * 压缩文件
 * </pre>
 *
 * @author xueyunlong
 * @create 2013年11月4日 下午1:13:39
 * @modify
 * @since   JDK1.6
 */
public class FileZipUtil {
    /**
     * 中文条件下文件(夹)压缩
     * @throws IOException
     */  
    public static void zipCompress(String src,String des)   
        throws IOException{  
        ZipOutputStream out=null;  
        try {  
            CheckedOutputStream cusm=  
                new CheckedOutputStream(new FileOutputStream(des),new Adler32());  
            out=new ZipOutputStream(new BufferedOutputStream(cusm));  
              
            fileZip(new File(src),out,"");  
        }finally{  
            if(out!=null){  
                out.close();  
            }  
        }  
    }  
 
      
    private static void fileZip(File file, ZipOutputStream out,   
                String base)  throws IOException{  
        if(file.isFile()){  
            if(base.length()>0){  
                out.putNextEntry(new ZipEntry(base));  
            }else{  
                out.putNextEntry(new ZipEntry(file.getName()));  
            }  
              
            BufferedReader in=new BufferedReader(  
                    new InputStreamReader(new FileInputStream(file),"ISO8859-1"));  
              
            int c;  
            while((c=in.read())!=-1){
                out.write(c);  
            }  
            in.close();  
              
        }else if(file.isDirectory()){
            File[] subFiles=file.listFiles();  
//            out.putNextEntry(new ZipEntry(base+File.separator));  
            base=base.length()!=0?base+File.separator:"";  
            for(File subFile:subFiles){  
                fileZip(subFile,out,base+subFile.getName());  
            }  
        }  
          
    }     
}  

你可能感兴趣的:(zip)