fixing warnings from checkstyle in common-app-api
[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.IOUtils;
24 import org.apache.commons.io.output.ByteArrayOutputStream;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.zip.ZipEntry;
38 import java.util.zip.ZipInputStream;
39 import java.util.zip.ZipOutputStream;
40
41 public class ZipUtil {
42
43     private static final int KB = 1024;
44     private static Logger log = Logger.getLogger(ZipUtil.class.getName());
45
46     private ZipUtil() {
47     }
48
49     public static Map<String, byte[]> readZip(File file) {
50         try (InputStream fileInputStream = new FileInputStream(file)) {
51             return readZip(IOUtils.toByteArray(fileInputStream));
52         } catch (IOException e) {
53             log.info("close File stream failed - {}", e);
54             return null;
55         }
56     }
57
58     public static Map<String, byte[]> readZip(byte[] zipAsBytes) {
59         Map<String, byte[]> fileNameToByteArray = new HashMap<>();
60         byte[] buffer = new byte[KB];
61         try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipAsBytes);
62              ZipInputStream zis = new ZipInputStream(byteArrayInputStream)) {
63             // get the zipped file list entry
64             ZipEntry ze = zis.getNextEntry();
65
66             while (ze != null) {
67
68                 String fileName = ze.getName();
69
70                 if (!ze.isDirectory()) {
71
72                     try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
73                         int len;
74                         while ((len = zis.read(buffer)) > 0) {
75                             os.write(buffer, 0, len);
76                         }
77
78                         fileNameToByteArray.put(fileName, os.toByteArray());
79
80                     }
81                 }
82                 ze = zis.getNextEntry();
83             }
84         } catch (IOException ex) {
85             log.info("close Byte stream failed", ex);
86             return null;
87         }
88
89         return fileNameToByteArray;
90
91     }
92
93     public static void main(String[] args) {
94         String zipFileName = "/src/test/resources/config/config.zip";
95         zipFileName = "C:\\Git_work\\D2-SDnC\\catalog-be\\src\\test\\resources\\config\\config.zip";
96         Path path = Paths.get(zipFileName);
97
98         try {
99             byte[] zipAsBytes = Files.readAllBytes(path);
100             // encode to base
101
102             ZipUtil.readZip(zipAsBytes);
103
104         } catch (IOException e) {
105             log.info("close Byte stream failed", e);
106         }
107     }
108
109     public static byte[] zipBytes(byte[] input) throws IOException {
110         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
111              ZipOutputStream zos = new ZipOutputStream(baos)) {
112             ZipEntry entry = new ZipEntry("zip");
113             entry.setSize(input.length);
114             zos.putNextEntry(entry);
115             zos.write(input);
116             zos.closeEntry();
117             return baos.toByteArray();
118         }
119     }
120
121     public static byte[] unzip(byte[] zipped) {
122         try (ZipInputStream zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped));
123              ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
124             byte[] buf = new byte[KB];
125             ZipEntry zipentry = zipinputstream.getNextEntry();
126             int n;
127             while ((n = zipinputstream.read(buf, 0, KB)) > -1) {
128                 outputStream.write(buf, 0, n);
129             }
130             return outputStream.toByteArray();
131         } catch (Exception e) {
132             throw new IllegalStateException("Can't unzip input stream", e);
133         }
134     }
135 }