exception is thrown on import normative
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / ZipUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.sdc.common.util;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.io.output.ByteArrayOutputStream;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
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.HashMap;
32 import java.util.Map;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipInputStream;
35 import java.util.zip.ZipOutputStream;
36
37 public class ZipUtil {
38
39         private static Logger log = Logger.getLogger(ZipUtil.class.getName());
40
41         private ZipUtil() {
42         }
43
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);
49             return null;
50         }
51     }
52
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();
60
61                         while (ze != null) {
62
63                                 String fileName = ze.getName();
64
65                                 if (!ze.isDirectory()) {
66
67                                         try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
68                                                 int len;
69                                                 while ((len = zis.read(buffer)) > 0) {
70                                                         os.write(buffer, 0, len);
71                                                 }
72
73                                                 fileNameToByteArray.put(fileName, os.toByteArray());
74
75                                         }
76                                 }
77                                 ze = zis.getNextEntry();
78                         }
79                 } catch (IOException ex) {
80                         log.info("close Byte stream failed" , ex);
81                         return null;
82                 }
83
84                 return fileNameToByteArray;
85
86         }
87
88         public static void main(String[] args) {
89
90                 String zipFileName = "/src/test/resources/config/config.zip";
91
92                 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
93
94                 Path path = Paths.get(zipFileName);
95
96                 try {
97                         byte[] zipAsBytes = Files.readAllBytes(path);
98                         // encode to base
99
100                         ZipUtil.readZip(zipAsBytes);
101
102                 } catch (IOException e) {
103                         log.info("close Byte stream failed" , e);
104                 }
105
106         }
107
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);
114                         zos.write(input);
115                         zos.closeEntry();
116                         return baos.toByteArray();
117                 }
118         }
119
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();
125                         int n;
126                         while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
127                                 outputStream.write(buf, 0, n);
128                         }
129                         return outputStream.toByteArray();
130                 } catch (Exception e) {
131                         throw new IllegalStateException("Can't unzip input stream", e);
132                 }
133         }
134
135 }