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