Search in sources :

Example 6 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project caffeine by ben-manes.

the class AbstractTraceReader method readFile.

/** Returns the input stream, decompressing if required. */
private InputStream readFile(String filePath) throws IOException {
    BufferedInputStream input = new BufferedInputStream(openFile(filePath), BUFFER_SIZE);
    input.mark(100);
    try {
        return new XZInputStream(input);
    } catch (IOException e) {
        input.reset();
    }
    try {
        return new CompressorStreamFactory().createCompressorInputStream(input);
    } catch (CompressorException e) {
        input.reset();
    }
    try {
        return new ArchiveStreamFactory().createArchiveInputStream(input);
    } catch (ArchiveException e) {
        input.reset();
    }
    return input;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) XZInputStream(org.tukaani.xz.XZInputStream) BufferedInputStream(java.io.BufferedInputStream) CompressorException(org.apache.commons.compress.compressors.CompressorException) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Example 7 with ArchiveStreamFactory

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

the class TarAggregationStrategy method addFileToTar.

private void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException {
    File tmpTar = File.createTempFile(source.getName(), null, parentDir);
    tmpTar.delete();
    if (!source.renameTo(tmpTar)) {
        throw new IOException("Could not make 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);
    InputStream in = new FileInputStream(file);
    // copy the existing entries    
    ArchiveEntry nextEntry;
    while ((nextEntry = tin.getNextEntry()) != null) {
        tos.putArchiveEntry(nextEntry);
        IOUtils.copy(tin, tos);
        tos.closeArchiveEntry();
    }
    // Add the new entry
    TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName);
    entry.setSize(file.length());
    tos.putArchiveEntry(entry);
    IOUtils.copy(in, tos);
    tos.closeArchiveEntry();
    IOHelper.close(fis, in, 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) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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 8 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project tika by apache.

the class PackageParserTest method testCoverage.

@Test
public void testCoverage() throws Exception {
    //test that the package parser covers all inputstreams handled
    //by ArchiveStreamFactory.  When we update commons-compress, and they add
    //a new stream type, we want to make sure that we're handling it.
    ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(StandardCharsets.UTF_8.name());
    PackageParser packageParser = new PackageParser();
    ParseContext parseContext = new ParseContext();
    for (String name : archiveStreamFactory.getInputStreamArchiveNames()) {
        MediaType mt = PackageParser.getMediaType(name);
        //name of the missing stream
        if (mt.equals(MediaType.OCTET_STREAM)) {
            fail("getting octet-stream for: " + name);
        }
        if (!packageParser.getSupportedTypes(parseContext).contains(mt)) {
            fail("PackageParser should support: " + mt.toString());
        }
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ParseContext(org.apache.tika.parser.ParseContext) MediaType(org.apache.tika.mime.MediaType) Test(org.junit.Test)

Example 9 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project halyard by spinnaker.

the class BackupService method untarHalconfig.

private void untarHalconfig(String halconfigDir, String halconfigTar) {
    FileInputStream tarInput = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    try {
        tarInput = new FileInputStream(new File(halconfigTar));
        tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", tarInput);
    } catch (IOException | ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to open backup: " + e.getMessage(), e);
    }
    try {
        ArchiveEntry archiveEntry = tarArchiveInputStream.getNextEntry();
        while (archiveEntry != null) {
            String entryName = archiveEntry.getName();
            Path outputPath = Paths.get(halconfigDir, entryName);
            File outputFile = outputPath.toFile();
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            if (archiveEntry.isDirectory()) {
                outputFile.mkdir();
            } else {
                Files.copy(tarArchiveInputStream, outputPath, REPLACE_EXISTING);
            }
            archiveEntry = tarArchiveInputStream.getNextEntry();
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read archive entry: " + e.getMessage(), e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 10 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project halyard by spinnaker.

the class RegistryBackedArchiveProfileBuilder method build.

public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath, SpinnakerArtifact artifact, String archiveName) {
    String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact);
    InputStream is;
    try {
        is = profileRegistry.readArchiveProfile(artifact.getName(), version, archiveName);
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Error retrieving contents of archive " + archiveName + ".tar.gz", e);
    }
    TarArchiveInputStream tis;
    try {
        tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e);
    }
    try {
        List<Profile> result = new ArrayList<>();
        ArchiveEntry profileEntry = tis.getNextEntry();
        while (profileEntry != null) {
            if (profileEntry.isDirectory()) {
                profileEntry = tis.getNextEntry();
                continue;
            }
            String entryName = profileEntry.getName();
            String profileName = String.join("/", artifact.getName(), archiveName, entryName);
            String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString();
            String contents = IOUtils.toString(tis);
            result.add((new ProfileFactory() {

                @Override
                protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) {
                    profile.setContents(profile.getBaseContents());
                }

                @Override
                protected Profile getBaseProfile(String name, String version, String outputFile) {
                    return new Profile(name, version, outputFile, contents);
                }

                @Override
                protected boolean showEditWarning() {
                    return false;
                }

                @Override
                protected ArtifactService getArtifactService() {
                    return artifactService;
                }

                @Override
                public SpinnakerArtifact getArtifact() {
                    return artifact;
                }

                @Override
                protected String commentPrefix() {
                    return null;
                }
            }).getProfile(profileName, outputPath, deploymentConfiguration, null));
            profileEntry = tis.getNextEntry();
        }
        return result;
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e);
    }
}
Also used : SpinnakerArtifact(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerArtifact) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) ArrayList(java.util.ArrayList) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)

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