1 package org.onap.portalapp.widget.utils;
3 import java.io.BufferedOutputStream;
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;
12 import java.util.Stack;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
16 import org.onap.portalapp.widget.constant.WidgetConstant;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class UnzipUtil {
23 * Size of the buffer to read/write data
25 private static final int BUFFER_SIZE = 4096;
26 private static final Logger logger = LoggerFactory.getLogger(UnzipUtil.class);
29 * Extracts a zip file specified by the zipFilePath to a directory specified by
30 * destDirectory (will be created if does not exists)
34 * @param destDirectory
38 * @return Map of contents
42 public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName)
45 logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
46 File destDir = new File(destDirectory);
47 if (!destDir.exists())
49 ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
50 ZipEntry entry = zipIn.getNextEntry();
51 Map<String, byte[]> map = new HashMap<>();
53 String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
54 WidgetConstant.WIDGET_STYLE_LOCATION };
55 for (String k : requiredKeys)
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);
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));
77 // Is this one we need?
78 if (map.containsKey(entryShortName))
79 map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
81 entry = zipIn.getNextEntry();
84 while (!stack.isEmpty())
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);
95 * Extracts a zip entry (file entry)
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];
105 while ((read = zipIn.read(bytesIn)) != -1) {
106 bos.write(bytesIn, 0, read);