Search in sources :

Example 76 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project camel by apache.

the class TarFileDataFormat method unmarshal.

@Override
public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
    if (usingIterator) {
        return new TarIterator(exchange.getIn(), stream);
    } else {
        BufferedInputStream bis = new BufferedInputStream(stream);
        TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis);
        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
        try {
            TarArchiveEntry entry = tis.getNextTarEntry();
            if (entry != null) {
                exchange.getOut().setHeader(FILE_NAME, entry.getName());
                IOHelper.copy(tis, osb);
            }
            entry = tis.getNextTarEntry();
            if (entry != null) {
                throw new IllegalStateException("Tar file has more than 1 entry.");
            }
            return osb.build();
        } finally {
            IOHelper.close(osb, tis, bis);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 77 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project camel by apache.

the class TarAggregationStrategy method addEntryToTar.

private void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException {
    File tmpTar = File.createTempFile(source.getName(), null, parentDir);
    tmpTar.delete();
    if (!source.renameTo(tmpTar)) {
        throw new IOException("Cannot create temp file: " + source.getName());
    }
    FileInputStream fis = new FileInputStream(tmpTar);
    TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    // copy the existing entries    
    ArchiveEntry nextEntry;
    while ((nextEntry = tin.getNextEntry()) != null) {
        tos.putArchiveEntry(nextEntry);
        IOUtils.copy(tin, tos);
        tos.closeArchiveEntry();
    }
    // Create new entry
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setSize(length);
    tos.putArchiveEntry(entry);
    tos.write(buffer, 0, length);
    tos.closeArchiveEntry();
    IOHelper.close(fis, tin, tos);
    LOG.trace("Deleting temporary file: {}", tmpTar);
    FileUtil.deleteFile(tmpTar);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 78 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project camel by apache.

the class AggregationStrategyWithFilenameHeaderTest method testSplitter.

@Test
public void testSplitter() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry");
    mock.expectedMessageCount(1);
    template.setDefaultEndpointUri("direct:start");
    template.sendBodyAndHeader("foo", Exchange.FILE_NAME, FILE_NAMES.get(0));
    template.sendBodyAndHeader("bar", Exchange.FILE_NAME, FILE_NAMES.get(1));
    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];
    final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(new FileInputStream(resultFile)));
    try {
        int fileCount = 0;
        for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis.getNextTarEntry()) {
            fileCount++;
            assertTrue("Tar entry file name should be on of: " + FILE_NAMES, FILE_NAMES.contains(entry.getName()));
        }
        assertEquals("Tar file should contain " + FILE_NAMES.size() + " files", FILE_NAMES.size(), fileCount);
    } finally {
        IOHelper.close(tis);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 79 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project camel by apache.

the class AggregationStrategyWithPreservationTest method testSplitter.

@Test
public void testSplitter() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry");
    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> expectedTarFiles = new HashSet<String>(Arrays.asList("another/hello.txt", "other/greetings.txt", "chiau.txt", "hi.txt", "hola.txt"));
    TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(resultFile));
    try {
        int fileCount = 0;
        for (TarArchiveEntry te = tin.getNextTarEntry(); te != null; te = tin.getNextTarEntry()) {
            String name = te.getName();
            if (expectedTarFiles.contains(name)) {
                expectedTarFiles.remove(te.getName());
            } else {
                fail("Unexpected name in tarfile: " + name);
            }
            fileCount++;
        }
        assertTrue("Tar file should contains " + AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES + " files", fileCount == AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES);
        assertEquals("Should have found all of the tar files in the file.", 0, expectedTarFiles.size());
    } finally {
        IOHelper.close(tin);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) FileInputStream(java.io.FileInputStream) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project storm by apache.

the class Utils method unTarUsingJava.

private static void unTarUsingJava(File inFile, File untarDir, boolean gzipped) throws IOException {
    InputStream inputStream = null;
    try {
        if (gzipped) {
            inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile)));
        } else {
            inputStream = new BufferedInputStream(new FileInputStream(inFile));
        }
        try (TarArchiveInputStream tis = new TarArchiveInputStream(inputStream)) {
            for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) {
                unpackEntries(tis, entry, untarDir);
                entry = tis.getNextTarEntry();
            }
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)132 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)99 File (java.io.File)52 IOException (java.io.IOException)50 FileInputStream (java.io.FileInputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)46 InputStream (java.io.InputStream)35 FileOutputStream (java.io.FileOutputStream)34 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 OutputStream (java.io.OutputStream)16 Path (java.nio.file.Path)16 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)8