Search in sources :

Example 41 with TarArchiveOutputStream

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

the class BackupService method tarHalconfig.

private void tarHalconfig(String halconfigDir, String halconfigTar) throws IOException {
    FileOutputStream tarOutput = null;
    BufferedOutputStream bufferedTarOutput = null;
    TarArchiveOutputStream tarArchiveOutputStream = null;
    IOException fatalCleanup = null;
    try {
        tarOutput = new FileOutputStream(new File(halconfigTar));
        bufferedTarOutput = new BufferedOutputStream(tarOutput);
        tarArchiveOutputStream = new TarArchiveOutputStream(bufferedTarOutput);
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        TarArchiveOutputStream finalTarArchiveOutputStream = tarArchiveOutputStream;
        Arrays.stream(new File(halconfigDir).listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(finalTarArchiveOutputStream, f.getAbsolutePath(), ""));
    } catch (HalException e) {
        log.info("HalException caught during tar operation", e);
        throw e;
    } catch (IOException e) {
        log.info("IOException caught during tar operation", e);
        throw new HalException(Problem.Severity.FATAL, "Failed to backup halconfig: " + e.getMessage(), e);
    } finally {
        if (tarArchiveOutputStream != null) {
            try {
                tarArchiveOutputStream.finish();
                tarArchiveOutputStream.close();
            } catch (IOException e) {
                fatalCleanup = e;
            }
        }
        if (bufferedTarOutput != null) {
            bufferedTarOutput.close();
        }
        if (tarOutput != null) {
            tarOutput.close();
        }
    }
    if (fatalCleanup != null) {
        throw fatalCleanup;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) LocalFile(com.netflix.spinnaker.halyard.config.model.v1.node.LocalFile) File(java.io.File)

Example 42 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream 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 : java.util(java.util) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) LocalFile(com.netflix.spinnaker.halyard.config.model.v1.node.LocalFile) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) StringUtils(org.apache.commons.lang3.StringUtils) 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) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) Path(java.nio.file.Path) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) FileService(com.netflix.spinnaker.halyard.config.services.v1.FileService) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) HalconfigDirectoryStructure(com.netflix.spinnaker.halyard.config.config.v1.HalconfigDirectoryStructure) File(java.io.File) Consumer(java.util.function.Consumer) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) LocalFile(com.netflix.spinnaker.halyard.config.model.v1.node.LocalFile) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 43 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream 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());
        int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path));
        permissions = FileModeUtils.setFileBit(permissions);
        tarEntry.setMode(permissions);
        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) FileModeUtils(com.netflix.spinnaker.halyard.core.FileModeUtils) 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 44 with TarArchiveOutputStream

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

the class LocalDiskProfileReader method readArchiveProfileFrom.

public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {
    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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) 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 45 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project mycore by MyCoRe-Org.

the class MCRTarServlet method createContainer.

@Override
protected TarArchiveOutputStream createContainer(ServletOutputStream sout, String comment) {
    LOGGER.info("Constructing tar archive: {}", comment);
    TarArchiveOutputStream tout = new TarArchiveOutputStream(sout, "UTF8");
    tout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    return tout;
}
Also used : TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7