re base code
[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.output.ByteArrayOutputStream;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
25
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;
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(byte[] zipAsBytes) {
45
46                 ZipInputStream zis = null;
47                 zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
48
49                 return readZip(zis);
50         }
51
52         public static Map<String, byte[]> readZip(ZipInputStream zis) {
53
54                 Map<String, byte[]> fileNameToByteArray = new HashMap<>();
55
56                 byte[] buffer = new byte[1024];
57                 try {
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                                         ByteArrayOutputStream os = new ByteArrayOutputStream();
68                                         try {
69                                                 int len;
70                                                 while ((len = zis.read(buffer)) > 0) {
71                                                         os.write(buffer, 0, len);
72                                                 }
73
74                                                 fileNameToByteArray.put(fileName, os.toByteArray());
75
76                                         } finally {
77                                                 if (os != null) {
78                                                         os.close();
79                                                 }
80                                         }
81                                 }
82                                 ze = zis.getNextEntry();
83
84                         }
85
86                         zis.closeEntry();
87                         zis.close();
88
89                 } catch (IOException ex) {
90                         
91                         log.info("close Byte stream failed - {}" , ex);
92                         return null;
93                 } finally {
94                         if (zis != null) {
95                                 try {
96                                         zis.closeEntry();
97                                         zis.close();
98                                 } catch (IOException e) {
99                                         // TODO: add log
100                                 }
101
102                         }
103                 }
104
105                 return fileNameToByteArray;
106
107         }
108
109         public static void main(String[] args) {
110
111                 String zipFileName = "/src/test/resources/config/config.zip";
112
113                 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
114
115                 Path path = Paths.get(zipFileName);
116
117                 try {
118                         byte[] zipAsBytes = Files.readAllBytes(path);
119                         // encode to base
120
121                         ZipUtil.readZip(zipAsBytes);
122
123                 } catch (IOException e) {
124                         log.info("close Byte stream failed - {}" , e);
125                 }
126
127         }
128
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);
135                 zos.write(input);
136                 zos.closeEntry();
137                 zos.close();
138                 return baos.toByteArray();
139         }
140
141         public static byte[] unzip(byte[] zipped) {
142                 ZipInputStream zipinputstream = null;
143                 ByteArrayOutputStream outputStream = null;
144                 try {
145                         byte[] buf = new byte[1024];
146
147                         zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
148                         ZipEntry zipentry = zipinputstream.getNextEntry();
149                         outputStream = new ByteArrayOutputStream();
150                         int n;
151                         while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
152                                 outputStream.write(buf, 0, n);
153                         }
154
155                         return outputStream.toByteArray();
156                 } catch (Exception e) {
157                         throw new IllegalStateException("Can't unzip input stream", e);
158                 } finally {
159                         if (outputStream != null) {
160                                 try {
161                                         outputStream.close();
162                                 } catch (IOException e) {
163                                         log.debug("Failed to close output stream", e);
164                                 }
165                         }
166                         if (zipinputstream != null) {
167                                 try {
168                                         zipinputstream.closeEntry();
169                                         zipinputstream.close();
170                                 } catch (IOException e) {
171                                         log.debug("Failed to close zip input stream", e);
172                                 }
173                         }
174                 }
175         }
176
177 }