package com.xjrsoft.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author dzx
 * @Description 文件压缩工具
 * 2023/12/5
 */
public class FileZipUtil {
    public static ByteArrayInputStream inputStream2ByteArray(InputStream in, boolean isClose) {
        byte[] byteArray = null;
        try {
            int total = in.available();
            byteArray = new byte[total];
            in.read(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isClose) {
                try {
                    in.close();
                } catch (Exception e2) {
                    //zou.log.Logger.getServerlog().error(" ر   ʧ  ");
                }
            }
        }
        return new ByteArrayInputStream(byteArray);
    }

    public static byte[] byteArray(InputStream in, boolean isClose) {
        byte[] byteArray = null;
        try {
            int total = in.available();
            byteArray = new byte[total];
            in.read(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isClose) {
                try {
                    in.close();
                } catch (Exception e2) {
                    //zou.log.Logger.getServerlog().error(" ر   ʧ  ");
                }
            }
        }
        return byteArray;
    }

    public static void zipFiles(File[] srcfile, File zipfile) {
        byte[] buf = new byte[1024];
        try {
            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            for (int i = 0; i < srcfile.length; i++) {
                FileInputStream in = new FileInputStream(srcfile[i]);
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
        }
    }

    public static void zipFiles(List<File> fileList, File zipfile) {
        byte[] buf = new byte[1024];
        try {
            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            for(File file : fileList) {
                FileInputStream in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block

            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
        }
    }


    public static byte[] byteAryMap2Zip(Map<String,byte[]> byteAryMap) {
        byte[] buf = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
            ZipOutputStream out = new ZipOutputStream(baos);
            for(String fileName : byteAryMap.keySet()) {
                byte[] byteAry = byteAryMap.get(fileName);
                ByteArrayInputStream in = new ByteArrayInputStream(byteAry);
                out.putNextEntry(new ZipEntry(fileName));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block

            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
        }
        return baos.toByteArray();
    }

    public static boolean createFloder(File file) {
        if (!file.exists()) {
            file.mkdir();
        }
        return true;
    }
    public static boolean createFile(File file) {

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    public static String saveFile(String fileName,byte[] fileContent) {
        File file1 = new File("D:\\     ļ \\flew\\");
        File file = new File("D:\\     ļ \\flew\\"+fileName);
        FileOutputStream out = null;
        createFloder(file1);
        createFile(file);
        try {
            out = new FileOutputStream(file);
            out.write(fileContent);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file.getPath();
    }

    /**
     * 将MultipartFile转成ZipFile
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static ZipFile convertToZipFile(MultipartFile multipartFile) throws IOException {
        // 创建临时文件
        String originalFilename = multipartFile.getOriginalFilename();
        Path tempPath = Files.createTempFile("temp", originalFilename.substring(originalFilename.lastIndexOf('.')));
        multipartFile.transferTo(tempPath); // 将MultipartFile内容写入临时文件

        // 使用ZipFile读取临时文件
        ZipFile zipfile = new ZipFile(tempPath.toFile(), Charset.forName("GBK"));
        return zipfile;
    }
}
