aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/util/ZipUtils.java
blob: c943c390342e7174f7410f0f500771a1fc82a0ec (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
package org.openecomp.core.tools.util;

import com.google.common.io.ByteStreams;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);

    private ZipUtils() {
        // prevent instantiation
    }

    public static void createZip(String zipFileName, Path dir) throws IOException {
        File dirObj = dir.toFile();
        Path zippedFile = Files.createFile(Paths.get(zipFileName));
        try (
                FileOutputStream fileOutputStream = new FileOutputStream(File.separator + zippedFile.toFile());
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
                ZipOutputStream out = new ZipOutputStream(bos)) {
            File[] files = dirObj.listFiles();
            for (File file : files) {
                out.putNextEntry(new ZipEntry(file.getName()));
                Files.copy(Paths.get(file.getPath()), out);
                out.closeEntry();
            }
            Utils.printMessage(logger, "Zip file was created " + zipFileName);
        }
    }

    public static void unzip(Path zipFile, Path outputFolder) throws IOException {
        if (zipFile == null || outputFolder == null) {
            return;
        }
        createDirectoryIfNotExists(outputFolder);

        try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
             ZipInputStream stream = new ZipInputStream(fileInputStream)) {

            ZipEntry entry;
            while ((entry = stream.getNextEntry()) != null) {
                assertEntryNotVulnerable(entry);
                String fileName = entry.getName();
                File newFile = new File(outputFolder.toString() + File.separator + fileName);
                if (entry.isDirectory()) {
                    createDirectoryIfNotExists(newFile.toPath());
                } else {
                    persistFile(stream, newFile);
                }
            }
        }

    }

    private static void persistFile(ZipInputStream stream, File newFile) throws IOException {
        new File(newFile.getParent()).mkdirs();
        try (FileOutputStream outputStream = new FileOutputStream(newFile)) {
            ByteStreams.copy(stream, outputStream);
        }
    }

    private static void createDirectoryIfNotExists(Path path) throws IOException {
        if (!path.toFile().exists()) {
            Files.createDirectories(path);
        }
    }

    private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
        if (entry.getName().contains("../")) {
            throw new ZipException("Path traversal attempt discovered.");
        }
    }
}