Search in sources :

Example 21 with ArchiveStreamFactory

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);
}
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 22 with ArchiveStreamFactory

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);
    }
}
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 23 with ArchiveStreamFactory

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();
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Example 24 with ArchiveStreamFactory

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);
    }
}
Also used : LinkedList(java.util.LinkedList) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 25 with ArchiveStreamFactory

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();
}
Also used : StreamingNotSupportedException(org.apache.commons.compress.archivers.StreamingNotSupportedException) TikaException(org.apache.tika.exception.TikaException) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaConfig(org.apache.tika.config.TikaConfig) EmbeddedDocumentExtractor(org.apache.tika.extractor.EmbeddedDocumentExtractor) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) MediaTypeRegistry(org.apache.tika.mime.MediaTypeRegistry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) PasswordRequiredException(org.apache.commons.compress.PasswordRequiredException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) PasswordProvider(org.apache.tika.parser.PasswordProvider) UnsupportedZipFeatureException(org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) DumpArchiveInputStream(org.apache.commons.compress.archivers.dump.DumpArchiveInputStream) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile) BufferedInputStream(java.io.BufferedInputStream) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Aggregations

ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)38 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)18 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)14 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)11 InputStream (java.io.InputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 LinkedList (java.util.LinkedList)9 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)9 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)8 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)8 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)8 OutputStream (java.io.OutputStream)7 GZIPInputStream (java.util.zip.GZIPInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4