[java] 压缩文件(夹)为zip文件

package com.wa.dataprocess.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class AntZip {

    public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
           System.out.println("================压缩开始==================");
        byte[] buf = new byte[1024];
        try {
          // Create the ZIP file
          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
         //ZipOutputStream out = new ZipOutputStream(Response.getOutputStream());--设置成这样可以不用保存在本地,再输出, 通过response流输出。
          // Compress the files
          for (int i = 0; i < srcfile.length; i++) {
            FileInputStream in = new FileInputStream(srcfile[i]);
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(srcfile[i].getName()));
            // Transfer bytes from the file to the ZIP file
            out.setEncoding("gbk");   
  
            int len;
            while ( (len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
            }
            // Complete the entry
            out.closeEntry();
            in.close();
          }
          // Complete the ZIP file
          out.close();
          System.out.println("================压缩完成==================");
        }
        catch (IOException e) {
          e.printStackTrace();
        }
    }
    
    
     /**
     * 创建ZIP文件
     * @param sourcePath 文件或文件夹路径
     * @param zipPath 生成的zip文件存在路径(包括文件名)
     */
    public static void createZip(String sourcePath, String zipPath) {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipPath);
            zos = new ZipOutputStream(fos);
            writeZip(new File(sourcePath), "", zos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    
    private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
         byte[] buf = new byte[1024];
        if(file.exists()){
            if(file.isDirectory()){//处理文件夹
                parentPath+=file.getName()+File.separator;
                File [] files=file.listFiles();
                for(File f:files){
                    writeZip(f, parentPath, zos);
                }
            }else{
                FileInputStream in;
                try {
                    in = new FileInputStream(file);
                
                // Add ZIP entry to output stream.
                zos.putNextEntry(new ZipEntry(file.getName()));
                // Transfer bytes from the file to the ZIP file
                zos.setEncoding("gbk");   
      
                int len;
                while ( (len = in.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                // Complete the entry
                zos.closeEntry();
                in.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) throws IOException {
        
        File f = new File("D:\\tools\\apache-tomcat-6.0.18\\webapps\\InfomationServiceSystem\\WEB-INF\\classes\\template\\doc");
        
        File out = new File("d:\\考核.zip");//文件输出路径
        
        AntZip az = new AntZip();
           
        az.ZipFiles(f.listFiles(), out);//可以做打包好后把原文件删除了,我这里就不做了。

    }
}

 

posted @ 2015-02-15 09:31  snow__wolf  阅读(288)  评论(0)    收藏  举报