如果目录中含有中文名称, 要用substr = new String(substr.getBytes("8859_1"), "GB2312");这样的语句转换,否则为乱码
01
/**
02
* 解压缩功能.
03
* 将ZIP_FILENAME文件解压到ZIP_DIR目录下.
04
* @throws Exception
05
*/
06
public int upZipFile(File zipFile, String folderPath)throws ZipException,IOException {
07
//public static void upZipFile() throws Exception{
08
ZipFile zfile=new ZipFile(zipFile);
09
Enumeration zList=zfile.entries();
10
ZipEntry ze=null;
11
byte[] buf=new byte[1024];
12
while(zList.hasMoreElements()){
13
ze=(ZipEntry)zList.nextElement();
14
if(ze.isDirectory()){
15
Log.d("upZipFile", "ze.getName() = "+ze.getName());
16
String dirstr = folderPath + ze.getName();
17
//dirstr.trim();
18
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
19
Log.d("upZipFile", "str = "+dirstr);
20
File f=new File(dirstr);
21
f.mkdir();
22
continue;
23
}
24
Log.d("upZipFile", "ze.getName() = "+ze.getName());
25
OutputStream os=new BufferedOutputStream(newFileOutputStream(getRealFileName(folderPath, ze.getName())));
26
InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
27
int readLen=0;
28
while ((readLen=is.read(buf, 0, 1024))!=-1) {
29
os.write(buf, 0, readLen);
30
}
31
is.close();
32
os.close();
33
}
34
zfile.close();
35
return 0;
36
}
37
38
/**
39
* 给定根目录,返回一个相对路径所对应的实际文件名.
40
* @param baseDir 指定根目录
41
* @param absFileName 相对路径名,来自于ZipEntry中的name
42
* @return java.io.File 实际的文件
43
*/
44
public static File getRealFileName(String baseDir, String absFileName){
45
String[] dirs=absFileName.split("/");
46
File ret=new File(baseDir);
47
String substr = null;
48
if(dirs.length>1){
49
for (int i = 0; i < dirs.length-1;i++) {
50
substr = dirs[i];
51
try {
52
//substr.trim();
53
substr = new String(substr.getBytes("8859_1"),"GB2312");
54
55
} catch (UnsupportedEncodingException e) {
56
// TODO Auto-generated catch block
57
e.printStackTrace();
58
}
59
ret=new File(ret, substr);
60
61
}
62
Log.d("upZipFile", "1ret = "+ret);
63
if(!ret.exists())
64
ret.mkdirs();
65
substr = dirs[dirs.length-1];
66
try {
67
//substr.trim();
68
substr = new String(substr.getBytes("8859_1"), "GB2312");
69
Log.d("upZipFile", "substr = "+substr);
70
} catch (UnsupportedEncodingException e) {
71
// TODO Auto-generated catch block
72
e.printStackTrace();
73
}
74
75
ret=new File(ret, substr);
76
Log.d("upZipFile", "2ret = "+ret);
77
return ret;
78
}
79
80
return ret;
81
}