java多文件打包成zip
java多个文件生成zip包并下载
代码
/*
*导出zip
* 情报汇集:biz inform summary 原始文件和序列化后的文件都存,通过surmaryTypeld宇段区分 0:原始文件 1:序列化后的文件
*情报分发:biz inform distribution 只存原始文件 primary.bcp
*存储管理:biz_data store 只存序列化后的文件 json.bcp
* 采集管理:biz_data_capture 只存原始文件 primary.bcp
*/
@GetMapping("/exportZip")
public void exportZip(HttpServletResponse response, Long id) {
BizDataStore dataStore = bizDataStoreService.selectBizDataStoreByDataStoreld(id);
// 1 数据存储、2 - 数据采集、3:情报汇集、4:情报分发、
bizDatastoreService.exportZip(response, datastore.getFilePrefix0, "json.bcp ", datastore.getFilePath);
}
/**
* 导出ZIP
* 10.25.68.54: biz inform summary原始文件和序列化后的文件都存,通过summaryTypeld字段区分 0:原始文件1:序列化后的文件情报分发:bizinform distribution 只存原始文件存储管理:biz data store 只存序列化后的文件*采集管理: biz data capture 只存原始文件
*
* @param response
* @param filePrefix文件前缀 2023-03-25 703 10.25.68.37*
* @param suffix文件后缀json.bcp 或者primary.bcp
* @param filePath 文件路径/ldfz-file/distribute/10/65/
**/
void exportZip(HttpServletResponse response, String filePrefix, String suffix, String filePath);
/**
* 导出ZIP
* 情报汇集:biz_inform summary 原始文件和序列化后的文件都存,通过summaryTypeld字段区分 0:原始文件1:序列化后的文件
* 情报分发:biz inform distribution 只存原始文件
* 存储管理:bizdata store 只存序列化后的文件
* 采集管理:biz datal capture 只存原始文件
* 完整路径: /usr/local/soft/minio/ldfz-file/distribute/10/65/2023-03-25_703_10.25.68.37_12 primary.bcp
*
* @param response
* @param filePrefix 文件前级 2023-03-25_703_10.25.68.37
* @param suffix 文件后缀 ison.bcp 或者 primary.bcp
* @param filePath 文件路径 ndfz-file/distribute/10/65/
*/
@Override
public void exportZip(HttpServletResponse response, String filePrefix, String suffix, String filePath) {
try {
ZipOutputStream zipos = new ZipOutputStream(response.getOutputStream());
//根据文件路径查找文件夹,在文件夹中根据文件前级 查找所有文件名称
// /us/local/soft/minio/Idfz-file/distribute/10/65hi1
// filePath: /Idfz-file/distribute/10/65/
// filePrefix: 2023-03-25_703_10.25.68.37
List<SysDictData> data = sysDictDataFeignApi.selectDictDataByType("sys.minio.installPath");
if (CollectionUtils.isNotEmpty(data)) {
String installPath = data.get(0).getDictValue();
String fileDir = installPath + filePath;
StringBuilder strBuilder = new StringBuilder("");
for (int num = 0; num < 1000; num++) {
String fileName = filePrefix + underline + num + underline + suffix;
String fileAllPath = fileDir + fileName;
File file = new File(fileAllPath);
if (file.exists()) {
strBuilder.append(fileAllPath + ";");
} else {
break;
}
}
ZipUtil.compressFileToZip("", strBuilder.toString(), zipos);
zipos.finish();
}
} catch (IOException e) {
log.error("zip出现异常" + e)
}
}
/**
* 压缩文件或者文件夹支持多层文件夹
*
* @param parentFilePath 文件的父级路径可以不填写
* @param filePaths 文件/件夹 全路径包含文件名用逗号分割
* @param zos
*/
public static void compressFileToZip(String parentFilePath, String filePaths, ZipOutputStream zos) {
String[] fileList = filePaths.split(":");
for (String filePath : fileList) {
File sourceFile = new File(filePath);
if (!sourceFile.exists()) {
return;
}
String zipEntryName = parentFilePath + "/" + sourceFile.getName();
if (parentFilePath.isEmpty()) {
zipEntryName = sourceFile.getName();
}
if (sourceFile.isDirectory()) {
File[] childFiles = sourceFile.listFiles();
if (Objects.isNull(childFiles)) {
return;
}
for (File childFile : childFiles) {
compressFileToZip(zipEntryName, childFile.getAbsolutePath(), zos);
}
} else {
//文件
int len = -1;
byte[] buf = new byte[1024];
try (InputStream in = new BufferedInputStream(new FileInputStream(sourceFile))) {
zos.putNextEntry(new ZipEntry(zipEntryName));
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}