在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面给大家一个解压缩zip的 java代码,可以在Android上任何版本中使用,Unzip这个静态方法比较简单,参数一为源zip文件的完整路径,参数二为解压缩后存放的文件夹。 |
view sourceprint? |
01 |
private static void Unzip(String zipFile, String targetDir) { |
02 |
int BUFFER = 4096 ; //这里缓冲区我们使用4KB, |
03 |
String strEntry; //保存每个zip的条目名称 |
04 |
try { |
05 |
BufferedOutputStream dest = null ; //缓冲输出流 |
06 |
FileInputStream fis = new FileInputStream(zipFile); |
07 |
ZipInputStream zis = new ZipInputStream( new BufferedInputStream(fis)); |
08 |
ZipEntry entry; //每个zip条目的实例 |
09 |
while ((entry = zis.getNextEntry()) != null ) { |
10 |
try { |
11 |
Log.i( "Unzip: " , "=" + entry); |
12 |
int count; |
13 |
byte data[] = new byte [BUFFER]; |
14 |
strEntry = entry.getName(); |
15 |
File entryFile = new File(targetDir + strEntry); |
16 |
File entryDir = new File(entryFile.getParent()); |
17 |
if (!entryDir.exists()) { |
18 |
entryDir.mkdirs(); |
19 |
} |
20 |
FileOutputStream fos = new FileOutputStream(entryFile); |
21 |
dest = new BufferedOutputStream(fos, BUFFER); |
22 |
while ((count = zis.read(data, 0 , BUFFER)) != - 1 ) { |
23 |
dest.write(data, 0 , count); |
24 |
} |
25 |
dest.flush(); |
26 |
dest.close(); |
27 |
} catch (Exception ex) { |
28 |
ex.printStackTrace(); |
29 |
} |
30 |
} |
31 |
zis.close(); |
32 |
} catch (Exception cwj) { |
33 |
cwj.printStackTrace(); |
34 |
} |
35 |
} |
by: 发表于:2018-01-10 11:15:44 顶(0) | 踩(0) 回复
??
回复评论