package thyyxxk.webserver.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @description: 压缩与解压缩
 * @author: DingJie
 * @create: 2021/7/2820:45
 */
@Slf4j
public class ZipUtil {
    public static String DIRECTORY;

    public static void setDirectory(String directory) {
        DIRECTORY = directory;
    }

    /**
     * zip压缩
     * */
    public static String zip(String filename) throws IOException {
        //压缩文件名称与位置
        OutputStream outputStream = new FileOutputStream(filename + ".zip");
        //获取压缩文件输出流
        ZipOutputStream out = new ZipOutputStream(outputStream);
        //被压缩的文件
        FileInputStream fis = new FileInputStream(filename);
        out.putNextEntry(new ZipEntry(filename));
        int len;
        byte[] buffer = new byte[1024];
        // 读入需要下载的文件的内容，打包到zip文件
        while ((len = fis.read(buffer)) > 0) {
            //写入压缩文件输出流中(压缩文件zip中)
            out.write(buffer, 0, len);
        }
        out.closeEntry();
        fis.close();
        out.close();
        outputStream.close();
        return filename + ".zip";
    }

    /**
     * zip解压
     */
    public static String unZip(String filename) throws IOException {
        String unZipPath = "";
        //读取压缩文件
        ZipInputStream in = new ZipInputStream(Files.newInputStream(Paths.get(filename)));
        //zip文件实体类
        ZipEntry entry;
        //遍历压缩文件内部 文件数量
        while((entry = in.getNextEntry()) != null){
            if(!entry.isDirectory()){
                //文件输出流
                unZipPath = DIRECTORY + entry.getName();
                FileOutputStream out = new FileOutputStream(unZipPath);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                int len;
                byte[] buf = new byte[1024];
                while ((len = in.read(buf)) != -1) {
                    bos.write(buf, 0, len);
                }
                // 关流顺序，先打开的后关闭
                bos.close();
                out.close();
            }
        }
        in.close();
        return unZipPath;
    }

    public static String deleteFile(String filename) {
        File file = new File(filename);
        if (file.exists()) {
            if (file.delete()) {
                return "删除文件成功：" + filename;
            }
            return "删除文件失败：" + filename;
        }
        return "文件不存在：" + filename;
    }

    public static byte[] zipFileToBytes(String filename) {
        byte[] buffer = null;
        try {
            File file = new File(filename);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (IOException e) {
            log.error("获取压缩文件字节流出错", e);
        }
        return buffer;
    }
}
