Search in sources :

Example 36 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project RecordManager2 by moravianlibrary.

the class TarGzUtils method extract.

public static void extract(File tarFile, File destFile) throws IOException {
    logger.info("Extracting file: " + tarFile.getName());
    if (destFile.exists())
        throw new FileAlreadyExistsException(destFile.getAbsolutePath());
    if (!destFile.mkdir())
        throw new IOException("Can't make directory for " + destFile.getAbsolutePath());
    TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));
    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    while (tarEntry != null) {
        // create a file with the same name as the tarEntry
        File destPath = new File(destFile, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!destPath.mkdirs())
                logger.info("Can't make directory for " + destPath.getAbsolutePath());
        } else {
            if (!destPath.createNewFile())
                logger.info("Name of file already exists " + destPath.getAbsolutePath());
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            byte[] btoRead = new byte[1024];
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }
            bout.close();
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 37 with TarArchiveEntry

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

the class BackupService method addFileToTar.

private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
    File file = new File(path);
    String fileName = file.getName();
    if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
        return;
    }
    String tarEntryName = String.join("/", base, fileName);
    try {
        if (file.isFile()) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
            tarArchiveOutputStream.putArchiveEntry(tarEntry);
            IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
            tarArchiveOutputStream.closeArchiveEntry();
        } else if (file.isDirectory()) {
            Arrays.stream(file.listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
        } else {
            log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName() + " to archive entry: " + tarEntryName + " " + e.getMessage(), e);
    }
}
Also used : Arrays(java.util.Arrays) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) BufferedOutputStream(java.io.BufferedOutputStream) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HalconfigParser(com.netflix.spinnaker.halyard.config.config.v1.HalconfigParser) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HalconfigDirectoryStructure(com.netflix.spinnaker.halyard.config.config.v1.HalconfigDirectoryStructure) File(java.io.File) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Paths(java.nio.file.Paths) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Objects(java.util.Objects) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 38 with TarArchiveEntry

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

the class GitProfileReader method readArchiveProfile.

@Override
public InputStream readArchiveProfile(String artifactName, String version, String profileName) throws IOException {
    Path profilePath = Paths.get(profilePath(artifactName, version, profileName));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);
    ArrayList<Path> filePathsToAdd = java.nio.file.Files.walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS).filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));
    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();
    }
    tarArchive.finish();
    tarArchive.close();
    return new ByteArrayInputStream(os.toByteArray());
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Files(java.nio.file.Files) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) Path(java.nio.file.Path) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 39 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project zalenium by zalando.

the class DockerSeleniumRemoteProxy method copyVideos.

@SuppressWarnings("ResultOfMethodCallIgnored")
@VisibleForTesting
void copyVideos(final String containerId) {
    boolean videoWasCopied = false;
    TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
    try {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
            testInformation.setFileExtension(fileExtension);
            File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
            File parent = videoFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            OutputStream outputStream = new FileOutputStream(videoFile);
            IOUtils.copy(tarStream, outputStream);
            outputStream.close();
            videoWasCopied = true;
            LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
        }
    } catch (IOException e) {
        // This error happens in k8s, but the file is ok, nevertheless the size is not accurate
        boolean isPipeClosed = e.getMessage().toLowerCase().contains("pipe closed");
        if (ContainerFactory.getIsKubernetes().get() && isPipeClosed) {
            LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
        } else {
            LOGGER.warn(getId() + " Error while copying the video", e);
        }
        ga.trackException(e);
    } finally {
        if (!videoWasCopied) {
            testInformation.setVideoRecorded(false);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 40 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project syndesis by syndesisio.

the class ProjectGeneratorTest method generate.

// *****************************
// Helpers
// *****************************
private Path generate(Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException {
    final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager, getMavenProperties());
    try (InputStream is = generator.generate(integration)) {
        Path ret = testFolder.newFolder("integration-project").toPath();
        try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
            TarArchiveEntry tarEntry = tis.getNextTarEntry();
            // tarIn is a TarArchiveInputStream
            while (tarEntry != null) {
                // create a file with the same name as the tarEntry
                File destPath = new File(ret.toFile(), tarEntry.getName());
                if (tarEntry.isDirectory()) {
                    destPath.mkdirs();
                } else {
                    destPath.getParentFile().mkdirs();
                    destPath.createNewFile();
                    try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
                        IOUtils.copy(tis, bout);
                    }
                }
                tarEntry = tis.getNextTarEntry();
            }
        }
        return ret;
    }
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IntegrationProjectGenerator(io.syndesis.integration.api.IntegrationProjectGenerator) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IntegrationProjectGenerator(io.syndesis.integration.api.IntegrationProjectGenerator) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)200 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)99 File (java.io.File)86 FileInputStream (java.io.FileInputStream)55 IOException (java.io.IOException)55 FileOutputStream (java.io.FileOutputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)38 InputStream (java.io.InputStream)31 BufferedInputStream (java.io.BufferedInputStream)29 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 OutputStream (java.io.OutputStream)17 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)15 HashMap (java.util.HashMap)11 GZIPInputStream (java.util.zip.GZIPInputStream)11