[SDC-29] rebase continue work to align source
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / 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;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.io.output.ByteArrayOutputStream;
37
38 public class ZipUtil {
39
40         public static void main(String[] args) {
41
42                 String zipFileName = "/src/test/resources/config/config.zip";
43
44                 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
45                 zipFileName = "src/test/resources/config/config.zip";
46
47                 Path path = Paths.get(zipFileName);
48
49                 try {
50                         byte[] zipAsBytes = Files.readAllBytes(path);
51                         // encode to base
52
53                         byte[] decodedMd5 = Base64.encodeBase64(zipAsBytes);
54                         String decodedStr = new String(decodedMd5);
55
56                         zipAsBytes = Base64.decodeBase64(decodedStr.getBytes());
57
58                         // String str = new String(zipAsBytes);
59
60                         // readZip(str.getBytes());
61                         readZip(zipAsBytes);
62
63                 } catch (IOException e) {
64                         e.printStackTrace();
65                 }
66
67         }
68
69         private static Map<String, byte[]> readZip(byte[] zipAsBytes) {
70
71                 Map<String, byte[]> fileNameToByteArray = new HashMap<String, byte[]>();
72
73                 byte[] buffer = new byte[1024];
74                 ZipInputStream zis = null;
75                 try {
76
77                         zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
78                         // get the zipped file list entry
79                         ZipEntry ze = zis.getNextEntry();
80
81                         while (ze != null) {
82
83                                 String fileName = ze.getName();
84
85                                 if (false == ze.isDirectory()) {
86
87                                         ByteArrayOutputStream os = new ByteArrayOutputStream();
88                                         try {
89                                                 int len;
90                                                 while ((len = zis.read(buffer)) > 0) {
91                                                         os.write(buffer, 0, len);
92                                                 }
93
94                                                 // aClass.outputStreamMethod(os);
95                                                 String aString = new String(os.toByteArray(), "UTF-8");
96
97                                                 fileNameToByteArray.put(fileName, os.toByteArray());
98
99                                         } finally {
100                                                 if (os != null) {
101                                                         os.close();
102                                                 }
103                                         }
104                                 }
105                                 ze = zis.getNextEntry();
106
107                         }
108
109                         zis.closeEntry();
110                         zis.close();
111
112                 } catch (IOException ex) {
113                         ex.printStackTrace();
114                         return null;
115                 } finally {
116                         if (zis != null) {
117                                 try {
118                                         zis.closeEntry();
119                                         zis.close();
120                                 } catch (IOException e) {
121                                         // TODO: add log
122                                 }
123
124                         }
125                 }
126
127                 return fileNameToByteArray;
128
129         }
130
131 }