2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.core.tools.util;
23 import com.google.common.io.ByteStreams;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
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;
36 public class ZipUtils {
38 private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
41 // prevent instantiation
44 public static void createZip(String zipFileName, Path dir) throws IOException {
45 File dirObj = dir.toFile();
46 Path zippedFile = Files.createFile(Paths.get(zipFileName));
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);
57 Utils.printMessage(logger, "Zip file was created " + zipFileName);
61 public static void unzip(Path zipFile, Path outputFolder) throws IOException {
62 if (zipFile == null || outputFolder == null) {
65 createDirectoryIfNotExists(outputFolder);
67 try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
68 ZipInputStream stream = new ZipInputStream(fileInputStream)) {
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());
78 persistFile(stream, newFile);
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);
92 private static void createDirectoryIfNotExists(Path path) throws IOException {
93 if (!path.toFile().exists()) {
94 Files.createDirectories(path);
98 private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
99 if (entry.getName().contains("../")) {
100 throw new ZipException("Path traversal attempt discovered.");