package com.dxhy.sign.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author dxhy
 */
@Slf4j
public class ZipUtils {

    /** 定义递归次数变量 */
    private static int k = 1;

    public static void zip(String zipFileName, File inputFile) throws Exception {
        log.info("压缩中...");
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        BufferedOutputStream bo = new BufferedOutputStream(out);
        zip(out, inputFile, inputFile.getName(), bo);
        bo.close();
        // 输出流关闭
        out.close();
        log.info("压缩完成");
    }

    private static void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception { // 方法重载
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            if (fl.length == 0) {
                // 创建zip压缩进入点base
                out.putNextEntry(new ZipEntry(base + "/"));
                log.info(base + "/");
            }
            for (File file : fl) {
                // 递归遍历子文件夹
                zip(out, file, base + "/" + file.getName(), bo);
            }
            log.info("第" + k + "次递归");
            k++;
        } else {
            // 创建zip压缩进入点base
            out.putNextEntry(new ZipEntry(base));
            log.info(base);
            FileInputStream in = new FileInputStream(f);
            BufferedInputStream bi = new BufferedInputStream(in);
            int b;
            while ((b = bi.read()) != -1) {
                // 将字节流写入当前zip目录
                bo.write(b);
            }
            bi.close();
            // 输入流关闭
            in.close();
        }
    }

    /**
     * 解压缩
     *
     * @param inputPathZipName
     *            压缩包路径加文件名
     * @param outPath
     *            解压后文件路径
     * @throws IOException
     */
    public static void jyZip(String inputPathZipName, String outPath) throws IOException {
        long startTime = System.currentTimeMillis();
        // 输入源zip路径
        ZipInputStream zin = new ZipInputStream(new FileInputStream(inputPathZipName));
        BufferedInputStream bin = new BufferedInputStream(zin);
        File fout = null;
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null && !entry.isDirectory()) {
            fout = new File(outPath, entry.getName());
            if (!fout.exists()) {
                (new File(fout.getParent())).mkdirs();
            }
            FileOutputStream out = new FileOutputStream(fout);
            BufferedOutputStream bout = new BufferedOutputStream(out);
            int b;
            while ((b = bin.read()) != -1) {
                bout.write(b);
            }
            bout.close();
            out.close();
            log.info(fout + "解压成功");
        }
        bin.close();
        zin.close();
        long endTime = System.currentTimeMillis();
        log.info("耗费时间： " + (endTime - startTime) + " ms");
    }

    /**
     * 读取压缩文件
     *
     * @param file
     *            文件路径+文件名
     * @return 解压后的二进制输出流
     * @throws Exception
     */
    public static byte[] readZipFile(String file) throws Exception {
        // 输入源zip路径
        ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
        BufferedInputStream bin = new BufferedInputStream(zin);
        ByteArrayOutputStream bos = null;
        byte[] byteArray = null;
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null && !entry.isDirectory()) {
            bos = new ByteArrayOutputStream();
            int b;
            while ((b = bin.read()) != -1) {
                bos.write(b);
                bos.close();
            }
        }
        bin.close();
        zin.close();
        byteArray = bos.toByteArray();
        return byteArray;
    }

    public static byte[] readInputStream(InputStream inputStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toByteArray();
    }

    public static void main(String[] args) throws IOException {
        String a = "H4sIAAAAAAAAAO2TUU/TUBTHv8t9JGBuu43RvZmY+AIPPhLDy6jlWltSVrWlZC+YDRDChoIGISzDqSExBHGRAeKn6e3Wb+G9vd3c1Bp9bDztQ+85vefcc//n/FZQsUhQYfGJYYwj1zRGDHO+byypnmdrlnW39FBFhfsrSJ93XVRYQZpFTFRAsizlZPzjkbLZDBpHnqdZd/j/eE3i9SOrtMSjsCxP4NwEzjMfy+5Fe7HErMfqsm24kS1jZtuaweKZNcvDdcIMdVk14pyiyIF7+NgRZ/9819YW7ZLNL4sUScphVsnMbXxvOi/j/IzYwC+P6FaFttq0s9ptHIdrtbBVD9+ddk+b3Xo1ONwI9+u08pHWOvSiTW/e0+ZusLlB16vCydMQTfWG0/idw+D8M926Golofeg1X0tTtHYhNgY7J/Rslza2/ast+ml1mlUX/dyrhi+vWNoF86fyueJSVpKz8mRmVmwQ5VcrvRf7tNbyvz6n6yf09MjvvAkPqvTguLfa9K9vhi8h4kS9/xhHdP0BC5Oi3t/COHLZ3KUMHLpN9GjPqEt1+XlvL4PNb35nLTz40t1+5V83aOVZsNfm/ZvnaQYBmh01VlHEFLFlcL4eXtaYWfTi5pqmS2x+FCqzOhzXHUztn6fVJVHQ78eTZeWK9jYvg6NDPpCW5ujcE5y1hYc4hM2soznRxjHhHxsKIJpjszkmxaIZCcFe1jv+jR/egYWoCrZSHfHVLJ2l7c84t1S9b9nGUyGOEocPax67fulMpCiPL8+Vx/+G5CyQDCSnhuQskJyoTQ5IBpJTQ3IOSE7UZhJIBpJTQ/IkkJyoDQaSgeTUkIyB5ERtJCAZSE4NyRKQnKiNDCQDyakhWQaSE7XJA8lAcmpIzgPJidpMAclAcmpIngKSE7VRgGQgOTUkK0BykjYZIBlITg3Jmf+U5Lnyd3ufDZnlKQAA";
        jyZip(a, "E://hhh.txt");
    }

}
