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.output.ByteArrayOutputStream;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
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(byte[] zipAsBytes) {
46 ZipInputStream zis = null;
47 zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
52 public static Map<String, byte[]> readZip(ZipInputStream zis) {
54 Map<String, byte[]> fileNameToByteArray = new HashMap<>();
56 byte[] buffer = new byte[1024];
58 // get the zipped file list entry
59 ZipEntry ze = zis.getNextEntry();
63 String fileName = ze.getName();
65 if (!ze.isDirectory()) {
67 ByteArrayOutputStream os = new ByteArrayOutputStream();
70 while ((len = zis.read(buffer)) > 0) {
71 os.write(buffer, 0, len);
74 fileNameToByteArray.put(fileName, os.toByteArray());
82 ze = zis.getNextEntry();
89 } catch (IOException ex) {
91 log.info("close Byte stream failed - {}" , ex);
98 } catch (IOException e) {
105 return fileNameToByteArray;
109 public static void main(String[] args) {
111 String zipFileName = "/src/test/resources/config/config.zip";
113 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
115 Path path = Paths.get(zipFileName);
118 byte[] zipAsBytes = Files.readAllBytes(path);
121 ZipUtil.readZip(zipAsBytes);
123 } catch (IOException e) {
124 log.info("close Byte stream failed - {}" , e);
129 public static byte[] zipBytes(byte[] input) throws IOException {
130 ByteArrayOutputStream baos = new ByteArrayOutputStream();
131 ZipOutputStream zos = new ZipOutputStream(baos);
132 ZipEntry entry = new ZipEntry("zip");
133 entry.setSize(input.length);
134 zos.putNextEntry(entry);
138 return baos.toByteArray();
141 public static byte[] unzip(byte[] zipped) {
142 ZipInputStream zipinputstream = null;
143 ByteArrayOutputStream outputStream = null;
145 byte[] buf = new byte[1024];
147 zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
148 ZipEntry zipentry = zipinputstream.getNextEntry();
149 outputStream = new ByteArrayOutputStream();
151 while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
152 outputStream.write(buf, 0, n);
155 return outputStream.toByteArray();
156 } catch (Exception e) {
157 throw new IllegalStateException("Can't unzip input stream", e);
159 if (outputStream != null) {
161 outputStream.close();
162 } catch (IOException e) {
163 log.debug("Failed to close output stream", e);
166 if (zipinputstream != null) {
168 zipinputstream.closeEntry();
169 zipinputstream.close();
170 } catch (IOException e) {
171 log.debug("Failed to close zip input stream", e);