use of org.apache.commons.compress.archivers.ArchiveStreamFactory 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.ArchiveStreamFactory 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.ArchiveStreamFactory in project Xponents by OpenSextant.
the class ArchiveNavigator method unzip.
/*
* Un-TAR. Oops. Its just a copy of Un-TAR and I replace tar with zip.
*
* so there may be Zip-specific stuff here, ... but the approach is the
* same.
*/
public File unzip(File zipFile) throws IOException, ConfigException {
// String _working = FilenameUtils.concat(getWorkingDir(),
// FilenameUtils.getBaseName(zipFile.getPath()));
// if (_working == null){
// throw new IOException("Invalid archive path for "+zipFile.getPath());
// }
// File workingDir = new File(_working);
// workingDir.mkdir();
File workingDir = saveDir;
InputStream input = new BufferedInputStream(new FileInputStream(zipFile));
ZipArchiveInputStream in = null;
try {
in = (ZipArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("zip", input));
ZipArchiveEntry zipEntry;
while ((zipEntry = (ZipArchiveEntry) in.getNextEntry()) != null) {
if (filterEntry(zipEntry)) {
continue;
}
try {
File tmpFile = saveArchiveEntry(zipEntry, in, workingDir);
converter.convert(tmpFile);
} catch (IOException err) {
log.error("Unable to save item, FILE=" + zipEntry.getName() + "!" + zipEntry.getName(), err);
}
}
return workingDir;
} catch (ArchiveException ae) {
throw new IOException(ae);
} finally {
in.close();
}
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project POL-POM-5 by PlayOnLinux.
the class Zip method uncompress.
/**
* Uncompress a tar
*
* @param countingInputStream to count the number of byte extracted
* @param outputDir The directory where files should be extracted
* @return A list of extracted files
* @throws ArchiveException if the process fails
*/
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
final List<File> uncompressedFiles = new LinkedList<>();
try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip", inputStream)) {
ZipArchiveEntry entry;
while ((entry = (ZipArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
Files.createDirectories(outputFile.toPath());
}
} else {
LOGGER.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
outputFile.getParentFile().mkdirs();
try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
IOUtils.copy(debInputStream, outputFileStream);
}
}
uncompressedFiles.add(outputFile);
stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
}
return uncompressedFiles;
} catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
throw new ArchiveException("Unable to extract the file", e);
}
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project tika by apache.
the class PackageParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
//lazily load the MediaTypeRegistry at parse time
//only want to call getDefaultConfig() once, and can't
//load statically because of the ForkParser
TikaConfig config = context.get(TikaConfig.class);
MediaTypeRegistry mediaTypeRegistry = null;
if (config != null) {
mediaTypeRegistry = config.getMediaTypeRegistry();
} else {
if (bufferedMediaTypeRegistry == null) {
//buffer this for next time.
synchronized (lock) {
//now that we're locked, check again
if (bufferedMediaTypeRegistry == null) {
bufferedMediaTypeRegistry = TikaConfig.getDefaultConfig().getMediaTypeRegistry();
}
}
}
mediaTypeRegistry = bufferedMediaTypeRegistry;
}
// Ensure that the stream supports the mark feature
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
TemporaryResources tmp = new TemporaryResources();
ArchiveInputStream ais = null;
try {
ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory());
// At the end we want to close the archive stream to release
// any associated resources, but the underlying document stream
// should not be closed
ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream));
} catch (StreamingNotSupportedException sne) {
// Most archive formats work on streams, but a few need files
if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) {
// Rework as a file, and wrap
stream.reset();
TikaInputStream tstream = TikaInputStream.get(stream, tmp);
// Seven Zip suports passwords, was one given?
String password = null;
PasswordProvider provider = context.get(PasswordProvider.class);
if (provider != null) {
password = provider.getPassword(metadata);
}
SevenZFile sevenz;
if (password == null) {
sevenz = new SevenZFile(tstream.getFile());
} else {
sevenz = new SevenZFile(tstream.getFile(), password.getBytes("UnicodeLittleUnmarked"));
}
// Pending a fix for COMPRESS-269 / TIKA-1525, this bit is a little nasty
ais = new SevenZWrapper(sevenz);
} else {
tmp.close();
throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne);
}
} catch (ArchiveException e) {
tmp.close();
throw new TikaException("Unable to unpack document stream", e);
}
updateMediaType(ais, mediaTypeRegistry, metadata);
// Use the delegate parser to parse the contained document
EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
try {
ArchiveEntry entry = ais.getNextEntry();
while (entry != null) {
if (!entry.isDirectory()) {
parseEntry(ais, entry, extractor, metadata, xhtml);
}
entry = ais.getNextEntry();
}
} catch (UnsupportedZipFeatureException zfe) {
// If it's an encrypted document of unknown password, report as such
if (zfe.getFeature() == Feature.ENCRYPTION) {
throw new EncryptedDocumentException(zfe);
}
// Otherwise throw the exception
throw new TikaException("UnsupportedZipFeature", zfe);
} catch (PasswordRequiredException pre) {
throw new EncryptedDocumentException(pre);
} finally {
ais.close();
tmp.close();
}
xhtml.endDocument();
}
Aggregations