From 2c967cef0a345594a2427e228ac5468db7ab5853 Mon Sep 17 00:00:00 2001 From: lu Date: Tue, 10 Sep 2024 17:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=AF=BC=E5=87=BA500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/course/api/CoursePortalApi.java | 98 ++++++++++++------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java b/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java index 180733fd..75d3d6a7 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java @@ -404,49 +404,79 @@ public class CoursePortalApi extends ApiBaseController{ if (totalFileSize > 2L * 1024 * 1024 * 1024) { return success("您要下载的作业过大,请分批下载或联系管理员!"); } + // 创建压缩文件 + String zipFilePath = "/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip"; + createZipFile(map, zipFilePath); + return success(zipFilePath); // 创建一个临时文件用于存储ZIP文件 - File tempZipFile = new File("/home/www/elearning/upload/temp.zip"); +// File tempZipFile = new File("/home/www/elearning/upload/temp.zip"); +// +// try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipFile))) { +// for (Map.Entry e : map.entrySet()) { +// File fileToZip = new File(e.getValue()); +// // 添加 ZIP 条目 +// ZipEntry entry = new ZipEntry(e.getKey()); +// entry.setSize(fileToZip.length()); +// +// zos.putNextEntry(entry); +// +// try (FileInputStream fis = new FileInputStream(fileToZip)) { +// byte[] buffer = new byte[4096]; +// int len; +// while ((len = fis.read(buffer)) > 0) { +// zos.write(buffer, 0, len); +// } +// } +// +// zos.closeEntry(); +// } +// } +// // 将临时文件移动到指定位置 +// Path source = tempZipFile.toPath(); +// //生成uuid +//// String uuid = UUID.randomUUID().toString(); +// Path destination = Paths.get("/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip"); +// +// // 确保目标目录存在 +// Files.createDirectories(destination.getParent()); +// +// // 删除目标文件如果已存在 +// if (Files.exists(destination)) { +// Files.delete(destination); +// } +// +// // 移动文件 +// Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); +// +// // 返回文件路径给前端 +// String filePath = destination.toAbsolutePath().toString(); +// return success(filePath); + } + private static void createZipFile(Map map, String zipFilePath) throws IOException { + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) { + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); - try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipFile))) { - for (Map.Entry e : map.entrySet()) { - File fileToZip = new File(e.getValue()); - // 添加 ZIP 条目 - ZipEntry entry = new ZipEntry(e.getKey()); - entry.setSize(fileToZip.length()); + File file = new File(value); + if (!file.exists()) { + continue; + } - zos.putNextEntry(entry); + byte[] buffer = new byte[1024]; + FileInputStream fis = new FileInputStream(file); + ZipEntry zipEntry = new ZipEntry(key); + zos.putNextEntry(zipEntry); - try (FileInputStream fis = new FileInputStream(fileToZip)) { - byte[] buffer = new byte[4096]; - int len; - while ((len = fis.read(buffer)) > 0) { - zos.write(buffer, 0, len); - } + int length; + while ((length = fis.read(buffer)) > 0) { + zos.write(buffer, 0, length); } zos.closeEntry(); + fis.close(); } } - // 将临时文件移动到指定位置 - Path source = tempZipFile.toPath(); - //生成uuid -// String uuid = UUID.randomUUID().toString(); - Path destination = Paths.get("/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip"); - - // 确保目标目录存在 - Files.createDirectories(destination.getParent()); - - // 删除目标文件如果已存在 - if (Files.exists(destination)) { - Files.delete(destination); - } - - // 移动文件 - Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); - - // 返回文件路径给前端 - String filePath = destination.toAbsolutePath().toString(); - return success(filePath); } @GetMapping("/detail-study") public JsonResponse> detailStudy(String courseId, String aid){