Search in sources :

Example 1 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project Xponents by OpenSextant.

the class ArchiveNavigator method untar.

/*
     * Un-TAR Once items are saved off to temp folder, they'll be converted by
     * the file converter. The converter can choose to do something else with
     * them.
     */
public File untar(File tarFile) throws IOException, ConfigException {
    String _working = FilenameUtils.concat(getWorkingDir(), FilenameUtils.getBaseName(tarFile.getPath()));
    if (_working == null) {
        throw new IOException("Invalid archive path for " + tarFile.getPath());
    }
    File workingDir = new File(_working);
    workingDir.mkdir();
    InputStream input = new BufferedInputStream(new FileInputStream(tarFile));
    TarArchiveInputStream in = null;
    try {
        in = (TarArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("tar", input));
        TarArchiveEntry tarEntry;
        while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) {
            if (filterEntry(tarEntry)) {
                continue;
            }
            try {
                File tmpFile = saveArchiveEntry(tarEntry, in, _working);
                converter.convert(tmpFile);
            } catch (IOException err) {
                log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err);
            }
        }
    } catch (ArchiveException ae) {
        throw new IOException(ae);
    } finally {
        in.close();
    }
    return workingDir;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Example 2 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException 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 3 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException 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 4 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException 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)

Example 5 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project onebusaway-application-modules by camsys.

the class SyncBundleAction method unzipBundle.

private void unzipBundle(String tmpDir, String bundleFileName) throws IOException {
    byte[] buffer = new byte[1024];
    GZIPInputStream zipIn = new GZIPInputStream(new FileInputStream(tmpDir + File.separator + bundleFileName));
    FileOutputStream out = new FileOutputStream(tmpDir + File.separator + "unzippedBundle");
    int len;
    while ((len = zipIn.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    zipIn.close();
    out.close();
    // Now to untar the unzipped file
    File tarFile = new File(tmpDir + File.separator + "unzippedBundle");
    File untarredFile = new File(tmpDir + File.separator + "untarredBundle");
    // File untarredFile = new File(tmpDir);
    try {
        List<File> fileList = (new FileUtility()).unTar(tarFile, untarredFile);
    } catch (ArchiveException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileOutputStream(java.io.FileOutputStream) FileUtility(org.onebusaway.util.FileUtility) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ArchiveException (org.apache.commons.compress.archivers.ArchiveException)13 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)8 IOException (java.io.IOException)6 File (java.io.File)5 BufferedInputStream (java.io.BufferedInputStream)4 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)3 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)3 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)3 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)3 CompressorException (org.apache.commons.compress.compressors.CompressorException)3 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)1 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)1