安卓解压文件

主要就是采用流的方式处理大文件

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();
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值