aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/util/ZipUtils.java
blob: 6447f85ea7dd131f291f0f72b2fb77a92c12dfb7 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.openecomp.core.tools.util;

import com.google.common.io.ByteStreams;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    public static void createZip(String zipFileName, Path dir, String filterItem) throws Exception {
        File dirObj = dir.toFile();
        try (
                FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);
                ZipOutputStream out = new ZipOutputStream(fileOutputStream)) {
            addDir(dirObj, out, dir.toString(), filterItem);
        }
    }

    public static final String cleanStr(String inFilterStr) {
        if (Objects.isNull(inFilterStr)) {
            return inFilterStr;
        }
        Scanner scan = new Scanner(inFilterStr);
        while (scan.hasNextLine()) {
            inFilterStr = scan.nextLine().replaceAll("[^a-zA-Z0-9]", "");
        }
        return inFilterStr;
    }

    static void addDir(File dirObj, ZipOutputStream out, String root, String filterItem) throws IOException {
        File[] files = dirObj.listFiles();
        filterItem = cleanStr(filterItem);

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                addDir(files[i], out, root, filterItem);
                continue;
            }
            try (FileInputStream in = new FileInputStream((files[i].getAbsolutePath()))) {
                String filePath = files[i].getAbsolutePath().replace(root + File.separator, "");
                if (filterItem == null || filePath.contains(filterItem)) {
                    out.putNextEntry(new ZipEntry(filePath));
                    try {
                        ByteStreams.copy(in, out);

                    } finally {
                        out.closeEntry();
                    }
                }

            }
        }
    }

    public static void unzip(Path zipFile, Path outputFolder) throws IOException {
        if (zipFile == null || outputFolder == null) {
            return;
        }
        if (!Files.exists(outputFolder)) {
            Files.createDirectories(outputFolder);
        }

        try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
             ZipInputStream zis = new ZipInputStream(fileInputStream)) {
            ZipEntry ze = zis.getNextEntry();
            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(outputFolder.toString() + File.separator + fileName);
                new File(newFile.getParent()).mkdirs();
                try (FileOutputStream fos = new FileOutputStream(newFile)) {
                    ByteStreams.copy(zis, fos);
                }
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
        }

    }
}