文件导出ZIP压缩
@Override
public String downloadallfiles(HttpServletRequest request, String lsbpId, String bsTitle) {
JwtUserObject jwtUserObject = JwtUtil.getJwtUserObj(request.getHeader("Authorization"));
String userid = jwtUserObject.getUSERID();
String username = jwtUserObject.getUSERNAME();
String itemcode = jwtUserObject.getITEMCODE();
String orgcode = jwtUserObject.getORGCODE();
String useridentity = jwtUserObject.getUSERIDENTITY();
List<FileObject> list = benbuMapper.findCzFileZipById(lsbpId);
if (list!=null && list.size() > 0) {
String path = sso.getPresignedObjectUrlByBucketNameAndPath(list.get(0).getBucketname(), list.get(0).getFilepath());
return path;
}
File file1 = new File(tempfile);
if (!file1.exists()) {
file1.mkdirs();
} else {
deleteFile(file1);
if (flag == 1) {
System.out.println("文件删除成功!");
}
file1.mkdirs();
}
List<HashMap<String, String>> files = benbuMapper.findCzFileById(lsbpId);
if (files != null && files.size() > 0) {
for (HashMap map : files) {
File file2 = new File(tempfile + "/" + map.get("itemCode"));
if (!file2.exists()) {
file2.mkdirs();
}
sso.downLoadFileByid((String) map.get("fileId"), tempfile + "/" + map.get("itemCode"));
}
}
FileZipUtil.compressToZip(tempfile, zipDowload, bsTitle + ".zip");
FileItem fileItem = null;
MultipartFile file = getMultipartFile(bsTitle);
FileObject fileObject2 = sso.uploadFileCommondLs(file, userid, username, itemcode, orgcode, "10005", useridentity, lsbpId,
itemcode.toLowerCase(), FileTypeConstant.COMP_10 + "/10005/", "");
String path = sso.getPresignedObjectUrlByBucketNameAndPathLs(fileObject2.getBucketname(), fileObject2.getFilepath());
deleteFile(new File(zipDowload));
return path;
}
文件压缩工具类
public class FileZipUtil {
public static String compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
File sourceFile = new File(sourceFilePath);
File zipPath = new File(zipFilePath);
if (!zipPath.exists()) {
zipPath.mkdirs();
}
File zipFile = new File(zipPath + File.separator + zipFilename);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
writeZip(sourceFile, "", zos);
return "压缩完成";
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
public static void writeZip(File file, String parentPath, ZipOutputStream zos) throws IOException {
if (file.isDirectory()) {
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, len);
zos.flush();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
}
优化
@NotNull
private static String zipPersonFace(List<PersonVo> personList) {
String compressedPath = FilePathConfig.gettempfilepath() + "人脸照片.zip";
log.info("使用Buffer流压缩。。。。。。。。。。");
long beginTime = System.currentTimeMillis();
try {
FileOutputStream fos = new FileOutputStream(compressedPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zipOut = new ZipOutputStream(bos);
zipOut.setLevel(Deflater.BEST_SPEED);
for (PersonVo personVo : personList) {
zipFaceByUrl(personVo, zipOut);
}
zipOut.close();
bos.close();
fos.close();
long endTime = System.currentTimeMillis();
log.info("导出用时:" + (endTime - beginTime));
} catch (IOException e) {
log.error("导出失败..." + e);
throw new BusinessException(ResultCode.OPERATION_FAILURE, "导出失败..." + e);
}
return FilePathConfig.DOWNLOAD_URL + "人脸照片.zip";
}
private static void zipFaceByUrl(PersonVo personVo, ZipOutputStream zipOut) throws IOException {
String faceUrl = personVo.getFaceUrl();
if (StrUtil.isEmpty(faceUrl) || !faceUrl.startsWith("http")) {
return;
}
URL url = new URL(faceUrl);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
String suffix;
if (faceUrl.contains("?")) {
suffix = faceUrl.substring(faceUrl.lastIndexOf("."), faceUrl.indexOf("?"));
} else {
suffix = faceUrl.substring(faceUrl.lastIndexOf("."));
}
String fileName = personVo.getDepartmentName() + "_" + personVo.getName() + "(" + personVo.getCode() + ")" + suffix;
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
zipOut.write(buffer, 0, bytesRead);
}
zipOut.closeEntry();
bis.close();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!