Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / util / ZipUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.tools.util;
22
23 import com.google.common.io.ByteStreams;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26
27 import java.io.*;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipException;
33 import java.util.zip.ZipInputStream;
34 import java.util.zip.ZipOutputStream;
35
36 public class ZipUtils {
37
38     private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
39
40     private ZipUtils() {
41         // prevent instantiation
42     }
43
44     public static void createZip(String zipFileName, Path dir) throws IOException {
45         File dirObj = dir.toFile();
46         Path zippedFile = Files.createFile(Paths.get(zipFileName));
47         try (
48                 FileOutputStream fileOutputStream = new FileOutputStream(File.separator + zippedFile.toFile());
49                 BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
50                 ZipOutputStream out = new ZipOutputStream(bos)) {
51             File[] files = dirObj.listFiles();
52             for (File file : files) {
53                 out.putNextEntry(new ZipEntry(file.getName()));
54                 Files.copy(Paths.get(file.getPath()), out);
55                 out.closeEntry();
56             }
57             Utils.printMessage(logger, "Zip file was created " + zipFileName);
58         }
59     }
60
61     public static void unzip(Path zipFile, Path outputFolder) throws IOException {
62         if (zipFile == null || outputFolder == null) {
63             return;
64         }
65         createDirectoryIfNotExists(outputFolder);
66
67         try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
68              ZipInputStream stream = new ZipInputStream(fileInputStream)) {
69
70             ZipEntry entry;
71             while ((entry = stream.getNextEntry()) != null) {
72                 assertEntryNotVulnerable(entry);
73                 String fileName = entry.getName();
74                 File newFile = new File(outputFolder.toString() + File.separator + fileName);
75                 if (entry.isDirectory()) {
76                     createDirectoryIfNotExists(newFile.toPath());
77                 } else {
78                     persistFile(stream, newFile);
79                 }
80             }
81         }
82
83     }
84
85     private static void persistFile(ZipInputStream stream, File newFile) throws IOException {
86         new File(newFile.getParent()).mkdirs();
87         try (FileOutputStream outputStream = new FileOutputStream(newFile)) {
88             ByteStreams.copy(stream, outputStream);
89         }
90     }
91
92     private static void createDirectoryIfNotExists(Path path) throws IOException {
93         if (!path.toFile().exists()) {
94             Files.createDirectories(path);
95         }
96     }
97
98     private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
99         if (entry.getName().contains("../")) {
100             throw new ZipException("Path traversal attempt discovered.");
101         }
102     }
103 }
104