diff --git a/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java b/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java index 50f3152..a82570b 100644 --- a/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java +++ b/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2018 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import org.springframework.messaging.MessagingException; * * @author Gunnar Hillert * @author Artem Bilan + * @author Ingo Dueppe * * @since 1.0 * @@ -58,24 +59,19 @@ public class UnZipTransformer extends AbstractZipTransformer { * a result from the executed Unzip operation. If set to true and * more than 1 element is returned, then that * 1 element is extracted and returned as payload. - * * If the result map contains more than 1 element and * {@link #expectSingleResult} is true, then a * {@link MessagingException} is thrown. - * * If set to false, the complete result list is returned as the * payload. This is the {@code default}. - * * @param expectSingleResult If not set explicitly, will default to false - * */ public void setExpectSingleResult(boolean expectSingleResult) { this.expectSingleResult = expectSingleResult; } @Override - protected Object doZipTransform(final Message message) throws Exception { - + protected Object doZipTransform(final Message message) { try { final Object payload = message.getPayload(); final Object unzippedData; @@ -135,6 +131,7 @@ public class UnZipTransformer extends AbstractZipTransformer { destinationFile.mkdirs(); //NOSONAR false positive } else { + mkDirOfAncestorDirectories(destinationFile); SpringZipUtils.copy(zipEntryInputStream, destinationFile); uncompressedData.put(zipEntryName, destinationFile); } @@ -182,7 +179,8 @@ public class UnZipTransformer extends AbstractZipTransformer { else { throw new MessagingException(message, String.format("The UnZip operation extracted %s " - + "result objects but expectSingleResult was 'true'.", uncompressedData.size())); + + "result objects but expectSingleResult was 'true'.", uncompressedData + .size())); } } else { @@ -209,4 +207,11 @@ public class UnZipTransformer extends AbstractZipTransformer { } } + private static void mkDirOfAncestorDirectories(File destinationFile) { + File parentDirectory = destinationFile.getParentFile(); + if (parentDirectory != null) { + parentDirectory.mkdirs(); + } + } + } diff --git a/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java b/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java index c48eaaa..00a8876 100644 --- a/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java +++ b/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2018 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * * @author Gunnar Hillert * @author Artem Bilan + * @author Ingo Dueppe * * @since 1.0 * @@ -71,15 +72,30 @@ public class UnZipTransformerTests { this.workDir = testFolder.newFolder(); } - /** - * UnCompress a ZIP archive containing a single file only. The result will be - * a byte array. - * - * @throws IOException - */ + @Test + public void unzipFlatFileEntryZip() throws IOException { + final Resource zipResource = this.resourceLoader.getResource("classpath:testzipdata/flatfileentry.zip"); + final InputStream is = zipResource.getInputStream(); + + final Message message = MessageBuilder.withPayload(is).build(); + + final UnZipTransformer unZipTransformer = new UnZipTransformer(); + unZipTransformer.setZipResultType(ZipResultType.FILE); + unZipTransformer.afterPropertiesSet(); + + final Message resultMessage = unZipTransformer.transform(message); + + Assert.assertNotNull(resultMessage); + + @SuppressWarnings("unchecked") + Map unzippedData = (Map) resultMessage.getPayload(); + + Assert.assertNotNull(unzippedData); + Assert.assertEquals(1, unzippedData.size()); + } + @Test public void unzipSingleFileAsInputStreamToByteArray() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip"); final InputStream is = resource.getInputStream(); @@ -97,16 +113,11 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * - * - * @throws IOException - */ @Test public void unzipSingleFileToByteArray() throws IOException { @@ -131,20 +142,14 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertTrue(inputFile.exists()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * - * - * @throws IOException - */ @Test public void unzipSingleFileToByteArrayWithDeleteFilesTrue() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip"); final InputStream is = resource.getInputStream(); @@ -169,21 +174,14 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertFalse(inputFile.exists()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * UnCompress a ZIP archive containing multiple files. The result will be - * a collection of files. - * - * @throws IOException - */ @Test public void unzipMultipleFilesAsInputStreamToByteArray() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip"); final InputStream is = resource.getInputStream(); @@ -201,19 +199,11 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 5); - + Assert.assertEquals(5, unzippedData.size()); } - /** - * UnCompress a ZIP archive containing multiple files. The result will be - * a collection of files. - * - * @throws IOException - */ @Test public void unzipMultipleFilesAsInputStreamWithExpectSingleResultTrue() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip"); final InputStream is = resource.getInputStream(); @@ -238,8 +228,7 @@ public class UnZipTransformerTests { } @Test - public void unzipInvalidZipFile() throws IOException, InterruptedException { - + public void unzipInvalidZipFile() throws IOException { File fileToUnzip = this.testFolder.newFile(); FileUtils.writeStringToFile(fileToUnzip, "hello world"); @@ -281,4 +270,5 @@ public class UnZipTransformerTests { containsString("is trying to leave the target output directory")); } } + } diff --git a/spring-integration-zip/src/test/resources/testzipdata/flat.txt b/spring-integration-zip/src/test/resources/testzipdata/flat.txt new file mode 100644 index 0000000..8758da1 --- /dev/null +++ b/spring-integration-zip/src/test/resources/testzipdata/flat.txt @@ -0,0 +1 @@ +Spring Integration Rocks! \ No newline at end of file diff --git a/spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip b/spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip new file mode 100644 index 0000000..22d2d82 Binary files /dev/null and b/spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip differ diff --git a/spring-integration-zip/src/test/resources/testzipdata/single.txt b/spring-integration-zip/src/test/resources/testzipdata/single.txt new file mode 100644 index 0000000..8758da1 --- /dev/null +++ b/spring-integration-zip/src/test/resources/testzipdata/single.txt @@ -0,0 +1 @@ +Spring Integration Rocks! \ No newline at end of file