From f8c2ff785e6d58292a4c5e9b06220fa281f8e19c Mon Sep 17 00:00:00 2001 From: RB Date: Mon, 7 Oct 2024 13:41:22 +0800 Subject: [PATCH] feat: word zip --- @rbv | 2 +- .../service/datareport/DataReportManager.java | 1 + .../service/datareport/ReportsException.java | 4 ++ .../core/support/setup/DatabaseBackup.java | 4 +- .../java/com/rebuild/utils/CompressUtils.java | 45 ++++++++++++++++--- .../web/general/ReportsController.java | 3 +- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/@rbv b/@rbv index 44a1fed9ad..d9e6649023 160000 --- a/@rbv +++ b/@rbv @@ -1 +1 @@ -Subproject commit 44a1fed9ad96a93b55a4bd95f11904864d83b9fe +Subproject commit d9e6649023b80aeb93ef4668886c1a674dbfae0c diff --git a/src/main/java/com/rebuild/core/service/datareport/DataReportManager.java b/src/main/java/com/rebuild/core/service/datareport/DataReportManager.java index 0d762348b6..a37f0553dc 100644 --- a/src/main/java/com/rebuild/core/service/datareport/DataReportManager.java +++ b/src/main/java/com/rebuild/core/service/datareport/DataReportManager.java @@ -238,6 +238,7 @@ public static String getPrettyReportName(ID reportId, Object idOrEntity, String else if (fileName.endsWith(".xlsx")) name += ".xlsx"; else if (fileName.endsWith(".xls")) name += ".xls"; else if (fileName.endsWith(".csv")) name += ".csv"; + else if (fileName.endsWith(".zip")) name += ".zip"; return StringUtils.defaultIfBlank(name, "UNTITLE"); } diff --git a/src/main/java/com/rebuild/core/service/datareport/ReportsException.java b/src/main/java/com/rebuild/core/service/datareport/ReportsException.java index 9903c84155..eca1806b73 100644 --- a/src/main/java/com/rebuild/core/service/datareport/ReportsException.java +++ b/src/main/java/com/rebuild/core/service/datareport/ReportsException.java @@ -23,4 +23,8 @@ public ReportsException(String msg) { public ReportsException(Throwable cause) { super(cause); } + + public ReportsException(String msg, Throwable cause) { + super(msg, cause); + } } diff --git a/src/main/java/com/rebuild/core/support/setup/DatabaseBackup.java b/src/main/java/com/rebuild/core/support/setup/DatabaseBackup.java index 1002f5312d..e5a7ff25c1 100644 --- a/src/main/java/com/rebuild/core/support/setup/DatabaseBackup.java +++ b/src/main/java/com/rebuild/core/support/setup/DatabaseBackup.java @@ -91,8 +91,8 @@ public File backup(File backups) throws IOException { File zip = new File(backups, destName + ".zip"); try { - CompressUtils.forceZip(dest, zip, null); - + CompressUtils.forceZip(zip, dest, null); + FileUtils.deleteQuietly(dest); dest = zip; } catch (Exception e) { diff --git a/src/main/java/com/rebuild/utils/CompressUtils.java b/src/main/java/com/rebuild/utils/CompressUtils.java index f72e57a100..e1b7ceba8f 100644 --- a/src/main/java/com/rebuild/utils/CompressUtils.java +++ b/src/main/java/com/rebuild/utils/CompressUtils.java @@ -31,29 +31,29 @@ public class CompressUtils { /** + * @param destZip * @param fileOrDir - * @param destZip delete after create * @param filter * @throws IOException */ - public static void forceZip(File fileOrDir, File destZip, FileFilter filter) throws IOException { + public static void forceZip(File destZip, File fileOrDir, FileFilter filter) throws IOException { if (destZip.exists()) { log.warn("delete exists after create : {}", destZip); FileUtils.deleteQuietly(destZip); } - zip(fileOrDir, Files.newOutputStream(destZip.toPath()), filter); + zip(Files.newOutputStream(destZip.toPath()), fileOrDir, filter); } /** * Creates a zip output stream at the specified path with the contents of the specified directory. * - * @param fileOrDir * @param zipOutputStream + * @param fileOrDir * @param filter * @throws IOException */ - public static void zip(File fileOrDir, OutputStream zipOutputStream, FileFilter filter) throws IOException { + public static void zip(OutputStream zipOutputStream, File fileOrDir, FileFilter filter) throws IOException { BufferedOutputStream bufferedOutputStream = null; ZipArchiveOutputStream zipArchiveOutputStream = null; @@ -74,6 +74,41 @@ public static void zip(File fileOrDir, OutputStream zipOutputStream, FileFilter } } + /** + * @param destZip + * @param files + * @throws IOException + */ + public static void forceZip(File destZip, File... files) throws IOException { + if (destZip.exists()) { + log.warn("delete exists after create : {}", destZip); + FileUtils.deleteQuietly(destZip); + } + + OutputStream zipOutputStream = Files.newOutputStream(destZip.toPath()); + BufferedOutputStream bufferedOutputStream = null; + ZipArchiveOutputStream zipArchiveOutputStream = null; + + try { + bufferedOutputStream = new BufferedOutputStream(zipOutputStream); + zipArchiveOutputStream = new ZipArchiveOutputStream(bufferedOutputStream); + + for (File file : files) { + addFileToZip(zipArchiveOutputStream, file, null, null); + } + + } finally { + if (zipArchiveOutputStream != null) { + zipArchiveOutputStream.finish(); + zipArchiveOutputStream.close(); + } + + IOUtils.closeQuietly(bufferedOutputStream); + IOUtils.closeQuietly(zipOutputStream); + } + + } + private static void addFileToZip(ZipArchiveOutputStream zipArchiveOutputStream, File file, String path, FileFilter filter) throws IOException { // at first call it is the folder, otherwise is the relative path String entryName = (path != null) ? path + file.getName() : file.getName(); diff --git a/src/main/java/com/rebuild/web/general/ReportsController.java b/src/main/java/com/rebuild/web/general/ReportsController.java index 28683f45ad..e9aee50132 100644 --- a/src/main/java/com/rebuild/web/general/ReportsController.java +++ b/src/main/java/com/rebuild/web/general/ReportsController.java @@ -97,9 +97,8 @@ public ModelAndView reportGenerate(@PathVariable String entity, try { EasyExcelGenerator reportGenerator; if (tt.type == DataReportManager.TYPE_WORD) { - // 暂不支持多个 reportGenerator = (EasyExcelGenerator33) CommonsUtils.invokeMethod( - "com.rebuild.rbv.data.WordReportGenerator#create", reportId, recordId); + "com.rebuild.rbv.data.WordReportGenerator#create", reportId, recordIds); } else if (tt.type == DataReportManager.TYPE_HTML5) { reportGenerator = (EasyExcelGenerator33) CommonsUtils.invokeMethod( "com.rebuild.rbv.data.Html5ReportGenerator#create", reportId, recordIds);