package com.yunyan.common.utils.file;

import com.yunyan.common.exception.ServiceException;
import com.yunyan.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class ZipFileUtils {

    /**
     * web下载打成压缩包的文件--流方式
     *
     * @param response 响应
     * @param fileList 文件列表
     * @param zipName  压缩包名
     */
    public static void  downloadZipFiles(HttpServletResponse response, List<String> fileList, String zipName) throws IOException {
        ZipOutputStream zipOutputStream = null;
        //try {
            //设置响应头
            response.reset();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            //设置文件名称
            response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
            zipOutputStream = new ZipOutputStream(response.getOutputStream());
            for (String file : fileList) {
                toZip(zipOutputStream, new File(file), "");
            }
        /*} catch (Exception e) {
            log.error("文件压缩异常: [{}], [{}]", e.getMessage(), e);
            throw new ServiceException("文件压缩打包异常, 请检查后再测~");
        } finally {
            //关闭资源
            if (null != zipOutputStream) {
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    log.error("文件IO异常: [{}], [{}]", e.getMessage(), e);
                }
            }
        }*/
    }

    /**
     * 压缩文件
     *
     * @param zipOutputStream 压缩文件流
     * @param file            待压缩文件
     */
    public static boolean toZip(ZipOutputStream zipOutputStream, File file, String filename) {
        boolean result = false;
        if(StringUtils.isBlank(filename)) {
            filename = file.getName();
        } else {
            filename += "." + FileTypeUtils.getFileType(file);
        }
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
            //设置压缩包内文件的名称
            zipOutputStream.putNextEntry(new ZipEntry(filename));
            int size;
            byte[] buffer = new byte[4096];
            while ((size = bis.read(buffer)) > 0) {
                zipOutputStream.write(buffer, 0, size);
            }
            zipOutputStream.closeEntry();
            result = true;
        } catch (Exception e) {
            log.error("文件压缩异常: [{}], [{}]", e.getMessage(), e);
            throw new ServiceException("文件压缩打包异常~");
            //throw new RuntimeException(e);
        } finally {
            //关闭资源
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    log.error("文件IO异常: [{}], [{}]", e.getMessage(), e);
                }
            }
        }

        return result;
    }
}
