2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.openecomp.sdc;
25 import org.apache.commons.codec.binary.Base64;
26 import org.apache.commons.io.output.ByteArrayOutputStream;
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;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipInputStream;
39 public class ZipUtil {
41 public static void main(String[] args) {
43 String zipFileName = "/src/test/resources/config/config.zip";
45 zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
46 zipFileName = "src/test/resources/config/config.zip";
48 Path path = Paths.get(zipFileName);
51 byte[] zipAsBytes = Files.readAllBytes(path);
54 byte[] decodedMd5 = Base64.encodeBase64(zipAsBytes);
55 String decodedStr = new String(decodedMd5);
57 zipAsBytes = Base64.decodeBase64(decodedStr.getBytes());
59 // String str = new String(zipAsBytes);
61 // readZip(str.getBytes());
64 } catch (IOException e) {
70 private static Map<String, byte[]> readZip(byte[] zipAsBytes) {
72 Map<String, byte[]> fileNameToByteArray = new HashMap<>();
74 byte[] buffer = new byte[1024];
75 ZipInputStream zis = null;
78 zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
79 // get the zipped file list entry
80 ZipEntry ze = zis.getNextEntry();
84 String fileName = ze.getName();
86 if (!ze.isDirectory()) {
88 ByteArrayOutputStream os = new ByteArrayOutputStream();
91 while ((len = zis.read(buffer)) > 0) {
92 os.write(buffer, 0, len);
95 // aClass.outputStreamMethod(os);
96 String aString = new String(os.toByteArray(), "UTF-8");
98 fileNameToByteArray.put(fileName, os.toByteArray());
106 ze = zis.getNextEntry();
113 } catch (IOException ex) {
114 ex.printStackTrace();
121 } catch (IOException e) {
128 return fileNameToByteArray;
132 private static byte[] loadResource(String resourceDir) throws IOException, URISyntaxException {
134 Path path = Paths.get(ZipUtil.class.getResource(resourceDir).toURI());
135 return Files.readAllBytes(path);
138 public static Map<String, byte[]> readData(String resourceDir) throws IOException, URISyntaxException {
140 byte[] data = loadResource(resourceDir);
141 return readZip(data);