This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.4.x by this push:
     new a857381722c CAMEL-20852: New option to allow adding empty files to a 
zip (#14426)
a857381722c is described below

commit a857381722c0401978b181f9c872632700d512fb
Author: Alexander Friedrichs <[email protected]>
AuthorDate: Mon Jun 10 07:22:52 2024 +0200

    CAMEL-20852: New option to allow adding empty files to a zip (#14426)
---
 .../aggregate/zipfile/ZipAggregationStrategy.java  | 33 +++++++++++++++---
 .../ZipAggregationStrategyEmptyFileTest.java       | 39 +++++++++++++++++++---
 2 files changed, 64 insertions(+), 8 deletions(-)

diff --git 
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
 
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
index 5b59f2eee7b..2584392cd66 100644
--- 
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
+++ 
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
@@ -55,6 +55,7 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
 
     private String filePrefix;
     private String fileSuffix = ".zip";
+    private boolean allowEmptyFiles;
     private boolean preserveFolderStructure;
     private boolean useFilenameHeader;
     private boolean useTempFile;
@@ -91,9 +92,25 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
      *                                of memory.
      */
     public ZipAggregationStrategy(boolean preserveFolderStructure, boolean 
useFilenameHeader, boolean useTempFile) {
+        this(preserveFolderStructure, useFilenameHeader, useTempFile, false);
+    }
+
+    /**
+     * @param preserveFolderStructure if true, the folder structure is 
preserved when the source is a type of
+     *                                {@link GenericFileMessage}. If used with 
a file, use recursive=true.
+     * @param useFilenameHeader       if true, the filename header will be 
used to name aggregated byte arrays within
+     *                                the ZIP file.
+     * @param useTempFile             if true, the ZipFileSystem will use 
temporary files for zip manipulations instead
+     *                                of memory.
+     * @param allowEmptyFiles         if true, the Aggregation will also add 
empty files to the zip.
+     *
+     */
+    public ZipAggregationStrategy(boolean preserveFolderStructure, boolean 
useFilenameHeader, boolean useTempFile,
+                                  boolean allowEmptyFiles) {
         this.preserveFolderStructure = preserveFolderStructure;
         this.useFilenameHeader = useFilenameHeader;
         this.useTempFile = useTempFile;
+        this.allowEmptyFiles = allowEmptyFiles;
     }
 
     /**
@@ -132,6 +149,14 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
         this.fileSuffix = fileSuffix;
     }
 
+    public boolean isAllowEmptyFiles() {
+        return allowEmptyFiles;
+    }
+
+    public void setAllowEmptyFiles(boolean allowEmptyFiles) {
+        this.allowEmptyFiles = allowEmptyFiles;
+    }
+
     public File getParentDir() {
         return parentDir;
     }
@@ -183,8 +208,8 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
         if (body instanceof File) {
             try {
                 File appendFile = (File) body;
-                // do not try to append empty files
-                if (appendFile.length() > 0) {
+                // try to append empty data only when explicit set
+                if (this.allowEmptyFiles || appendFile.length() > 0) {
                     String entryName = preserveFolderStructure
                             ? 
newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class)
                             : newExchange.getIn().getMessageId();
@@ -197,8 +222,8 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
             // Handle all other messages
             try {
                 byte[] buffer = 
newExchange.getIn().getMandatoryBody(byte[].class);
-                // do not try to append empty data
-                if (buffer.length > 0) {
+                // try to append empty data only when explicit set
+                if (this.allowEmptyFiles || buffer.length > 0) {
                     String entryName = useFilenameHeader
                             ? 
newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class)
                             : newExchange.getIn().getMessageId();
diff --git 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
index b3c5c0e7212..74fb0fb6bf4 100644
--- 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
+++ 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.processor.aggregate.zipfile;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
@@ -35,19 +36,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class ZipAggregationStrategyEmptyFileTest extends CamelTestSupport {
 
-    private static final int EXPECTED_NO_FILES = 3;
+    private static final int EXPECTED_NO_EMPTY_FILES = 3;
+    private static final int EXPECTED_WITH_EMPTY_FILE = 4;
     private static final String TEST_DIR = 
"target/out_ZipAggregationStrategyEmptyFileTest";
 
     @Override
     @BeforeEach
     public void setUp() throws Exception {
         deleteDirectory("target/foo");
+        deleteDirectory("target/bar");
         deleteDirectory(TEST_DIR);
         super.setUp();
     }
 
     @Test
-    public void testEmptyFile() throws Exception {
+    public void testNoEmptyFile() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
         mock.expectedMessageCount(1);
 
@@ -57,6 +60,23 @@ public class ZipAggregationStrategyEmptyFileTest extends 
CamelTestSupport {
         template.sendBody("file:target/foo", "Bye");
         template.sendBody("file:target/foo", "Howdy");
 
+        
checkResult(ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_EMPTY_FILES);
+    }
+
+    @Test
+    public void testAddEmptyFile() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("file:target/bar", "Hello");
+        template.sendBody("file:target/bar", "");
+        template.sendBody("file:target/bar", "Bye");
+        template.sendBody("file:target/bar", "Howdy");
+
+        
checkResult(ZipAggregationStrategyEmptyFileTest.EXPECTED_WITH_EMPTY_FILE);
+    }
+
+    private void checkResult(int expectedCount) throws InterruptedException, 
IOException {
         MockEndpoint.assertIsSatisfied(context);
 
         File[] files = new File(TEST_DIR).listFiles();
@@ -71,8 +91,8 @@ public class ZipAggregationStrategyEmptyFileTest extends 
CamelTestSupport {
             for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = 
zin.getNextEntry()) {
                 fileCount++;
             }
-            
assertEquals(ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_FILES, fileCount,
-                    "Zip file should contains " + 
ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_FILES + " files");
+            assertEquals(expectedCount, fileCount,
+                    "Zip file should contains " + expectedCount + " files");
         } finally {
             IOHelper.close(zin);
         }
@@ -91,6 +111,17 @@ public class ZipAggregationStrategyEmptyFileTest extends 
CamelTestSupport {
                         .to("file:" + TEST_DIR)
                         .to("mock:aggregateToZipEntry")
                         .log("Done processing zip file: 
${header.CamelFileName}");
+
+                ZipAggregationStrategy allowEmptyFilesZipAggregationStrategy = 
new ZipAggregationStrategy();
+                allowEmptyFilesZipAggregationStrategy.setAllowEmptyFiles(true);
+                from("file:target/bar")
+                        .aggregate(allowEmptyFilesZipAggregationStrategy)
+                        .constant(true)
+                        .completionSize(4)
+                        .eagerCheckCompletion()
+                        .to("file:" + TEST_DIR)
+                        .to("mock:aggregateToZipEntry")
+                        .log("Done processing zip file: 
${header.CamelFileName}");
             }
         };
 

Reply via email to