GH-215: Fix for flat zip structure
Fixes https://github.com/spring-projects/spring-integration-extensions/issues/215 * Add test of unzipping flat entry zip * Changed indent from key to tap * Fix formatting * Enhance author information * Add extra test files * Polishing code style
This commit is contained in:
@@ -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 <code>true</code> 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 <code>true</code>, then a
|
||||
* {@link MessagingException} is thrown.
|
||||
*
|
||||
* If set to <code>false</code>, 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<InputStream> 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<String, byte[]> unzippedData = (Map<String, byte[]>) 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<String, byte[]> unzippedData = (Map<String, byte[]>) 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<String, byte[]> unzippedData = (Map<String, byte[]>) 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<String, byte[]> unzippedData = (Map<String, byte[]>) 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<String, byte[]> unzippedData = (Map<String, byte[]>) 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"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Spring Integration Rocks!
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Spring Integration Rocks!
|
||||
Reference in New Issue
Block a user