Android zip 解压

Android zip 解压

本文主要记录下android中对zip文件的解压操作.

代码如下:

public class ZipUtils {


    /**
     * 解压文件
     *
     * @throws
     */
    public static void unZip(String zipFilePath, File targetDir) throws IOException {
        File destDir = new File(targetDir);       
        if (!destDir.exists()) {
        destDir.mkdir();
        }
        File file = new File(zipFilePath);
        FileInputStream fis = new FileInputStream(file);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        String targetBasePath = targetDir.getAbsolutePath();
        unzip(zis, targetBasePath);
    }

    private static void unzip(ZipInputStream zis, String targetPath) throws IOException {
        ZipEntry entry = zis.getNextEntry();
        if (entry != null) {
            String entryName = entry.getName();
            File file = new File(targetPath + File.separator + entry.getName());
            if (entry.isDirectory()) {
                // 可能存在空文件夹
                if (!file.exists()) {
                    file.mkdirs();
                }
                unzip(zis, targetPath);
            } else {
                File parentFile = file.getParentFile();
                if (parentFile != null && !parentFile.exists()) {
                    parentFile.mkdirs();
                }
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);// 输出流创建文件时必须保证父路径存在
                    int len = 0;
                    byte[] buf = new byte[1024];
                    while ((len = zis.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    fos.flush();

                }catch (Exception ex)
                {}
                finally {
                    if(fos != null)
                    {
                        fos.close();
                    }
                }
                zis.closeEntry();
                unzip(zis, targetPath);
            }
        }
    }

}

相关推荐

  1. Linux 压缩

    2024-02-03 02:56:02       54 阅读
  2. Android zip

    2024-02-03 02:56:02       52 阅读
  3. Python压缩、文件

    2024-02-03 02:56:02       53 阅读
  4. 命令之一 gzip

    2024-02-03 02:56:02       189 阅读
  5. spark分布式工具

    2024-02-03 02:56:02       58 阅读
  6. Linux安装MongoDB

    2024-02-03 02:56:02       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 02:56:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 02:56:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 02:56:02       87 阅读
  4. Python语言-面向对象

    2024-02-03 02:56:02       96 阅读

热门阅读

  1. 蓝桥杯算法赛第4场小白入门赛&强者挑战赛

    2024-02-03 02:56:02       52 阅读
  2. k8s中deployment模板

    2024-02-03 02:56:02       43 阅读
  3. 智合同丨2024年人工智能法律服务怎么做?

    2024-02-03 02:56:02       68 阅读
  4. webassembly003 MINISIT mnist/convert-h5-to-ggml.py

    2024-02-03 02:56:02       51 阅读