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;
27 import java.io.ByteArrayInputStream;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.HashMap;
37 import java.util.zip.ZipEntry;
38 import java.util.zip.ZipInputStream;
39 import java.util.zip.ZipOutputStream;
41 public class ZipUtil {
43 private static final int KB = 1024;
44 private static Logger log = Logger.getLogger(ZipUtil.class.getName());
49 public static Map<String, byte[]> readZip(File file) {
50 try (InputStream fileInputStream = new FileInputStream(file)) {
51 return readZip(IOUtils.toByteArray(fileInputStream));
52 } catch (IOException e) {
53 log.info("close File stream failed - {}", e);
58 public static Map<String, byte[]> readZip(byte[] zipAsBytes) {
59 Map<String, byte[]> fileNameToByteArray = new HashMap<>();
60 byte[] buffer = new byte[KB];
61 try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipAsBytes);
62 ZipInputStream zis = new ZipInputStream(byteArrayInputStream)) {
63 // get the zipped file list entry
64 ZipEntry ze = zis.getNextEntry();
68 String fileName = ze.getName();
70 if (!ze.isDirectory()) {
72 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
74 while ((len = zis.read(buffer)) > 0) {
75 os.write(buffer, 0, len);
78 fileNameToByteArray.put(fileName, os.toByteArray());
82 ze = zis.getNextEntry();
84 } catch (IOException ex) {
85 log.info("close Byte stream failed", ex);
89 return fileNameToByteArray;
93 public static void main(String[] args) {
94 String zipFileName = "/src/test/resources/config/config.zip";
95 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
96 Path path = Paths.get(zipFileName);
99 byte[] zipAsBytes = Files.readAllBytes(path);
102 ZipUtil.readZip(zipAsBytes);
104 } catch (IOException e) {
105 log.info("close Byte stream failed", e);
109 public static byte[] zipBytes(byte[] input) throws IOException {
110 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
111 ZipOutputStream zos = new ZipOutputStream(baos)) {
112 ZipEntry entry = new ZipEntry("zip");
113 entry.setSize(input.length);
114 zos.putNextEntry(entry);
117 return baos.toByteArray();
121 public static byte[] unzip(byte[] zipped) {
122 try (ZipInputStream zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
123 ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
124 byte[] buf = new byte[KB];
125 ZipEntry zipentry = zipinputstream.getNextEntry();
127 while ((n = zipinputstream.read(buf, 0, KB)) > -1) {
128 outputStream.write(buf, 0, n);
130 return outputStream.toByteArray();
131 } catch (Exception e) {
132 throw new IllegalStateException("Can't unzip input stream", e);