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);
}
}
}
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);
}
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);
}
}
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);
}
}
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();
}
}
}
Aggregations