在Android平台中如何实现Zip文件的解压缩功能呢? 由于Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩仍是比较简单的,下面给你们一个解压缩zip的 java代码,能够在Android上任何版本中使用,Unzip这个静态方法比较简单,参数一为源zip文件的完整路径,参数二为解压缩后存放的文件夹。
java
01 |
private static void Unzip(String zipFile, String targetDir) { |
02 |
int BUFFER = 4096 ; //这里缓冲区咱们使用4KB, |
03 |
String strEntry; //保存每一个zip的条目名称 |
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 ) { |
11 |
Log.i( "Unzip: " , "=" + entry); |
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()) { |
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); |
27 |
} catch (Exception ex) { |
32 |
} catch (Exception cwj) { |
33 |
cwj.printStackTrace(); |
上面是总结的zip文件解压缩代码,但愿你你们有用,须要注意的是参数均填写完整的路径,好比/mnt/sdcard/xxx.zip这样的类型。