package com.cm.common.utils;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @ClassName: ZipUtil
 * @Description: 压缩
 * @Author: WangGeng
 * @Date: 2019/4/28 11:24 AM
 * @Version: 1.0
 **/
public class ZipUtil {

    /**
     * 压缩文件
     *
     * @param inputFileName
     * @param zipFileName
     * @return
     * @throws Exception
     */
    public static Boolean zip(String inputFileName, String zipFileName) throws Exception {
        zip(zipFileName, new File(inputFileName));
        return true;
    }

    private static void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFile, "");
        out.flush();
        out.close();
    }

    private static void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
        }
    }

    /**
     * 解压 zip 文件
     *
     * @param zipFile  zip 压缩文件
     * @param destDir  zip 压缩文件解压后保存的目录
     * @param encoding zip 文件的编码
     * @return 返回 zip 压缩文件里的文件名的 list
     * @throws IOException
     */
    public static List<String> unZip(File zipFile, String destDir, String encoding) throws IOException {
        // 如果 destDir 为 null, 空字符串, 或者全是空格, 则解压到压缩文件所在目录
        if (destDir == null || destDir.length() == 0) {
            destDir = zipFile.getParent();
        }
        destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator;
        ZipArchiveInputStream is = null;
        List<String> fileNames = new ArrayList<String>();
        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile), 1024), encoding);
            ZipArchiveEntry entry = null;
            while ((entry = is.getNextZipEntry()) != null) {
                fileNames.add(entry.getName());
                File file = new File(destDir, entry.getName());
                if (entry.isDirectory()) {
                    // 创建文件夹，如果中间有路径会自动创建
                    FileUtils.forceMkdir(file);
                } else {
                    OutputStream os = null;
                    try {
                        FileUtils.touch(file);
                        os = new FileOutputStream(new File(destDir, entry.getName()));
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            IOUtils.closeQuietly(is);
        }
        return fileNames;
    }

    /**
     * 解压 zip 文件
     *
     * @param zipFile  zip 压缩文件的路径
     * @param destDir  zip 压缩文件解压后保存的目录
     * @param encoding zip 文件的编码
     * @return 返回 zip 压缩文件里的文件名的 list
     * @throws Exception
     */
    public static List<String> unZip(String zipFile, String destDir, String encoding) throws IOException {
        File zipfile = new File(zipFile);
        return unZip(zipfile, destDir, encoding);
    }

    public static List<String> unZip(String zipFile, String destDir) throws IOException {
        return unZip(zipFile, destDir, "UTF-8");
    }

}
