aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-application-maven-plugin/src/main/java/com/yahoo/container/plugin/mojo/Compression.java
blob: f793df23558b7db6b1940a4034bfe2734b154539 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright Yahoo. 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.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author Tony Vaagenes
 */
public class Compression {

    public static void zipDirectory(File dir, String zipTopLevelDir) throws Exception {
        FileOutputStream zipFile = new FileOutputStream(new File(dir.getParent(), dir.getName() + ".zip"));
        ZipOutputStream zipOutputStream = new ZipOutputStream(zipFile);
        try {
            addDirectory(zipOutputStream, zipTopLevelDir, 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 {
            try (FileInputStream fileInput = new FileInputStream(child)) {
                fileInput.transferTo(zipOutputStream);
            }
        } finally {
            zipOutputStream.closeEntry();
        }
    }

    private static String composePath(String relativePath, String subDir) {
        return  relativePath.isEmpty() ?
                subDir :
                relativePath + File.separator + subDir;
    }

}