Code Smells in emsdriver conf
[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         relativeAddrIdx = this.compressPath.lastIndexOf(File.separator) + 1;
43     }
44
45     public void compress() throws IOException {
46         File theFile = new File(zipFilePath);
47         if (!theFile.exists()) {
48             String parentPath = theFile.getParent();
49             if (parentPath != null)
50                 new File(parentPath).mkdirs();
51             theFile.createNewFile();
52         }
53         zipOutput = new ZipOutputStream(new FileOutputStream(zipFilePath));
54         zipOutput.setMethod(ZipOutputStream.DEFLATED);
55         zipOutput.setLevel(compressLevel);
56         compressDirectory(new File(compressPath));
57         zipOutput.close();
58     }
59
60     protected void compressDirectory(File directoryPath) throws IOException {
61         if (directoryPath.isFile()) {
62             compressFile(directoryPath.getAbsolutePath());
63         } else {
64             File[] listFiles = directoryPath.listFiles();
65             for (int i = 0; i < listFiles.length; i++)
66                 if (listFiles[i].isFile()) {
67                     compressFile(listFiles[i].getAbsolutePath());
68                 } else {
69                     compressDirectoryCount++;
70                     compressDirectory(listFiles[i]);
71                 }
72         }
73     }
74
75     protected void compressFile(String absolutePath) throws IOException {
76             compressFileCount++;
77             byte[] byteBuf = new byte[2048];
78             zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
79
80             try( FileInputStream input = new FileInputStream(absolutePath)){
81                     for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
82                             zipOutput.write(byteBuf, 0, count);
83             }finally{
84                     zipOutput.closeEntry();
85             }
86     }
87
88     public void setCompressLevel(int level) {
89         compressLevel = level;
90     }
91 }