d699e5da40ea9f1a1dd0832725da1ef9891c7a97
[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                 ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
50                 ZipEntry entry = zipIn.getNextEntry();
51                 Map<String, byte[]> map = new HashMap<>();
52
53                 String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
54                                 WidgetConstant.WIDGET_STYLE_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                 zipIn.close();
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          * Extracts a zip entry (file entry)
96          * 
97          * @param zipIn
98          * @param filePath
99          * @throws IOException
100          */
101         private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
102                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
103                 byte[] bytesIn = new byte[BUFFER_SIZE];
104                 int read = 0;
105                 while ((read = zipIn.read(bytesIn)) != -1) {
106                         bos.write(bytesIn, 0, read);
107                 }
108                 bos.close();
109         }
110 }