Search in sources :

Example 71 with ZipInputStream

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;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 72 with ZipInputStream

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);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 73 with ZipInputStream

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);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 74 with ZipInputStream

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);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 75 with ZipInputStream

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);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43