1
// 一个压缩解压缩文件和目录的完整程序.
2
// 版权没有, 随便使用.
3
package cross.pauliuyou.tool.io;
4
5
import java.io.*;
6
import java.util.*;
7
import java.util.zip.*;
8
9
10
public class ZipUtil
11

{
12
public static int bufsize = 8192;
13
private String zipBase;
14
15
private int maxFileNameLength;
16
private long totalLength;
17
private long bytesHandled;
18
19
private String getSpace(int num)
20

{
21
String space = "";
22
for (int i = 0; i < num; i++)
23

{
24
space += " ";
25
}
26
return space;
27
}
28
private void getLength(File path)
29

{
30
if (path.isFile())
31

{
32
totalLength += path.length();
33
if (path.getPath().length() > maxFileNameLength)
34

{
35
maxFileNameLength = path.getPath().length();
36
}
37
}
38
else
39

{
40
File [] fs = path.listFiles();
41
for (int i = 0; i < fs.length; i++)
42

{
43
getLength(fs[i]);
44
}
45
}
46
}
47
48
private String lengthString(long fileLength)
49

{
50
long newValue = 0;
51
String size = null;
52
if (fileLength < 1024)
{
53
size = fileLength + " B";
54
} else if (fileLength < (1024 * 1024))
{
55
newValue = fileLength / 1024;
56
size = newValue + " KB";
57
} else if (fileLength < (1024 * 1024 * 1024))
{
58
newValue = fileLength / (1024 * 1024);
59
size = newValue + " MB";
60
} else
{
61
newValue = fileLength / (1024 * 1024 * 1024);
62
size = newValue + " GB";
63
}
64
return size;
65
}
66
public void zip(String path) throws Exception
67

{
68
zip(path,null);
69
}
70
public void zip(String path,String dest) throws Exception
71

{
72
File f = new File(path);
73
if (!f.exists())
74

{
75
System.out.println("File : " + path + " Not Exists!");
76
return;
77
}
78
getLength(f);
79
80
System.out.println("total " + lengthString(totalLength) + " to zip");
81
String path2 = "";
82
for (int i = 0; i < path.length(); i++)
83

{
84
char c = path.charAt(i);
85
if (c == '\\')
86

{
87
c = '/';
88
}
89
path2 += c;
90
}
91
path = path2;
92
93
zipBase = path.substring(path.lastIndexOf("/") == -1 ? 0 : path.lastIndexOf("/") + 1);
94
if (dest == null)
95

{
96
if (f.isFile())
97

{
98
if (dest == null)
99

{
100
zipBase = "./";
101
dest = path.substring(0,(path.lastIndexOf(".") == -1 ? path.length() : path.lastIndexOf("."))) + ".zip";
102
}
103
}
104
else
105

{
106
path2 = path.substring(0,path.lastIndexOf("/") == -1 ? path.length() : path.lastIndexOf("/"));
107
if (path.equals(path2))
108

{
109
dest = ".";
110
}
111
dest = path2 + "/" + zipBase.substring(0,(zipBase.lastIndexOf(".") == -1 ? zipBase.length() : zipBase.lastIndexOf("."))) + ".zip";
112
}
113
}
114
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(dest));
115
zipFile(f,zipOut);
116
zipOut.close();
117
System.out.print("\r100%\t");
118
System.out.println(path + " has been compressed to zip file : " + dest + getSpace(40));
119
}
120
121
public void zipFile(File f,ZipOutputStream zipOut) throws Exception
122

{
123
if (f.isDirectory())
124

{
125
File [] fs = f.listFiles();
126
for (int i = 0; i < fs.length; i++)
127

{
128
zipFile(fs[i],zipOut);
129
}
130
}
131
else
132

{
133
String path = f.getPath();
134
FileInputStream fileIn = new FileInputStream(path);
135
String entryName = path;
136
int basePos = path.indexOf(zipBase);
137
if (basePos > 0)
138

{
139
entryName = path.substring(basePos);
140
}
141
ZipEntry theEntry = new ZipEntry(entryName);
142
theEntry.setSize(f.length());
143
144
zipOut.putNextEntry(theEntry);
145
146
byte[] buffer = new byte[bufsize];
147
int bytesRead = fileIn.read(buffer,0,bufsize);
148
bytesHandled += bytesRead;
149
150
while (bytesRead > 0)
151

{
152
zipOut.write(buffer,0,bytesRead);
153
bytesRead = fileIn.read(buffer,0,bufsize);
154
bytesHandled += bytesRead;
155
int percent = (int)(((double)bytesHandled / totalLength) * 100 );
156
System.out.print("\r" + percent + "% ");
157
if (totalLength > 1024 * 1024 * 80)
158

{
159
int j;
160
for (j = 0; j < percent % 4 + 1; j++)
161

{
162
System.out.print(">");
163
164
}
165
for (int x = 0; x < 4 - j; x++)
166

{
167
System.out.print(" ");
168
}
169
}
170
System.out.print(" Compress File " + path + getSpace(maxFileNameLength - path.length()));
171
}
172
fileIn.close();
173
zipOut.closeEntry();
174
}
175
}
176
177
public void unzip(String zipFileName) throws Exception
178

{
179
unzip(zipFileName,null);
180
}
181
182
public void unzip(String zipFileName,String dest) throws Exception
183

{
184
File f = new File(zipFileName);
185
if (!f.exists())
186

{
187
System.out.println("Zip File : " + zipFileName + " Not Exists!");
188
return;
189
}
190
191
byte[] buffer = new byte[bufsize];
192
193
ZipFile zf = new ZipFile(zipFileName);
194
195
ZipEntry theEntry = null;
196
197
Enumeration enumer = zf.entries();
198
while (enumer.hasMoreElements())
199

{
200
theEntry = (ZipEntry)enumer.nextElement();
201
totalLength += theEntry.getSize();
202
if (theEntry.getName().length() > maxFileNameLength)
203

{
204
maxFileNameLength = theEntry.getName().length();
205
}
206
}
207
208
System.out.println("Zip File " + lengthString(f.length()) + " and Total " + lengthString(totalLength) + " Data to unzip");
209
210
enumer = zf.entries();
211
while (enumer.hasMoreElements())
212

{
213
theEntry = (ZipEntry)enumer.nextElement();
214
String entryName = theEntry.getName();
215
String entryName2 = "";
216
for (int i = 0; i < entryName.length(); i++)
217

{
218
char c = entryName.charAt(i);
219
if (c == '\\')
220

{
221
c = '/';
222
}
223
entryName2 += c;
224
}
225
entryName = entryName2;
226
zipBase = ".";
227
if (dest != null)
228

{
229
if (dest.endsWith("/"))
230

{
231
dest = dest.substring(0,dest.length() - 1);
232
}
233
zipBase = dest;
234
}
235
String tmpFolder = zipBase;
236
237
int begin = 0;
238
int end = 0;
239
while (true)
240

{
241
end = entryName.indexOf("/",begin);
242
if (end == -1)
243

{
244
break;
245
}
246
tmpFolder += "/" + entryName.substring(begin,end);
247
begin = end + 1;
248
f = new File(tmpFolder);
249
if (!f.exists())
250

{
251
f.mkdir();
252
}
253
}
254
OutputStream os = new FileOutputStream(zipBase + "/" + entryName);
255
InputStream is = zf.getInputStream(theEntry);
256
int bytesRead = is.read(buffer,0,bufsize);
257
bytesHandled += bytesRead;
258
while (bytesRead > 0)
259

{
260
os.write(buffer,0,bytesRead);
261
bytesRead = is.read(buffer,0,bufsize);
262
bytesHandled += bytesRead;
263
System.out.print("\r");
264
int percent = (int)(((double)bytesHandled / totalLength) * 100 );
265
System.out.print("\r" + percent + "% ");
266
if (totalLength > 1024 * 1024 * 80)
267

{
268
int j;
269
for (j = 0; j < percent % 4 + 1; j++)
270

{
271
System.out.print(">");
272
273
}
274
for (int x = 0; x < 4 - j; x++)
275

{
276
System.out.print(" ");
277
}
278
}
279
280
System.out.print(" Unzip File " + entryName + getSpace(maxFileNameLength - entryName.length()));
281
}
282
is.close();
283
os.close();
284
}
285
286
System.out.println("\r100% Zip File : " + zipFileName + " has been unCompressed to " + dest + "/ !" + getSpace(40));
287
}
288
289
public static void main(String []args) throws Exception
290

{
291
if (args.length < 2)
292

{
293
System.out.println("usage : java cross.pauliuyou.tool.io.ZipUtil -zip[-unzip] filename");
294
return;
295
}
296
ZipUtil zip = new ZipUtil();
297
String dest = null;
298
if (args[0].equals("-zip"))
299

{
300
if (args.length > 2)
301

{
302
dest = args[2];
303
}
304
zip.zip(args[1],dest);
305
}
306
if (args[0].equals("-unzip"))
307

{
308
if (args.length > 2)
309

{
310
dest = args[2];
311
}
312
zip.unzip(args[1],dest);
313
}
314
}
315
}
posted on 2008-07-23 17:57
scea2009 阅读(256)
评论(0) 编辑 收藏