把字符串写入到zip文件中

该博客主要展示了如何在单元测试中使用GeneratorDomain类来生成并输出一个Zip文件。内容涉及了使用Hutool库进行文件操作,将Map内容写入ZipEntry,并处理可能的IOException。
摘要由CSDN通过智能技术生成

 

package com.xh.generator.domain.test;

import cn.hutool.core.io.IoUtil;
import com.xh.generator.domain.GeneratorDomain;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class GeneratorDomainTest {

    @Autowired
    private GeneratorDomain mysqlDomain;

    @Test
    public void outputString() {
        try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("D:/demo.zip"))) {
            Map<String, String> map = mysqlDomain.outputString("res_reminder");
            map.forEach((k, v) -> {
                try {
                    zip.putNextEntry(new ZipEntry(k));
                    IoUtil.writeUtf8(zip, false, v);
                    zip.flush();
                    zip.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旷野历程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值