package org.ta.pddserver.PDFTemplate;

import jakarta.servlet.http.HttpServletResponse;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public  boolean makeZip(String pdfName,Map<String, ByteArrayOutputStream> outMap, String name, HttpServletResponse response) throws IOException {
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("fileName", name);
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {

            byte[] buf = new byte[1024];
            for (String key : outMap.keySet()) {
                out.putNextEntry(new ZipEntry(pdfName+"-" + key + ".pdf"));
                byte[] data = outMap.get(key).toByteArray();
                ByteArrayInputStream in = new ByteArrayInputStream(data);
                int len;
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
                response.flushBuffer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
        return false;

    }
}
