CAMEL-22400: Add support for empty zip files / entries (#19199)
diff --git a/components/camel-zipfile/src/main/java/org/apache/camel/dataformat/zipfile/ZipIterator.java b/components/camel-zipfile/src/main/java/org/apache/camel/dataformat/zipfile/ZipIterator.java
index 637d6cb..5ea5534 100644
--- a/components/camel-zipfile/src/main/java/org/apache/camel/dataformat/zipfile/ZipIterator.java
+++ b/components/camel-zipfile/src/main/java/org/apache/camel/dataformat/zipfile/ZipIterator.java
@@ -86,13 +86,9 @@
parent = getNextElement();
if (parent == null) {
zipInputStream.close();
- availableDataInCurrentEntry = false;
} else {
availableDataInCurrentEntry = true;
}
- if (first && parent == null) {
- throw new IllegalStateException("Unable to unzip the file, it may be corrupted.");
- }
}
return availableDataInCurrentEntry;
} catch (IOException exception) {
diff --git a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorCorruptTest.java b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorCorruptTest.java
index dfc5412..b31c257 100644
--- a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorCorruptTest.java
+++ b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorCorruptTest.java
@@ -20,6 +20,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.RoutesBuilder;
+import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
@@ -31,7 +32,7 @@
public void testZipFileUnmarshal() throws Exception {
getMockEndpoint("mock:dead").expectedMessageCount(1);
getMockEndpoint("mock:dead").message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT)
- .isInstanceOf(IllegalStateException.class);
+ .isInstanceOf(RuntimeCamelException.class);
getMockEndpoint("mock:end").expectedMessageCount(0);
MockEndpoint.assertIsSatisfied(context);
diff --git a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorEmptyTest.java b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorEmptyTest.java
new file mode 100644
index 0000000..f392eac
--- /dev/null
+++ b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitIteratorEmptyTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dataformat.zipfile;
+
+import java.util.Iterator;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class ZipFileSplitIteratorEmptyTest extends CamelTestSupport {
+
+ @Test
+ public void testZipFileUnmarshal() throws Exception {
+ getMockEndpoint("mock:end").expectedMessageCount(0);
+ getMockEndpoint("mock:end").setSleepForEmptyTest(1000);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ ZipFileDataFormat zf = new ZipFileDataFormat();
+ zf.setUsingIterator(true);
+
+ from("file://src/test/resources?delay=10&fileName=empty.zip&noop=true")
+ .unmarshal(zf)
+ .split(bodyAs(Iterator.class)).streaming()
+ .convertBodyTo(String.class)
+ .to("mock:end")
+ .end();
+ }
+ };
+ }
+}
diff --git a/components/camel-zipfile/src/test/resources/corrupt.zip b/components/camel-zipfile/src/test/resources/corrupt.zip
index fc74404..7ed8f58 100644
--- a/components/camel-zipfile/src/test/resources/corrupt.zip
+++ b/components/camel-zipfile/src/test/resources/corrupt.zip
@@ -1 +1 @@
-I am corrupted
\ No newline at end of file
+I am totally corrupted!
\ No newline at end of file
diff --git a/components/camel-zipfile/src/test/resources/empty.zip b/components/camel-zipfile/src/test/resources/empty.zip
new file mode 100644
index 0000000..15cb0ec
--- /dev/null
+++ b/components/camel-zipfile/src/test/resources/empty.zip
Binary files differ