package com.kingdee.eas.custom.compensation.utils;


import java.io.*;
import java.util.Map;
import java.util.zip.*;

/**
 * @Description TODO
 * @Date 2025/9/29 14:37
 * @Created by 59279
 */
public class AdvancedZipUtils {

    /**
     * 创建压缩包并设置压缩级别
     */
    public static void createZipWithCompressionLevel(byte[] fileData, String fileName,
                                                     String zipFilePath, int compressionLevel) {
        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            // 设置压缩级别 (0-9, 0=不压缩, 9=最大压缩)
            zos.setLevel(compressionLevel);

            // 设置注释
            zos.setComment("Created by Java Zip Utils");

            ZipEntry entry = new ZipEntry(fileName);
            zos.putNextEntry(entry);
            zos.write(fileData);
            zos.closeEntry();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建带目录结构的压缩包
     */
    public static void createZipWithDirectory(byte[] fileData, String filePathInZip,
                                              String zipFilePath) {
        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            ZipEntry entry = new ZipEntry(filePathInZip);
            zos.putNextEntry(entry);
            zos.write(fileData);
            zos.closeEntry();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建多个文件压缩包
     */
    public static void createZipFromMultipleFiles(Map<String, byte[]> files, String zipFilePath) {
        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            for (Map.Entry<String, byte[]> file : files.entrySet()) {
                ZipEntry entry = new ZipEntry(file.getKey());
                zos.putNextEntry(entry);
                zos.write(file.getValue());
                zos.closeEntry();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}