若依批量解压ZIP包返回文件流

若依批量解压ZIP包

Controller 类
/**
* 压缩文件 返回 文件流
* NqiReportPlan 是我本地的对象 根基对象的参数去查询数据
*/

@GetMapping("/batchGenCode")
public void batchGenCode(@Validated @RequestBody NqiReportPlan nqiReportPlan,HttpServletResponse response, String tables) throws IOException
{
    //String[] tableNames = Convert.toStrArray(tables);
    byte[] data = nqiReportPlanService.downloadCodeRuoyi(nqiReportPlan);
    genCode(response, data);
}

/**
* 生成zip文件
*/

private void genCode(HttpServletResponse response, byte[] data) throws IOException
{
    response.reset();
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
    //filename  是zip包名称
    response.setHeader("Content-Disposition", "attachment; filename=\"sehenbao.zip\"");
    response.addHeader("Content-Length", "" + data.length);
    response.setContentType("application/octet-stream; charset=UTF-8");
    IOUtils.write(data, response.getOutputStream());
}

Service 层

    public byte[] downloadCodeRuoyi(NqiReportPlan nqiReportPlan) throws IOException;

ServiceImpl层
/*
*NqiReportPlan 是我本地的对象 根基对象去查询数据
*
/
@Override
public byte[] downloadCodeRuoyi(NqiReportPlan nqiReportPlan) throws IOException {
//你们自己的业务逻辑
//List list = nqiReportPlanMapper.selectNqiReportPlanList(nqiReportPlan);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);
    //循环文件 开始压缩
    for (NqiReportPlan item : list)
    {
        //我的文件地址在数据库中是JSON数据,你们可以直接切换成自己的 地址字段就行
        //我的数据 例子  [{
				"msg": "操作成功",
				"fileName": "/profile/upload/2023/03/23/1.xlsx",
				"fileNewSize": "15.79K",
				"code": 200,
				"newFileName": "20230323095034A001.xlsx",
				"fileUrl": "/upload/2023/03/23/1.xlsx",
				"url": "http://192.168.3.90:8098/profile/upload/2023/03/23/1.xlsx",
				"originalFilename": "1.xlsx"
			}]
		//如果是单纯的一个字段 的话直接去字段就行 则可以忽略以下的JSON转换	
        
        String planUrl = item.getPlanUrl();
        JSONArray json = JSON.parseArray(planUrl);
        if(StringUtils.isNotNull(json) && json.size() > 0){
            String  urlString = json.get(0).toString();
            JSONObject object = JSONObject.parseObject(urlString);
            generatorCode(RuoYiConfig.getProfile() + object.getString("fileUrl"), zip);
        }
		//单个字段地址 
		//PlanUrl =  "/upload/2023/03/23/1.xlsx";
		//RuoYiConfig.getProfile()  是配置文application-dev.yml件中文件地址
			
			String planUrl = item.getPlanUrl();
		  generatorCode(RuoYiConfig.getProfile() + planUrl , zip);
		
    }
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
}

/**
* 根据生成zip
* fileUrl 文件所在的地址 比如C:/ruoyi/uploadPath/1.png
*/

private void generatorCode(String fileUrl, ZipOutputStream zip) throws IOException {
    File file = new File(fileUrl);
    //取出文件名
    String name = file.getName();
    //  定义输入文件流
    InputStream  input = new FileInputStream(file);
    // 添加到zip
    zip.putNextEntry(new ZipEntry(name));
    int readsize = 1024 * 1024 * 5;
    byte[] buf = new byte[readsize];
    int temp = 0;
    while ((temp = input.read(buf)) != -1) {
        zip.write(buf, 0, temp);
    }
    zip.flush();
    zip.closeEntry();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值