Code improvements in Common Utils
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / UnZip.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.emsdriver.commons.utils;
17
18 import org.apache.tools.zip.ZipEntry;
19 import org.apache.tools.zip.ZipFile;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Enumeration;
26
27
28 public class UnZip {
29     protected String deCompressPath = null;
30     protected String zipFilePath = null;
31
32     public UnZip(String zipFilePath, String toPath) throws IOException {
33         File zipFile = new File(
34                 new File(zipFilePath).getAbsolutePath());
35         if (!zipFile.isFile())
36             throw new IOException("not found file '" + zipFilePath + "'");
37
38         this.deCompressPath = toPath;
39         this.zipFilePath = zipFile.getAbsolutePath();
40
41         if (deCompressPath == null)
42             deCompressPath = zipFile.getParent() + File.separator;
43
44         else if (deCompressPath.charAt(deCompressPath.length() - 1) != '/')
45             deCompressPath = deCompressPath + File.separator;
46     }
47
48     public void deCompress() throws IOException {
49         try(ZipFile zipFile = new ZipFile(zipFilePath)){
50             Enumeration<ZipEntry> e = zipFile.getEntries();
51             for (ZipEntry entry; e.hasMoreElements(); ) {
52                 entry = e.nextElement();
53                 if (!entry.isDirectory()) {
54                     String toPath = new StringBuffer(
55                             deCompressPath).append(entry.getName()).toString();
56                     toPath = toPath.replace("\\", File.separator);
57                     deCompressFile(zipFile.getInputStream(entry), toPath);
58                 }
59             }
60         } catch (IOException e) {
61             throw new IOException("deCompress: ",e);
62         }  
63    }
64
65     protected void deCompressFile(InputStream input, String toPath)
66             throws IOException {
67         byte[] byteBuf = new byte[2048];
68         String path = new File(toPath).getParent();
69         if (!new File(path).exists()) {
70             new File(path).mkdirs();
71         }
72         try(FileOutputStream output = new FileOutputStream(toPath, false)){
73             for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
74                 output.write(byteBuf, 0, count);
75         } catch (IOException e) {
76             throw new IOException ("deCompressFile: ", e);
77         } 
78     }
79 }