Merge "App onboarding fixes"
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / onap / portalapp / widget / utils / UnzipUtil.java
1 package org.onap.portalapp.widget.utils;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Stack;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
15
16 import org.onap.portalapp.widget.constant.WidgetConstant;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class UnzipUtil {
21
22         /**
23          * Size of the buffer to read/write data
24          */
25         private static final int BUFFER_SIZE = 4096;
26         private static final Logger logger = LoggerFactory.getLogger(UnzipUtil.class);
27
28         /**
29          * Extracts a zip file specified by the zipFilePath to a directory specified by
30          * destDirectory (will be created if does not exists)
31          * 
32          * @param zipFilePath
33          *            path
34          * @param destDirectory
35          *            directory
36          * @param widgetName
37          *            name
38          * @return Map of contents
39          * @throws IOException
40          *             On error
41          */
42         public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName)
43                         throws IOException {
44
45                 logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
46                 File destDir = new File(destDirectory);
47                 if (!destDir.exists())
48                         destDir.mkdir();
49                 try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)))
50                 {
51                         ZipEntry entry = zipIn.getNextEntry();
52                         Map<String, byte[]> map = new HashMap<>();
53         
54                         String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION };
55                         for (String k : requiredKeys)
56                                 map.put(k, null);
57         
58                         // iterates over entries in the zip file
59                         Stack<File> stack = new Stack<>();
60                         while (entry != null) {
61                                 String filePath = destDirectory + File.separator + widgetName + File.separator
62                                                 + entry.getName().substring(entry.getName().indexOf("/") + 1);
63                                 final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1);
64                                 logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName);
65                                 if (!entry.isDirectory()) {
66                                         // if the entry is a file, extracts it
67                                         logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath);
68                                         stack.push(new File(filePath));
69                                         extractFile(zipIn, filePath);
70                                 } else {
71                                         // if the entry is a directory, make the directory
72                                         logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath);
73                                         File dir = new File(filePath);
74                                         stack.push(new File(filePath));
75                                         dir.mkdir();
76                                 }
77                                 // Is this one we need?
78                                 if (map.containsKey(entryShortName))
79                                         map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
80                                 zipIn.closeEntry();
81                                 entry = zipIn.getNextEntry();
82                         }
83                 
84                         while (!stack.isEmpty())
85                                 stack.pop().delete();
86         
87                         for (String k : requiredKeys)
88                                 if (!map.containsKey(k))
89                                         logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k);
90                         
91                         return map;
92                 }
93         }
94
95         /**
96          * Extracts a zip entry (file entry)
97          * 
98          * @param zipIn
99          * @param filePath
100          * @throws IOException
101          */
102         private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
103                 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))){
104                         byte[] bytesIn = new byte[BUFFER_SIZE];
105                         int read = 0;
106                         while ((read = zipIn.read(bytesIn)) != -1) {
107                                 bos.write(bytesIn, 0, read);
108                         }
109                 }
110         }
111 }