Search in sources :

Example 61 with TarArchiveOutputStream

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

the class CommonCrawlDataDumper method constructNewStream.

private void constructNewStream(File outputDir) throws IOException {
    String archiveName = new SimpleDateFormat("yyyyMMddhhmm'.tar.gz'").format(new Date());
    LOG.info("Creating a new gzip archive: " + archiveName);
    fileOutput = new FileOutputStream(new File(outputDir + File.separator + archiveName));
    bufOutput = new BufferedOutputStream(fileOutput);
    gzipOutput = new GzipCompressorOutputStream(bufOutput);
    tarOutput = new TarArchiveOutputStream(gzipOutput);
    tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat) SequenceFile(org.apache.hadoop.io.SequenceFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Date(java.util.Date)

Example 62 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project winery by eclipse.

the class ScriptPlugin method compressFolderContents.

private void compressFolderContents(final String sourceDirectory, final String tarLocation) {
    try (FileOutputStream fos = new FileOutputStream(tarLocation);
        GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
        TarArchiveOutputStream tarOs = new TarArchiveOutputStream(gos)) {
        final File folder = new File(sourceDirectory);
        final File[] fileNames = folder.listFiles();
        if (fileNames != null) {
            for (final File file : fileNames) {
                addFilesToTarGZ(file, tarOs);
            }
        }
    } catch (final IOException e) {
        logger.error("Error while creating tar-ball", e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 63 with TarArchiveOutputStream

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

the class MeecrowaveBundleMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().warn(getClass().getSimpleName() + " skipped");
        return;
    }
    final File distroFolder = new File(buildDirectory, rootName == null ? artifactId + "-distribution" : rootName);
    if (distroFolder.exists()) {
        delete(distroFolder);
    }
    Stream.of("bin", "conf", "logs", "lib").forEach(i -> new File(distroFolder, i).mkdirs());
    copyProvidedFiles(distroFolder);
    write(new File(distroFolder, "logs/you_can_safely_delete.txt"), DELETE_TEXT);
    final Collection<String> includedArtifacts = project.getArtifacts().stream().filter(this::isIncluded).map(a -> {
        addLib(distroFolder, a.getFile());
        return a.getArtifactId();
    }).collect(toList());
    if (app.exists()) {
        addLib(distroFolder, app);
    }
    if (webapp != null && webapp.isDirectory()) {
        try {
            final Path rootSrc = webapp.toPath().toAbsolutePath();
            final Path rootTarget = distroFolder.toPath().toAbsolutePath().resolve("docBase");
            Files.walkFileTree(rootSrc, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    final Path target = rootTarget.resolve(rootSrc.relativize(file));
                    target.toFile().getParentFile().mkdirs();
                    Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING);
                    return super.visitFile(file, attrs);
                }
            });
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    if (enforceCommonsCli && !includedArtifacts.contains("commons-cli")) {
        addLib(distroFolder, resolve("commons-cli", "commons-cli", "1.4", ""));
    }
    if (libs != null) {
        libs.forEach(l -> {
            final boolean transitive = l.endsWith("?transitive");
            final String coords = transitive ? l.substring(0, l.length() - "?transitive".length()) : l;
            final String[] c = coords.split(":");
            if (c.length < 3 || c.length > 5) {
                throw new IllegalArgumentException("libs syntax is groupId:artifactId:version[:classifier][:type[?transitive]]");
            }
            if (!transitive) {
                addLib(distroFolder, resolve(c[0], c[1], c[2], c.length == 4 ? c[3] : ""));
            } else {
                addTransitiveDependencies(distroFolder, includedArtifacts, new Dependency() {

                    {
                        setGroupId(c[0]);
                        setArtifactId(c[1]);
                        setVersion(c[2]);
                        if (c.length == 4 && !"-".equals(c[3])) {
                            setClassifier(c[3]);
                        }
                        if (c.length == 5) {
                            setType(c[4]);
                        }
                    }
                });
            }
        });
    }
    if (enforceMeecrowave && !includedArtifacts.contains("meecrowave-core")) {
        addTransitiveDependencies(distroFolder, includedArtifacts, new Dependency() {

            {
                setGroupId("org.apache.meecrowave");
                setArtifactId("meecrowave-core");
                setVersion(findVersion());
            }
        });
    }
    for (final String ext : asList("sh", "bat")) {
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bin/meecrowave." + ext)))) {
            final File target = new File(distroFolder, "bin/meecrowave." + ext);
            if (!target.exists()) {
                write(target, new Substitutor(new HashMap<String, String>() {

                    {
                        put("main", main);
                        put("logManager", hasLog4j(distroFolder) ? "org.apache.logging.log4j.jul.LogManager" : "org.apache.juli.ClassLoaderLogManager");
                    }
                }).replace(reader.lines().collect(joining("\n"))));
            }
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    if (fakeTomcatScripts) {
        Stream.of("catalina.sh", "shutdown.sh", "startup.sh").forEach(script -> {
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bin/" + script)))) {
                final File target = new File(distroFolder, "bin/" + script);
                if (!target.exists()) {
                    write(target, reader.lines().collect(joining("\n")));
                }
            } catch (final IOException e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        });
    }
    final Path prefix = skipArchiveRootFolder ? distroFolder.toPath() : distroFolder.getParentFile().toPath();
    for (final String format : formats) {
        getLog().info(format + "-ing Custom Meecrowave Distribution");
        final File output = new File(buildDirectory, artifactId + "-meecrowave-distribution." + format);
        switch(format.toLowerCase(ENGLISH)) {
            case "tar.gz":
                try (final TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(output)))) {
                    tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                    for (final String entry : distroFolder.list()) {
                        tarGz(tarGz, new File(distroFolder, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                break;
            case "zip":
                try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(output))) {
                    for (final String entry : distroFolder.list()) {
                        zip(zos, new File(distroFolder, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                break;
            default:
                throw new IllegalArgumentException(format + " is not supported");
        }
        attach(format, output);
    }
    if (!keepExplodedFolder) {
        delete(distroFolder);
    }
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) Parameter(org.apache.maven.plugins.annotations.Parameter) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) MavenProject(org.apache.maven.project.MavenProject) Arrays.asList(java.util.Arrays.asList) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) Artifact(org.apache.maven.artifact.Artifact) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Path(java.nio.file.Path) ENGLISH(java.util.Locale.ENGLISH) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) Substitutor(org.apache.meecrowave.lang.Substitutor) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Stream(java.util.stream.Stream) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) GZIPOutputStream(java.util.zip.GZIPOutputStream) AbstractMojo(org.apache.maven.plugin.AbstractMojo) RepositorySystem(org.eclipse.aether.RepositorySystem) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) Dependency(org.apache.maven.model.Dependency) Component(org.apache.maven.plugins.annotations.Component) HashMap(java.util.HashMap) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) StandardCopyOption(java.nio.file.StandardCopyOption) Mojo(org.apache.maven.plugins.annotations.Mojo) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Log(org.apache.maven.plugin.logging.Log) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) InputStreamReader(java.io.InputStreamReader) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RUNTIME_PLUS_SYSTEM(org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME_PLUS_SYSTEM) Collectors.toList(java.util.stream.Collectors.toList) ArtifactResolver(org.eclipse.aether.impl.ArtifactResolver) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InputStreamReader(java.io.InputStreamReader) Substitutor(org.apache.meecrowave.lang.Substitutor) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Dependency(org.apache.maven.model.Dependency) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 64 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project docker-maven-plugin by fabric8io.

the class ImageArchiveUtilTest method readEmptyArchive.

@Test
public void readEmptyArchive() throws IOException {
    byte[] emptyTar;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
        tarOutput.finish();
        emptyTar = baos.toByteArray();
    }
    ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(emptyTar));
    Assert.assertNull(manifest);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) Test(org.junit.Test)

Example 65 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project docker-maven-plugin by fabric8io.

the class ImageArchiveUtilTest method readInvalidManifestInArchive.

@Test(expected = JsonParseException.class)
public void readInvalidManifestInArchive() throws IOException {
    byte[] archiveBytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
        final byte[] entryData = ("}" + UUID.randomUUID().toString() + "{").getBytes();
        TarArchiveEntry tarEntry = new TarArchiveEntry(ImageArchiveUtil.MANIFEST_JSON);
        tarEntry.setSize(entryData.length);
        tarOutput.putArchiveEntry(tarEntry);
        tarOutput.write(entryData);
        tarOutput.closeArchiveEntry();
        tarOutput.finish();
        archiveBytes = baos.toByteArray();
    }
    ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
    Assert.assertNull(manifest);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) Test(org.junit.Test)

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