Java创建zip文件

package com.xxx.card.factory;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrBuilder;
import cn.quickercloud.common.util.EasyPoiUtil;
import com.xxx.common.core.base.constant.CardConstant;
import com.xxx.common.core.base.util.GenerateCodeUtils;
import com.xxx.component.CustomParamConfig;
import com.xxx.coupon.api.model.vo.CardNumExcelVO;
import com.xxx.coupon.api.model.vo.CpmMerchantCardStockCardInfoDownloadVO;
import com.xxx.coupon.api.model.vo.CpmMerchantCardStockDownloadVO;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author JZDELD
 * @date 2022/7/4 17:01
 */
public class CpmGiftCardStockFactory {
    public static byte[] createFile(CpmMerchantCardStockDownloadVO downloadVO) throws IOException {
        List<CpmMerchantCardStockCardInfoDownloadVO> cardInfoDownloadList = downloadVO.getCardInfoDownloadList();

        // 总的zip
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);

        if (cardInfoDownloadList.size() == 1) {
            CpmMerchantCardStockCardInfoDownloadVO cardInfoDownload = cardInfoDownloadList.get(0);

            String cardTemplateId = cardInfoDownload.getCardTemplateId();
            List<CardNumExcelVO> cardNumExcelList = cardInfoDownload.getCardNumList();

            if (CollUtil.isEmpty(cardNumExcelList)) {
                zipOutputStream.putNextEntry(new ZipEntry(String.format("卡模板:%s(空).txt", cardTemplateId)));
                zipOutputStream.write("".getBytes(StandardCharsets.UTF_8));
                zipOutputStream.closeEntry();

                byteArrayOutputStream.close();

                return byteArrayOutputStream.toByteArray();
            }

            // joinZip
            joinZip(zipOutputStream, cardNumExcelList);

            // 总的zip ZipOutputStream close
            zipOutputStream.flush();
            //解决CRS校验失败问题
            zipOutputStream.finish();
            zipOutputStream.close();
            byteArrayOutputStream.close();

            return byteArrayOutputStream.toByteArray();
        }

        for (CpmMerchantCardStockCardInfoDownloadVO cardInfoDownload : cardInfoDownloadList) {
            String cardTemplateId = cardInfoDownload.getCardTemplateId();
            List<CardNumExcelVO> cardNumExcelList = cardInfoDownload.getCardNumList();

            if (CollUtil.isEmpty(cardNumExcelList)) {
                zipOutputStream.putNextEntry(new ZipEntry(String.format("卡模板:%s(空).txt", cardTemplateId)));
                zipOutputStream.write("".getBytes(StandardCharsets.UTF_8));
                zipOutputStream.closeEntry();
                continue;
            }

            // 每个卡模板的小zip
            ByteArrayOutputStream childByteArrayOutputStream = new ByteArrayOutputStream(1024);
            ZipOutputStream childZipOutputStream = new ZipOutputStream(childByteArrayOutputStream);

            // joinZip
            joinZip(childZipOutputStream, cardNumExcelList);

            // 每个卡模板的小zip ZipOutputStream close
            childZipOutputStream.flush();
            childZipOutputStream.finish();
            childZipOutputStream.close();
            childByteArrayOutputStream.close();

            zipOutputStream.putNextEntry(new ZipEntry(String.format("卡模板:%s.zip", cardTemplateId)));
            zipOutputStream.write(childByteArrayOutputStream.toByteArray());
            zipOutputStream.closeEntry();
        }

        // 总的zip ZipOutputStream close
        zipOutputStream.flush();
        //解决CRS校验失败问题
        zipOutputStream.finish();
        zipOutputStream.close();
        byteArrayOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 将二维码zip 卡号.xlsx 加入 zip
     *
     * @param zipOutputStream
     * @param cardNumExcelList
     * @throws IOException
     */
    private static void joinZip(ZipOutputStream zipOutputStream, List<CardNumExcelVO> cardNumExcelList) throws IOException {
        // 二维码zip
        ByteArrayOutputStream qrcodeImgByteArrayOutputStream = getQrcodeImgByteArrayOutputStream(cardNumExcelList);

        // 将 二维码.zip 放入 zip
        zipOutputStream.putNextEntry(new ZipEntry("二维码.zip"));
        zipOutputStream.write(qrcodeImgByteArrayOutputStream.toByteArray());
        zipOutputStream.closeEntry();

        // 将 卡号.xlsx 放入 zip
        zipOutputStream.putNextEntry(new ZipEntry("卡号.xlsx"));
        zipOutputStream.write(EasyPoiUtil.createExcelByFile(CardNumExcelVO.class, cardNumExcelList));
        zipOutputStream.closeEntry();
    }

    /**
     * 获取 二维码.zip 的 ByteArrayOutputStream
     *
     * @param cardNumExcelList
     * @return
     * @throws IOException
     */
    private static ByteArrayOutputStream getQrcodeImgByteArrayOutputStream(List<CardNumExcelVO> cardNumExcelList) throws IOException {
        // 二维码zip
        ByteArrayOutputStream qrcodeImgByteArrayOutputStream = new ByteArrayOutputStream(1024);
        ZipOutputStream qrcodeImgZipOutputStream = new ZipOutputStream(qrcodeImgByteArrayOutputStream);
        for (CardNumExcelVO cardNumExcelVO : cardNumExcelList) {
            String sysCardSerialNum = new StrBuilder()
                    .append(CustomParamConfig.PAPER_COUPON_URL)
                    .append("?")
                    .append(CardConstant.PAPER_COUPON_URL_PARAM_NAME)
                    .append("=")
                    .append(cardNumExcelVO.getSysCardSerialNum())
                    .toString();

            String cardNum = cardNumExcelVO.getCardNum();
            byte[] qrcode = GenerateCodeUtils.getByteQrcode(sysCardSerialNum);
            //创建压缩包里的文件
            qrcodeImgZipOutputStream.putNextEntry(new ZipEntry(cardNum + ".png"));
            qrcodeImgZipOutputStream.write(qrcode);
            qrcodeImgZipOutputStream.closeEntry();
        }
        // 二维码zip ZipOutputStream close
        qrcodeImgZipOutputStream.flush();
        qrcodeImgZipOutputStream.finish();
        qrcodeImgZipOutputStream.close();
        qrcodeImgByteArrayOutputStream.close();

        return qrcodeImgByteArrayOutputStream;
    }
}

 

posted @ 2022-08-10 11:04  JZDELD  阅读(338)  评论(1编辑  收藏  举报