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;
18 import org.apache.tools.zip.ZipEntry;
19 import org.apache.tools.zip.ZipFile;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Enumeration;
29 protected String deCompressPath = null;
30 protected String zipFilePath = null;
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 + "'");
38 this.deCompressPath = toPath;
39 this.zipFilePath = zipFile.getAbsolutePath();
41 if (deCompressPath == null)
42 deCompressPath = zipFile.getParent() + File.separator;
44 else if (deCompressPath.charAt(deCompressPath.length() - 1) != '/')
45 deCompressPath = deCompressPath + File.separator;
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);
60 } catch (IOException e) {
61 throw new IOException("deCompress: ",e);
65 protected void deCompressFile(InputStream input, String toPath)
67 byte[] byteBuf = new byte[2048];
68 String path = new File(toPath).getParent();
69 if (!new File(path).exists()) {
70 new File(path).mkdirs();
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);