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