2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.sdc.common.util;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.io.output.ByteArrayOutputStream;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.HashMap;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipInputStream;
35 import java.util.zip.ZipOutputStream;
37 public class ZipUtil {
39 private static Logger log = Logger.getLogger(ZipUtil.class.getName());
44 public static Map<String, byte[]> readZip(File file) {
45 try(InputStream fileInputStream = new FileInputStream(file)){
46 return readZip(IOUtils.toByteArray(fileInputStream));
47 } catch (IOException e) {
48 log.info("close File stream failed - {}" , e);
53 public static Map<String, byte[]> readZip(byte[] zipAsBytes) {
54 Map<String, byte[]> fileNameToByteArray = new HashMap<>();
55 byte[] buffer = new byte[1024];
56 try(ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipAsBytes);
57 ZipInputStream zis = new ZipInputStream(byteArrayInputStream)) {
58 // get the zipped file list entry
59 ZipEntry ze = zis.getNextEntry();
63 String fileName = ze.getName();
65 if (!ze.isDirectory()) {
67 try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
69 while ((len = zis.read(buffer)) > 0) {
70 os.write(buffer, 0, len);
73 fileNameToByteArray.put(fileName, os.toByteArray());
77 ze = zis.getNextEntry();
79 } catch (IOException ex) {
80 log.info("close Byte stream failed" , ex);
84 return fileNameToByteArray;
88 public static void main(String[] args) {
90 String zipFileName = "/src/test/resources/config/config.zip";
92 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
94 Path path = Paths.get(zipFileName);
97 byte[] zipAsBytes = Files.readAllBytes(path);
100 ZipUtil.readZip(zipAsBytes);
102 } catch (IOException e) {
103 log.info("close Byte stream failed" , e);
108 public static byte[] zipBytes(byte[] input) throws IOException {
109 try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
110 ZipOutputStream zos = new ZipOutputStream(baos)){
111 ZipEntry entry = new ZipEntry("zip");
112 entry.setSize(input.length);
113 zos.putNextEntry(entry);
116 return baos.toByteArray();
120 public static byte[] unzip(byte[] zipped) {
121 try(ZipInputStream zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
122 ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
123 byte[] buf = new byte[1024];
124 ZipEntry zipentry = zipinputstream.getNextEntry();
126 while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
127 outputStream.write(buf, 0, n);
129 return outputStream.toByteArray();
130 } catch (Exception e) {
131 throw new IllegalStateException("Can't unzip input stream", e);