This is an automated email from the ASF dual-hosted git repository. nmalin pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push: new bac7bd005c Improved: Improve FileUtil to generate a zip fil with multiple entries bac7bd005c is described below commit bac7bd005ccb2cc36f45cf7f4761b1889094b534 Author: Nicolas Malin <nicolas.ma...@nereide.fr> AuthorDate: Sun May 21 15:23:33 2023 +0200 Improved: Improve FileUtil to generate a zip fil with multiple entries Add a new function zipFileStreams to extend a current zipFileStream (that permit to generate a zip file from a single file) to support the zip file creation from multiple file. The function take a map as fileName attendee in the zip and as value the inputStream related. --- .../java/org/apache/ofbiz/base/util/FileUtil.java | 41 +++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java index 972d83e3ec..774ae88778 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java @@ -39,6 +39,7 @@ import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.zip.Deflater; @@ -342,8 +343,28 @@ public final class FileUtil { * @throws IOException */ public static ByteArrayInputStream zipFileStream(InputStream fileStream, String fileName) throws IOException { - if (fileStream == null) return null; - if (fileName == null) fileName = UUID.randomUUID().toString(); + if (fileStream == null) { + return null; + } + if (fileName == null) { + fileName = UUID.randomUUID().toString(); + } + + // Create zip file from content input stream + return zipFileStreams(Map.of(fileName, fileStream)); + } + + /** + * For map with entries as [fileName: inputStream], create a zip stream containing all given files + * @param files + * @return + * @throws IOException + */ + public static ByteArrayInputStream zipFileStreams(Map<String, InputStream> files) throws IOException { + if (files == null) { + return null; + } + // Create zip file from content input stream String zipFileName = UUID.randomUUID().toString() + ".zip"; String zipFilePath = UtilProperties.getPropertyValue("general", "http.upload.tmprepository", "runtime/tmp"); @@ -351,12 +372,16 @@ public final class FileUtil { ZipOutputStream zos = new ZipOutputStream(fos); zos.setMethod(ZipOutputStream.DEFLATED); zos.setLevel(Deflater.BEST_COMPRESSION); - ZipEntry ze = new ZipEntry(fileName); - zos.putNextEntry(ze); - int len; - byte[] bufferData = new byte[8192]; - while ((len = fileStream.read(bufferData)) > 0) { - zos.write(bufferData, 0, len); + + // parse all map to set in the zip stream + for (String fileName : files.keySet()) { + ZipEntry ze = new ZipEntry(fileName); + zos.putNextEntry(ze); + int len; + byte[] bufferData = new byte[8192]; + while ((len = files.get(fileName).read(bufferData)) > 0) { + zos.write(bufferData, 0, len); + } } zos.closeEntry(); zos.close();