[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-widget-ms / src / main / java / org / openecomp / portalapp / widget / utils / UnzipUtil.java
1 package org.openecomp.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.openecomp.portalapp.widget.constant.WidgetConstant;
17 import org.openecomp.portalapp.widget.service.impl.StorageServiceImpl;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
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      * Extracts a zip file specified by the zipFilePath to a directory specified by
29      * destDirectory (will be created if does not exists)
30      * @param zipFilePath
31      * @param destDirectory
32      * @throws IOException
33      */
34
35     public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName) throws IOException {
36         
37         logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
38         File destDir = new File(destDirectory);
39         if (!destDir.exists()) {
40             destDir.mkdir();
41         }
42         ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
43         ZipEntry entry = zipIn.getNextEntry();
44         Map<String, byte[]> map = new HashMap<>();
45
46         map.put(WidgetConstant.WIDGET_CONTROLLER_LOCATION, null);
47         map.put(WidgetConstant.WIDGET_MARKUP_LOCATION, null);
48         map.put(WidgetConstant.WIDGET_STYLE_LOCATION, null);
49
50         
51         Stack<File> stack = new Stack<>();
52         
53         // iterates over entries in the zip file
54         while (entry != null) { 
55             String filePath = destDirectory + File.separator + widgetName + File.separator +
56                         entry.getName().substring(entry.getName().indexOf("/")+1);
57             logger.debug("UnzipUtil.unzip_db: file path " + filePath);
58             if (!entry.isDirectory()) {
59                 // if the entry is a file, extracts it
60                 logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath);
61                 stack.push(new File(filePath));
62                 extractFile(zipIn, filePath);
63             } else {
64                 // if the entry is a directory, make the directory
65                 logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath);
66                 File dir = new File(filePath);
67                 stack.push(new File(filePath));
68                 
69                 dir.mkdir();   
70
71             }
72             if(map.containsKey(entry.getName().substring(entry.getName().indexOf("/")+1))){     
73                 map.put(entry.getName().substring(entry.getName().indexOf("/")+1), Files.readAllBytes(Paths.get(filePath)));
74             }
75             zipIn.closeEntry();
76             entry = zipIn.getNextEntry();
77         }
78         zipIn.close();
79         while(!stack.isEmpty())
80                 stack.pop().delete();
81         return map;
82     }
83     
84     
85     
86     
87     /**
88      * Extracts a zip entry (file entry)
89      * @param zipIn
90      * @param filePath
91      * @throws IOException
92      */
93     private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
94         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
95         byte[] bytesIn = new byte[BUFFER_SIZE];
96         int read = 0;
97         while ((read = zipIn.read(bytesIn)) != -1) {
98             bos.write(bytesIn, 0, read);
99         }
100         bos.close();
101     }
102 }