package com.dxhy.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author http://javaflex.iteye.com/
 *
 */
@Slf4j
public class FileZip {

    /**
     * 
     * @param srcfile
     *            文件名数组
     * @param zipfile
     *            压缩后文件
     */
    public static void zipFiles(java.io.File[] srcfile, java.io.File zipfile) {
        byte[] buf = new byte[1024];
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            for (java.io.File file : srcfile) {
                FileInputStream in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (IOException e) {
            log.debug("压缩文件异常,异常信息为" + e);
        }
    }

    /**
     * 
     * @param srcfile
     *            文件名
     * @param zipfile
     *            压缩后文件
     */
    public static void zipFiles(java.io.File srcfile, java.io.File zipfile) {

        byte[] buf = new byte[1024];
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            FileInputStream in = new FileInputStream(srcfile);
            out.putNextEntry(new ZipEntry(srcfile.getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
            out.close();
        } catch (IOException e) {
            log.debug("压缩文件异常,异常信息为" + e);
        }
    }
}
