package com.ruoyi.common.utils.zip;

import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

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

/**
 * 压缩的工具类
 */
@Slf4j
public class ZipFileUtils {
    public static void main(String[] args) {

        String zipFilePath = "C:\\gnhz\\uploadPath\\download\\批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip";
        List<String> fileNamePathList = new ArrayList<>();
        fileNamePathList.add("C:\\gnhz\\uploadPath\\download\\EHR导出岗位人员关系表-差异对比.xlsx");
        fileNamePathList.add("C:\\gnhz\\uploadPath\\download\\EHR中不存在的岗检系统人员_20240312112506-543164.xlsx");

        new ZipFileUtils().createZipFile(fileNamePathList, zipFilePath);
    }

    /**
     * 创建压缩包，仅支持zip格式
     *
     * @param fileNamePathList 待文雅文件-含文件名：绝对磁盘路径
     * @param zipFilePath      压缩的存放路径-含文件名：绝对磁盘路径
     */
    public void createZipFile(List<String> fileNamePathList, String zipFilePath) {

        // 压缩文件的输出路径
//        String zipFilePath = "C:\\gnhz\\uploadPath\\download\\批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip";

        if (CollectionUtils.isEmpty(fileNamePathList))
            return;

        ZipOutputStream zipOutputStream = null;

        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        for (String fileNamePath : fileNamePathList) {
            // 要压缩的文件路径
            File filePath = new File(fileNamePath);//new File("C:\\gnhz\\uploadPath\\download\\EHR中不存在的岗检系统人员_20240312112506-543164.xlsx");
            FileInputStream inputStream = null;
            try {
                // 创建文件输入流
                inputStream = new FileInputStream(filePath);
                // 创建ZipOutputStream输出流


                // 创建ZipEntry
                ZipEntry zipEntry = new ZipEntry(filePath.getName());
                // 添加ZipEntry到ZipOutputStream中
                zipOutputStream.putNextEntry(zipEntry);

                // 读取文件内容并写入到ZipOutputStream中
                byte[] buffer = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, length);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    // 关闭当前ZipEntry
                    if (zipOutputStream != null) {
                        zipOutputStream.closeEntry();
                    }
                    if (zipOutputStream != null) {
                        zipOutputStream.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

        try {
            // 关闭当前ZipEntry
            if (zipOutputStream != null) {
                zipOutputStream.closeEntry();
            }
            if (zipOutputStream != null) {
                zipOutputStream.flush();
                zipOutputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("===========================压缩结束===============");
    }
}
