Skip to content

Commit

Permalink
feat: word zip
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Oct 7, 2024
1 parent c6730ac commit f8c2ff7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion @rbv
Submodule @rbv updated from 44a1fe to d9e664
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public ReportsException(String msg) {
public ReportsException(Throwable cause) {
super(cause);
}

public ReportsException(String msg, Throwable cause) {
super(msg, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 40 additions & 5 deletions src/main/java/com/rebuild/utils/CompressUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/rebuild/web/general/ReportsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f8c2ff7

Please sign in to comment.