diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java |
Publish
Diffstat (limited to 'vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java')
-rw-r--r-- | vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java b/vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java new file mode 100644 index 00000000000..0880b567343 --- /dev/null +++ b/vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java @@ -0,0 +1,68 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.mojo; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * @author tonytv + */ +public class Compression { + static public void zipDirectory(File dir) throws Exception { + FileOutputStream zipFile = new FileOutputStream(new File(dir.getParent(), dir.getName() + ".zip")); + ZipOutputStream zipOutputStream = new ZipOutputStream(zipFile); + try { + addDirectory(zipOutputStream, dir.getName(), dir, ""); + } finally { + zipOutputStream.close(); + } + } + + private static void addDirectory(ZipOutputStream zipOutputStream, String zipTopLevelDir, File baseDir, String relativePath) throws IOException { + File currentDir = new File(baseDir, relativePath); + + for (File child : currentDir.listFiles()) { + if (child.isDirectory()) { + addDirectory(zipOutputStream, zipTopLevelDir, baseDir, composePath(relativePath, child.getName())); + } else { + addFile(zipOutputStream, zipTopLevelDir, relativePath, child); + } + } + } + + private static void addFile(ZipOutputStream zipOutputStream, String zipTopLevelDir, String relativePath, File child) throws IOException { + ZipEntry entry = new ZipEntry(composePath(zipTopLevelDir, composePath(relativePath, child.getName()))); + zipOutputStream.putNextEntry(entry); + try { + FileInputStream fileInput = new FileInputStream(child); + try { + copyBytes(fileInput, zipOutputStream); + } finally { + fileInput.close(); + } + } finally { + zipOutputStream.closeEntry(); + } + } + + public static void copyBytes(InputStream input, OutputStream output) throws IOException { + byte[] b = new byte[1024]; + int numRead = 0; + + while((numRead = input.read(b)) != -1) { + output.write(b, 0, numRead); + } + } + + private static String composePath(String relativePath, String subDir) { + return relativePath.isEmpty() ? + subDir : + relativePath + File.separator + subDir; + } +} |