87e351be8a4b0d192367014ca0ac6ddf6e2389a0
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.openecomp.sdc;
24
25 import org.apache.commons.codec.binary.Base64;
26 import org.apache.commons.io.output.ByteArrayOutputStream;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipInputStream;
38
39 public class ZipUtil {
40
41     public static void main(String[] args) {
42
43         String zipFileName = "/src/test/resources/config/config.zip";
44
45         zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
46         zipFileName = "src/test/resources/config/config.zip";
47
48         Path path = Paths.get(zipFileName);
49
50         try {
51             byte[] zipAsBytes = Files.readAllBytes(path);
52             // encode to base
53
54             byte[] decodedMd5 = Base64.encodeBase64(zipAsBytes);
55             String decodedStr = new String(decodedMd5);
56
57             zipAsBytes = Base64.decodeBase64(decodedStr.getBytes());
58
59             // String str = new String(zipAsBytes);
60
61             // readZip(str.getBytes());
62             readZip(zipAsBytes);
63
64         } catch (IOException e) {
65             e.printStackTrace();
66         }
67
68     }
69
70     private static Map<String, byte[]> readZip(byte[] zipAsBytes) {
71
72         Map<String, byte[]> fileNameToByteArray = new HashMap<>();
73
74         byte[] buffer = new byte[1024];
75         ZipInputStream zis = null;
76         try {
77
78             zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
79             // get the zipped file list entry
80             ZipEntry ze = zis.getNextEntry();
81
82             while (ze != null) {
83
84                 String fileName = ze.getName();
85
86                 if (!ze.isDirectory()) {
87
88                     ByteArrayOutputStream os = new ByteArrayOutputStream();
89                     try {
90                         int len;
91                         while ((len = zis.read(buffer)) > 0) {
92                             os.write(buffer, 0, len);
93                         }
94
95                         // aClass.outputStreamMethod(os);
96                         String aString = new String(os.toByteArray(), "UTF-8");
97
98                         fileNameToByteArray.put(fileName, os.toByteArray());
99
100                     } finally {
101                         if (os != null) {
102                             os.close();
103                         }
104                     }
105                 }
106                 ze = zis.getNextEntry();
107
108             }
109
110             zis.closeEntry();
111             zis.close();
112
113         } catch (IOException ex) {
114             ex.printStackTrace();
115             return null;
116         } finally {
117             if (zis != null) {
118                 try {
119                     zis.closeEntry();
120                     zis.close();
121                 } catch (IOException e) {
122                     // TODO: add log
123                 }
124
125             }
126         }
127
128         return fileNameToByteArray;
129
130     }
131
132     private static byte[] loadResource(String resourceDir) throws IOException, URISyntaxException {
133
134         Path path = Paths.get(ZipUtil.class.getResource(resourceDir).toURI());
135         return Files.readAllBytes(path);
136     }
137
138     public static Map<String, byte[]> readData(String resourceDir) throws IOException, URISyntaxException {
139
140         byte[] data = loadResource(resourceDir);
141         return readZip(data);
142     }
143 }