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.nfvo.emsdriver.commons.utils;
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;
27 protected int compressDirectoryCount = 0;
28 protected int compressFileCount = 0;
30 protected int relativeAddrIdx = 0;
31 protected int compressLevel = 6;
32 protected String zipFilePath = null;
33 protected String compressPath = null;
35 protected ZipOutputStream zipOutput = null;
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!");
45 this.zipFilePath = zipFilePath;
46 this.compressPath = compressFile.getAbsolutePath();
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();
55 relativeAddrIdx = this.compressPath.lastIndexOf(File.separator)+1;
61 public void compress() throws IOException {
62 File theFile = new File(zipFilePath);
64 if (!theFile.exists()) {
65 String parentPath = theFile.getParent();
66 if (parentPath != null)
67 new File(parentPath).mkdirs();
68 theFile.createNewFile();
70 zipOutput = new ZipOutputStream(new FileOutputStream(zipFilePath));
71 zipOutput.setMethod(ZipOutputStream.DEFLATED);
72 zipOutput.setLevel(compressLevel);
73 compressDirectory(new File(compressPath));
77 protected void compressDirectory(File directoryPath) throws IOException {
78 if (directoryPath.isFile()) {
79 compressFile(directoryPath.getAbsolutePath());
81 File listFiles[] = directoryPath.listFiles();
82 for (int i=0; i<listFiles.length; i++)
83 if (listFiles[i].isFile()) {
84 compressFile(listFiles[i].getAbsolutePath());
86 compressDirectoryCount ++;
87 compressDirectory(listFiles[i]);
93 protected void compressFile(String absolutePath) throws IOException {
95 byte byteBuf[] = new byte[2048];
96 zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
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);
102 zipOutput.closeEntry();
105 public void setCompressLevel(int level) {
106 compressLevel = level;