Search in sources :

Example 1 with ZipOutputStream

use of org.codehaus.plexus.archiver.zip.ZipOutputStream in project jib by google.

the class MavenProjectPropertiesTest method zipUpDirectory.

private static Path zipUpDirectory(Path sourceRoot, Path targetZip) throws IOException {
    try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(targetZip))) {
        for (Path source : new DirectoryWalker(sourceRoot).filterRoot().walk()) {
            StringJoiner pathJoiner = new StringJoiner("/", "", "");
            sourceRoot.relativize(source).forEach(element -> pathJoiner.add(element.toString()));
            String zipEntryPath = Files.isDirectory(source) ? pathJoiner.toString() + '/' : pathJoiner.toString();
            ZipEntry entry = new ZipEntry(zipEntryPath);
            zipOut.putNextEntry(entry);
            if (!Files.isDirectory(source)) {
                try (InputStream in = Files.newInputStream(source)) {
                    ByteStreams.copy(in, zipOut);
                }
            }
            zipOut.closeEntry();
        }
    }
    return targetZip;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ZipOutputStream(org.codehaus.plexus.archiver.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(org.codehaus.plexus.archiver.zip.ZipEntry) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) StringJoiner(java.util.StringJoiner)

Example 2 with ZipOutputStream

use of org.codehaus.plexus.archiver.zip.ZipOutputStream in project jib by GoogleContainerTools.

the class MavenProjectPropertiesTest method zipUpDirectory.

private static Path zipUpDirectory(Path sourceRoot, Path targetZip) throws IOException {
    try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(targetZip))) {
        for (Path source : new DirectoryWalker(sourceRoot).filterRoot().walk()) {
            StringJoiner pathJoiner = new StringJoiner("/", "", "");
            sourceRoot.relativize(source).forEach(element -> pathJoiner.add(element.toString()));
            String zipEntryPath = Files.isDirectory(source) ? pathJoiner.toString() + '/' : pathJoiner.toString();
            ZipEntry entry = new ZipEntry(zipEntryPath);
            zipOut.putNextEntry(entry);
            if (!Files.isDirectory(source)) {
                try (InputStream in = Files.newInputStream(source)) {
                    ByteStreams.copy(in, zipOut);
                }
            }
            zipOut.closeEntry();
        }
    }
    return targetZip;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ZipOutputStream(org.codehaus.plexus.archiver.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(org.codehaus.plexus.archiver.zip.ZipEntry) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) StringJoiner(java.util.StringJoiner)

Aggregations

AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)2 DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 StringJoiner (java.util.StringJoiner)2 ZipEntry (org.codehaus.plexus.archiver.zip.ZipEntry)2 ZipOutputStream (org.codehaus.plexus.archiver.zip.ZipOutputStream)2