Code improvements in Common Utils
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / Zip.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.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.zip.ZipEntry;
23 import java.util.zip.ZipOutputStream;
24
25
26 public class Zip {
27     protected int compressDirectoryCount = 0;
28     protected int compressFileCount = 0;
29     protected int relativeAddrIdx = 0;
30     protected int compressLevel = 6;
31     protected String zipFilePath = null;
32     protected String compressPath = null;
33     protected ZipOutputStream zipOutput = null;
34
35     public Zip(String compressPath, String zipFilePath) throws IOException {
36         File compressFile = new File(compressPath);
37         if (!compressFile.exists())
38             throw new IOException("the file or directory '" + compressPath + "' not found!");
39         this.zipFilePath = zipFilePath;
40         this.compressPath = compressFile.getAbsolutePath();
41
42         /*if (this.zipFilePath == null) {
43             StringBuffer zipFilePathBuf = new StringBuffer(this.compressPath);
44             int bufLen = zipFilePathBuf.length();
45             if (zipFilePathBuf.charAt(bufLen - 1) == '/')
46                 zipFilePathBuf.deleteCharAt(bufLen - 1);
47             this.zipFilePath = zipFilePathBuf.append(".zip").toString();
48         }*/
49         relativeAddrIdx = this.compressPath.lastIndexOf(File.separator) + 1;
50     }
51
52     public void compress() throws IOException {
53         File theFile = new File(zipFilePath);
54         if (!theFile.exists()) {
55             String parentPath = theFile.getParent();
56             if (parentPath != null)
57                 new File(parentPath).mkdirs();
58             theFile.createNewFile();
59         }
60         zipOutput = new ZipOutputStream(new FileOutputStream(zipFilePath));
61         zipOutput.setMethod(ZipOutputStream.DEFLATED);
62         zipOutput.setLevel(compressLevel);
63         compressDirectory(new File(compressPath));
64         zipOutput.close();
65     }
66
67     protected void compressDirectory(File directoryPath) throws IOException {
68         if (directoryPath.isFile()) {
69             compressFile(directoryPath.getAbsolutePath());
70         } else {
71             File[] listFiles = directoryPath.listFiles();
72             for (int i = 0; i < listFiles.length; i++)
73                 if (listFiles[i].isFile()) {
74                     compressFile(listFiles[i].getAbsolutePath());
75                 } else {
76                     compressDirectoryCount++;
77                     compressDirectory(listFiles[i]);
78                 }
79         }
80     }
81
82     protected void compressFile(String absolutePath) throws IOException {
83             compressFileCount++;
84             byte[] byteBuf = new byte[2048];
85             zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
86
87             try( FileInputStream input = new FileInputStream(absolutePath)){
88                     for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
89                             zipOutput.write(byteBuf, 0, count);
90             }finally{
91                     zipOutput.closeEntry();
92             }
93     }
94
95     public void setCompressLevel(int level) {
96         compressLevel = level;
97     }
98 }