java文件压缩类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


/**
 * <p>类描述:压缩工具</p>
 *
 * @author: zhudz18200
 * @version: 2016-8-22下午06:35:37
 * ***********************************************
 * @Copyright: 2016 恒生电子股份有限公司.
 * All right reserved.
 * ***********************************************
 */
public class ZipUtils {

    /**
     * 文件压缩
     * 
     * @param fileName
     *            待压缩文件名
     * @param zipFileName
     *            压缩后文件名
     * @param path
     *            文件路径
     */
    @SuppressWarnings("unchecked")
    public static void genZipFile(String fileName, String zipFileName,
            String path) {
        List list = new ArrayList();
        list.add(fileName);
        genZipFile(list, zipFileName, path);
    }
    
    /**
     * 将文件列表中指定的文件打包成zip文件
     * 
     * @param fileList
     *                 待压缩文件名列表
     * @param zipFileName
     *                 压缩后文件名
     * @param path
     *                文件路径
     */
    public static void genZipFile(List fileList, String zipFileName, String path) {

        byte[] buf = new byte[8192];

        try {

            String outFilename = null;

            if (path.endsWith("/") || path.endsWith("\\")) {
                int index = path.lastIndexOf("/");
                if (index != -1) {
                    path = path.substring(0, index);
                } else {
                    index = path.lastIndexOf("\\");
                    path = path.substring(0, index);
                }

            }
            outFilename = path + "/" + zipFileName;
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                    outFilename));
            out.setEncoding("GBK");
            for (int i = 0; i < fileList.size(); i++) {
                FileInputStream in = new FileInputStream(path + "/"
                        + fileList.get(i));
                out.putNextEntry(new ZipEntry((String) fileList.get(i)));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (IOException e) {
        }
    }
    
    /**
     * 将文件/文件夹 打包成zip文件
     * 
     * @param zipFileName
     *                 压缩后文件名
     * @param path
     *                文件路径
     */
    public static void genZipFile(String zipFileName, String path) { 
        
        File zipFile = new File(zipFileName); 
        
        File srcdir = new File(path);
        if (!srcdir.exists()){  
            throw new RuntimeException(path + "不存在!");    
        }   
            
        Project project = new Project();    
        Zip zip = new Zip();    
        zip.setProject(project);    
        zip.setDestFile(zipFile);    
        FileSet fileSet = new FileSet();    
        fileSet.setProject(project);    
        fileSet.setDir(srcdir);    
        //fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");    
        //fileSet.setExcludes(...); //排除哪些文件或文件夹    
        zip.addFileset(fileSet);    
        zip.execute();    
    }    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        try {
//            String fileName = "44444444444_关于非信政测试项目的事前报告.txt";
//            fileName=new String(fileName.getBytes(),"utf-8");
            String path = "D://home/weblogic/attach/temp";
//            List<String> fileList = new ArrayList<String>();
//            fileList.add(fileName);
//            fileList.add("44444444444_关于未付项目的事前报告.txt");
//            ZipUtils.genZipFile(fileList, "test111.zip", path);
            
            ZipUtils.genZipFile( path+ "/" +"test222.zip", path);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
 

猜你喜欢

转载自blog.csdn.net/YINZONGCHAO/article/details/81780995