若依批量解压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();
}