package com.kingdee.shr.cmpdesign.web.handler.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Title: AdvancedZipUtils
 * @Author ÇàÎà
 * @Package com.kingdee.shr.cmpdesign.web.handler.utils
 * @Date 2026/1/15 18:24
 * @description:
 */
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);
            Throwable var7 = null;

            try {
                zos.setLevel(compressionLevel);
                zos.setComment("Created by Java Zip Utils");
                ZipEntry entry = new ZipEntry(fileName);
                zos.putNextEntry(entry);
                zos.write(fileData);
                zos.closeEntry();
            } catch (Throwable var32) {
                var7 = var32;
                throw var32;
            } finally {
                if (zos != null) {
                    if (var7 != null) {
                        try {
                            zos.close();
                        } catch (Throwable var31) {
                            var7.addSuppressed(var31);
                        }
                    } else {
                        zos.close();
                    }
                }

            }
        } 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);
            Throwable var6 = null;

            try {
                ZipEntry entry = new ZipEntry(filePathInZip);
                zos.putNextEntry(entry);
                zos.write(fileData);
                zos.closeEntry();
            } catch (Throwable var31) {
                var6 = var31;
                throw var31;
            } finally {
                if (zos != null) {
                    if (var6 != null) {
                        try {
                            zos.close();
                        } catch (Throwable var30) {
                            var6.addSuppressed(var30);
                        }
                    } else {
                        zos.close();
                    }
                }

            }
        } 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);
            Throwable var5 = null;

            try {
                for(Map.Entry<String, byte[]> file : files.entrySet()) {
                    ZipEntry entry = new ZipEntry((String)file.getKey());
                    zos.putNextEntry(entry);
                    zos.write((byte[])file.getValue());
                    zos.closeEntry();
                }
            } catch (Throwable var32) {
                var5 = var32;
                throw var32;
            } finally {
                if (zos != null) {
                    if (var5 != null) {
                        try {
                            zos.close();
                        } catch (Throwable var31) {
                            var5.addSuppressed(var31);
                        }
                    } else {
                        zos.close();
                    }
                }

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

    }
}
