-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipUtils.java
More file actions
166 lines (144 loc) · 4.89 KB
/
ZipUtils.java
File metadata and controls
166 lines (144 loc) · 4.89 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package installOpenpms.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
public class ZipUtils {
private static final int BUFFER_SIZE = 1024 * 2;
/**
* <b> zip 파일의 압축을 푼다. </b>
* @param zipFilePath - 압축 풀 Zip 파일의 경로
* @param targetDirPath - 압축 푼 파일이 들어갈 디렉토리 경로
*/
public static void unzip(String zipFilePath, String targetDirPath) throws Exception {
File zipFile = new File(zipFilePath);
File targetDir = new File(targetDirPath);
unzip(zipFile, targetDir);
}
public static void unzip(File zippedFile) throws IOException {
unzip(zippedFile, Charset.defaultCharset().name());
}
public static void unzip(File zippedFile, String charsetName) throws IOException {
unzip(zippedFile, zippedFile.getParentFile(), charsetName);
}
public static void unzip(File zippedFile, File destDir) throws IOException {
unzip(zippedFile, destDir, Charset.defaultCharset().name());
}
public static void unzip( File zippedFile, File destDir, String charsetName) throws FileNotFoundException, IOException {
InputStream is = new FileInputStream(zippedFile);
ZipArchiveInputStream zis ;
ZipArchiveEntry entry;
String name;
File target;
int nWritten = 0;
BufferedOutputStream bos;
byte[] buf = new byte[1024 * 8];
zis = new ZipArchiveInputStream(is, charsetName, false);
ZipFile zf = new ZipFile(zippedFile);
int fileCnt = zf.size(), iterCnt = 0, proc = 0;
while ((entry = zis.getNextZipEntry()) != null) {
if( proc <= (100*iterCnt++) / fileCnt ) {
switch(proc%5) { // ㅋㅋㅋㅋㅋㅋ+/-\|ㅋㅋㅋㅋㅋㅋㅋㅋ
case 0: System.out.print("\b\b\b\b\b.+"); break;
case 1: System.out.print("\b\b\b\b\b/"); break;
case 2: System.out.print("\b\b\b\b\b-"); break;
case 3: System.out.print("\b\b\b\b\b\\"); break;
case 4: System.out.print("\b\b\b\b\b|"); break;
}
System.out.printf(" %2d", proc);
System.out.print("%");
proc++;
}
name = entry.getName();
target = new File(destDir, name);
if (entry.isDirectory()) {
target.mkdirs(); /* does it always work? */
} else {
System.out.println(target);
target.createNewFile();
bos = new BufferedOutputStream(new FileOutputStream(target));
while ((nWritten = zis.read(buf)) >= 0) {
bos.write(buf, 0, nWritten);
}
bos.close();
}
}
System.out.println("\b\b\b\b\b# 100%");
zis.close();
/*
FileInputStream fis = null;
ZipInputStream zis = null;
ZipEntry zentry = null;
try {
fis = new FileInputStream(zipFile); // FileInputStream
zis = new ZipInputStream(fis); // ZipInputStream
ZipFile zf = new ZipFile(zipFile);
int fileCnt = zf.size(), iterCnt = 0, proc = 0;
while ((zentry = zis.getNextEntry()) != null) {
String fileNameToUnzip = zentry.getName();
if( proc <= (100*iterCnt++) / fileCnt ) {
switch(proc%5) { // ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ+/-\|ㅋㅋㅋㅋㅋㅋㅋㅋㅋ
case 0: System.out.print("\b\b\b\b\b.+"); break;
case 1: System.out.print("\b\b\b\b\b/"); break;
case 2: System.out.print("\b\b\b\b\b-"); break;
case 3: System.out.print("\b\b\b\b\b\\"); break;
case 4: System.out.print("\b\b\b\b\b|"); break;
}
System.out.printf(" %2d", proc);
System.out.print("%");
proc++;
}
File targetFile = new File(targetDir, fileNameToUnzip);
if (zentry.isDirectory()) {// Directory 인 경우
FileUtils.makeDir(targetFile.getAbsolutePath()); // 디렉토리 생성
} else { // File 인 경우
// parent Directory 생성
FileUtils.makeDir(targetFile.getParent());
unzipEntry(zis, targetFile);
}
}
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}*/
}
/**
* Zip 파일의 한 개 엔트리의 압축을 푼다.
*
* @param zis
* - Zip Input Stream
* @param filePath
* - 압축 풀린 파일의 경로
* @return
* @throws Exception
*/
protected static File unzipEntry(ZipInputStream zis, File targetFile)
throws Exception {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} finally {
if (fos != null) {
fos.close();
}
}
return targetFile;
}
}