2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.emsdriver.commons.utils;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Enumeration;
24 import org.apache.tools.zip.ZipEntry;
25 import org.apache.tools.zip.ZipFile;
29 protected String deCompressPath = null;
30 protected String zipFilePath = null;
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+"'");
41 this.deCompressPath = toPath;
42 this.zipFilePath = zipFile.getAbsolutePath();
44 if (deCompressPath == null)
45 deCompressPath = zipFile.getParent() + File.separator;
47 else if (deCompressPath.charAt(deCompressPath.length()-1) != '/')
48 deCompressPath = deCompressPath + File.separator;
54 public void deCompress() throws IOException {
55 ZipFile zipFile = new ZipFile(zipFilePath);
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);
66 }catch(IOException e){
76 protected void deCompressFile(InputStream input, String toPath)
78 byte byteBuf[] = new byte[2048];
79 String path = new File(toPath).getParent();
80 if(!new File(path).exists()){
81 new File(path).mkdirs();
83 FileOutputStream output = new FileOutputStream(toPath, false);
85 for (int count=0; (count=input.read(byteBuf,0,byteBuf.length))!=-1;)
86 output.write(byteBuf, 0, count);
87 }catch(IOException e){