blob: cb6d57b2e270d55098643b91c3ef4d6f875b5d2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/*
* Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.emsdriver.commons.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip {
protected int compressDirectoryCount = 0;
protected int compressFileCount = 0;
protected int relativeAddrIdx = 0;
protected int compressLevel = 6;
protected String zipFilePath = null;
protected String compressPath = null;
protected ZipOutputStream zipOutput = null;
public Zip(String compressPath, String zipFilePath) throws IOException {
File compressFile = new File(compressPath);
if (!compressFile.exists())
throw new IOException("the file or directory '" + compressPath + "' not found!");
this.zipFilePath = zipFilePath;
this.compressPath = compressFile.getAbsolutePath();
relativeAddrIdx = this.compressPath.lastIndexOf(File.separator) + 1;
}
public void compress() throws IOException {
File theFile = new File(zipFilePath);
if (!theFile.exists()) {
String parentPath = theFile.getParent();
if (parentPath != null)
new File(parentPath).mkdirs();
theFile.createNewFile();
}
zipOutput = new ZipOutputStream(new FileOutputStream(zipFilePath));
zipOutput.setMethod(ZipOutputStream.DEFLATED);
zipOutput.setLevel(compressLevel);
compressDirectory(new File(compressPath));
zipOutput.close();
}
protected void compressDirectory(File directoryPath) throws IOException {
if (directoryPath.isFile()) {
compressFile(directoryPath.getAbsolutePath());
} else {
File[] listFiles = directoryPath.listFiles();
for (int i = 0; i < listFiles.length; i++)
if (listFiles[i].isFile()) {
compressFile(listFiles[i].getAbsolutePath());
} else {
compressDirectoryCount++;
compressDirectory(listFiles[i]);
}
}
}
protected void compressFile(String absolutePath) throws IOException {
compressFileCount++;
byte[] byteBuf = new byte[2048];
zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
try( FileInputStream input = new FileInputStream(absolutePath)){
for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
zipOutput.write(byteBuf, 0, count);
}finally{
zipOutput.closeEntry();
}
}
public void setCompressLevel(int level) {
compressLevel = level;
}
}
|