- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathZip.java
More file actions
Latest commit
137 lines (128 loc) · 4.27 KB
/
Copy pathZip.java
File metadata and controls
137 lines (128 loc) · 4.27 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
packagecom.realmo.utils;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
publicclassZip {
privatestaticvoiddeleteFile(Filefile){
if (file.isDirectory()){
File[] files = file.listFiles();
for (Filef: files) {
deleteFile(f);
}
}else{
file.delete();
}
}
/**
* 解压zip文件至dir目录
* @param zip
* @param dir
*/
publicstaticvoidunZip(Filezip, Filedir) {
try {
deleteFile(dir);
ZipFilezipFile = newZipFile(zip);
//zip文件中每一个条目
Enumeration<? extendsZipEntry> entries = zipFile.entries();
//遍历
while (entries.hasMoreElements()) {
ZipEntryzipEntry = entries.nextElement();
//zip中 文件/目录名
Stringname = zipEntry.getName();
//空目录不管
if (!zipEntry.isDirectory()) {
Filefile = newFile(dir, name);
//创建目录
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
//写文件
FileOutputStreamfos = newFileOutputStream(file);
InputStreamis = zipFile.getInputStream(zipEntry);
byte[] buffer = newbyte[2048];
intlen;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
}
}
zipFile.close();
} catch (Exceptione) {
e.printStackTrace();
}
}
/**
* 压缩目录为zip
* @param dir 待压缩目录
* @param zip 输出的zip文件
* @throws Exception
*/
publicstaticvoidzip(Filedir, Filezip) throwsException {
zip.delete();
// 对输出文件做CRC32校验
CheckedOutputStreamcos = newCheckedOutputStream(newFileOutputStream(
zip), newCRC32());
ZipOutputStreamzos = newZipOutputStream(cos);
//压缩
compress(dir, zos, "");
zos.flush();
zos.close();
}
/**
* 添加目录/文件 至zip中
* @param srcFile 需要添加的目录/文件
* @param zos zip输出流
* @param basePath 递归子目录时的完整目录 如 lib/x86
* @throws Exception
*/
privatestaticvoidcompress(FilesrcFile, ZipOutputStreamzos,
StringbasePath) throwsException {
if (srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
for (Filefile : files) {
// zip 递归添加目录中的文件
compress(file, zos, basePath + srcFile.getName() + "/");
}
} else {
compressFile(srcFile, zos, basePath);
}
}
privatestaticvoidcompressFile(Filefile, ZipOutputStreamzos, Stringdir)
throwsException {
// temp/lib/x86/libdn_ssl.so
StringfullName = dir + file.getName();
// 需要去掉temp
String[] fileNames = fullName.split("/");
//正确的文件目录名 (去掉了temp)
StringBuffersb = newStringBuffer();
if (fileNames.length > 1){
for (inti = 1;i<fileNames.length;++i){
sb.append("/");
sb.append(fileNames[i]);
}
}else{
sb.append("/");
}
//添加一个zip条目
ZipEntryentry = newZipEntry(sb.substring(1));
zos.putNextEntry(entry);
//读取条目输出到zip中
FileInputStreamfis = newFileInputStream(file);
intlen;
bytedata[] = newbyte[2048];
while ((len = fis.read(data, 0, 2048)) != -1) {
zos.write(data, 0, len);
}
fis.close();
zos.closeEntry();
}
}