主要就是采用流的方式处理大文件
1.先将源文件转换成安卓系统自带CheckedInputStream,这个是一个检验流:
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
2.再将这个检验流转换成Zip输入流:
ZipInputStream zis = new ZipInputStream(cis);
3.获取zip流的实体:
while ((zipEntry = zis.getNextEntry()) != null)
4.然后用字节输出流的方式:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
完整代码:
public static File decompress(File srcFile, File destFile) throws Exception {
while (destFile.exists()) {
destFile = new File(destFile.getAbsolutePath()+"1");
}
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
doDecompress(destFile, zis);
zis.close();
return destFile;
}
private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
fileProber(dirFile);
if (zipEntry.isDirectory()) {
dirFile.mkdirs();
} else {
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
}
private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
}
|