use of java.util.zip.ZipInputStream in project translationstudio8 by heartsome.
the class ZipUtil method upZipFile.
/**
* 将压缩文件中的内容解压到指定目录中<br>
* 如果<code>baseDir</code>的值为空,则将文件解压到相同的目录中,目录名称为"zipFile_files"
* @param zipFile
* 压缩文件路径
* @param baseDir
* 解压的目标路径,可以为null
* @throws IOException
*/
public static String upZipFile(String zipFile, String baseDir) throws IOException {
File f = new File(zipFile);
if (baseDir == null) {
baseDir = f.getPath() + "_files";
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
ZipEntry ze;
byte[] buf = new byte[1024];
while ((ze = zis.getNextEntry()) != null) {
File outFile = getRealFileName(baseDir, ze.getName());
FileOutputStream os = new FileOutputStream(outFile);
int readLen = 0;
while ((readLen = zis.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
os.close();
}
zis.close();
return baseDir;
}
use of java.util.zip.ZipInputStream in project camel by apache.
the class ZipAggregationStrategyEmptyFileTest method testEmptyFile.
@Test
public void testEmptyFile() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
mock.expectedMessageCount(1);
template.sendBody("file:target/foo", "Hello");
// empty file which is not aggregated
template.sendBody("file:target/foo", "");
template.sendBody("file:target/foo", "Bye");
template.sendBody("file:target/foo", "Howdy");
assertMockEndpointsSatisfied();
Thread.sleep(500);
File[] files = new File("target/out").listFiles();
assertTrue(files != null);
assertTrue("Should be a file in target/out directory", files.length > 0);
File resultFile = files[0];
ZipInputStream zin = new ZipInputStream(new FileInputStream(resultFile));
try {
int fileCount = 0;
for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
fileCount = fileCount + 1;
}
assertEquals("Zip file should contains " + ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_FILES + " files", ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_FILES, fileCount);
} finally {
IOHelper.close(zin);
}
}
use of java.util.zip.ZipInputStream in project camel by apache.
the class ZipAggregationStrategyTest method testSplitter.
@Test
public void testSplitter() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("foo", "bar");
assertMockEndpointsSatisfied();
Thread.sleep(500);
File[] files = new File("target/out").listFiles();
assertTrue(files != null);
assertTrue("Should be a file in target/out directory", files.length > 0);
File resultFile = files[0];
ZipInputStream zin = new ZipInputStream(new FileInputStream(resultFile));
try {
int fileCount = 0;
for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
fileCount = fileCount + 1;
}
assertEquals("Zip file should contains " + ZipAggregationStrategyTest.EXPECTED_NO_FILES + " files", ZipAggregationStrategyTest.EXPECTED_NO_FILES, fileCount);
} finally {
IOHelper.close(zin);
}
}
use of java.util.zip.ZipInputStream in project camel by apache.
the class ZipFileDataFormat method unmarshal.
@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
if (usingIterator) {
ZipIterator zipIterator = new ZipIterator(exchange.getIn());
zipIterator.setAllowEmptyDirectory(allowEmptyDirectory);
return zipIterator;
} else {
ZipInputStream zis = new ZipInputStream(inputStream);
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
ZipEntry entry = zis.getNextEntry();
if (entry != null) {
exchange.getOut().setHeader(FILE_NAME, entry.getName());
IOHelper.copy(zis, osb);
}
entry = zis.getNextEntry();
if (entry != null) {
throw new IllegalStateException("Zip file has more than 1 entry.");
}
return osb.build();
} finally {
IOHelper.close(zis, osb);
}
}
}
use of java.util.zip.ZipInputStream in project camel by apache.
the class AggregationStrategyWithPreservationTest method testSplitter.
@Test
public void testSplitter() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
mock.expectedMessageCount(1);
assertMockEndpointsSatisfied();
Thread.sleep(500);
File[] files = new File("target/out").listFiles();
assertTrue("Should be a file in target/out directory", files.length > 0);
File resultFile = files[0];
Set<String> expectedZipFiles = new HashSet<String>(Arrays.asList("another" + File.separator + "hello.txt", "other" + File.separator + "greetings.txt", "chiau.txt", "hi.txt", "hola.txt"));
ZipInputStream zin = new ZipInputStream(new FileInputStream(resultFile));
try {
int fileCount = 0;
for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
expectedZipFiles.remove(ze.toString());
fileCount++;
}
assertTrue("Zip file should contains " + AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES + " files", fileCount == AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES);
assertEquals("Should have found all of the zip files in the file.", 0, expectedZipFiles.size());
} finally {
IOHelper.close(zin);
}
}
Aggregations