android开发——解压缩zip文件

 

压缩文件在应用中的使用量也挺大的,就此写一下曾经在android中解压缩一个压缩文件的方法,

File zipFile = new File(FILE_PATH);//FILE_PATH为压缩文件的路径

ZipFile zf = new ZipFile(zipFile);

for(Enumeration<?> entries = zf.entries(); entries.hasMoreElements();){//遍历压缩文件中的所以文件

ZipEntry entry = ((ZipEntry)entries.nextElement());

InputStream is = zf.getInputStream(entry);

String str = sPath + File.separator + entry.getName();

str = new String(str.getBytes(“8859_1″),”GB2312″);//压缩文件的编码是8859_1,此处可将其转换成指定的编码

File desFile = new File(str);

if(!desFile.exists()){

File fileParentDir = desFile.getParentFile();

if(!fileParentDir.exists()){

fileParentDir.mkdirs();

}

desFile.createNewFile();

}

FileOutputStream out = new FileOutputStream(desFile);

byte buffer[] = new byte[1024*1024];

int readLength;

while((readLength = is.read(buffer)) > 0){

out.write(buffer, 0, readLength);

}

is.close();

out.close();

}

代码的内容很好理解,就是把压缩文件打开后依次以流的方式读取里面的内容并写入到指定文件中即可。

你可能感兴趣的:(android开发——解压缩zip文件)