android中zip文件解压
public class ZipUtil {
/**
* 解压到指定路径
*
* @param inputStream
* @param outPathString
* @throws Exception
*/
public static void UnZipFolder(InputStream inputStream, String outPathString) throws Exception {
ZipInputStream inZip = new ZipInputStream(inputStream);
ZipEntry zipEntry;
String szName = "";
File root = new File(outPathString);
if (!root.exists()) {
root.mkdir();
}
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
//获取部件的文件夹名
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
Log.i("tag", outPathString + File.separator + szName);
File file = new File(outPathString + File.separator + szName);
if (!file.exists()) {
Log.i("tag", "Create the file:" + outPathString + File.separator + szName);
file.getParentFile().mkdirs();
file.createNewFile();
}
// 获取文件的输出流
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[2048];
// 读取(字节)字节到缓冲区
while ((len = inZip.read(buffer)) != -1) {
// 从缓冲区(0)位置写入(字节)字节
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
}
}
网友评论