Modify package structure and pom file
[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 java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Enumeration;
23
24 import org.apache.tools.zip.ZipEntry;
25 import org.apache.tools.zip.ZipFile;
26
27
28 public class UnZip {
29         protected String deCompressPath = null;  
30         protected String zipFilePath = null;    
31         
32         /**
33          * 
34          */
35         public UnZip(String zipFilePath, String toPath) throws IOException {
36                 File zipFile = new File(
37                                 new File(zipFilePath).getAbsolutePath());
38                 if (!zipFile.isFile())
39                         throw new IOException("not found file '"+zipFilePath+"'");
40                 
41                 this.deCompressPath = toPath;
42                 this.zipFilePath = zipFile.getAbsolutePath();
43
44                 if (deCompressPath == null)
45                         deCompressPath = zipFile.getParent() + File.separator;
46                 
47                 else if (deCompressPath.charAt(deCompressPath.length()-1) != '/')                       
48                         deCompressPath = deCompressPath + File.separator;
49         }
50
51         /**
52          * 
53          */
54         public void deCompress() throws IOException {
55                 ZipFile zipFile = new ZipFile(zipFilePath);
56                 try{            
57                         Enumeration<ZipEntry> e = zipFile.getEntries();
58                         for (ZipEntry entry; e.hasMoreElements(); ) {
59                                 if (!(entry=e.nextElement()).isDirectory()) {
60                                         String toPath = new StringBuffer(
61                                                         deCompressPath).append(entry.getName()).toString();
62                                         toPath = toPath.replace("\\", File.separator);
63                                         deCompressFile(zipFile.getInputStream(entry), toPath);
64                                 }
65                         }
66                 }catch(IOException e){
67                         throw e;
68                 }finally{
69                         zipFile.close();
70                 }
71         }
72         
73         /**
74          * 
75          */
76         protected void deCompressFile(InputStream input, String toPath)
77                 throws IOException {
78                 byte byteBuf[] = new byte[2048];
79                 String path = new File(toPath).getParent();
80                 if(!new File(path).exists()){
81                         new File(path).mkdirs();
82                 }
83                 FileOutputStream output = new FileOutputStream(toPath, false);
84                 try{
85                         for (int count=0; (count=input.read(byteBuf,0,byteBuf.length))!=-1;)
86                                 output.write(byteBuf, 0, count);
87                 }catch(IOException e){
88                         throw e;
89                 }finally{
90                         output.close();
91                         input.close();
92                 }
93         }       
94 }