package com.qmth.exam.reserve.util;

import com.qmth.boot.core.exception.StatusException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * zip文件 解压帮助类
 */
public class ZipUtil {

    private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);

    public static void unzip(File file, String targetFileDir) {
        ZipFile zipFile;
        byte[] buf = new byte[1024 * 1024];
        int readSize;
        ZipInputStream zis = null;
        FileOutputStream fos = null;

        try {
            zipFile = new ZipFile(file);
            zipFile.close();

            File newDir = new File(targetFileDir);
            if (!newDir.exists()) {
                boolean mkdirFlag = newDir.mkdirs();
                if (!mkdirFlag) {
                    log.warn("创建目录失败, targetFileDir:{}", targetFileDir);
                }
            }

            zis = new ZipInputStream(Files.newInputStream(file.toPath()), Charset.forName("GBK"));
            ZipEntry zipEntry = zis.getNextEntry();
            while (null != zipEntry) {
                String zipEntryName = zipEntry.getName().replace('\\', '/');
                if (zipEntry.isDirectory()) {
                    int indexNumber = zipEntryName.lastIndexOf('/');
                    File entryDirs = new File(targetFileDir + "/" + zipEntryName.substring(0, indexNumber));
                    boolean mkdirFlag = entryDirs.mkdirs();
                    if (!mkdirFlag) {
                        log.warn("创建目录失败, entryDirs:{}", targetFileDir + "/" + zipEntryName.substring(0, indexNumber));
                    }
                } else {
                    try {
                        fos = new FileOutputStream(targetFileDir + "/" + zipEntryName);
                        while ((readSize = zis.read(buf, 0, 1024 * 1024)) != -1) {
                            fos.write(buf, 0, readSize);
                        }
                    } catch (Exception e) {
                        log.error("解压文件失败:{}", e.getMessage());
                        throw new StatusException("解压失败");
                    } finally {
                        try {
                            if (null != fos) {
                                fos.close();
                            }
                        } catch (IOException e) {
                            log.error("解压文件失败:{}", e.getMessage());
                        }
                    }
                }
                zipEntry = zis.getNextEntry();
            }
        } catch (IOException e) {
            log.error("解压文件失败:{}", e.getMessage());
            throw new StatusException("解压失败");
        } finally {
            try {
                if (null != zis) {
                    zis.close();
                }
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                log.error("解压文件失败:{}", e.getMessage());
            }
        }
    }

}
