Android解压zip文件到指定目录

很多时候需要把一个预制的zip文件解压到根目录,下面是一个实例代码:

复制代码
	private static final int BUFFER_SIZE = 4096;

    public static void unZip(String zipFilePath, String targetDir) throws IOException {
        File destDir = new File(targetDir);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        try (FileInputStream fis = new FileInputStream(zipFilePath);
             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {

            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                String entryName = entry.getName();
                File file = new File(destDir, entryName);

                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    extractFile(zis, file);
                }
                zis.closeEntry();
            }
        }
    }

    private static void extractFile(ZipInputStream zis, File file) throws IOException {
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }

        try (FileOutputStream fos = new FileOutputStream(file);
             BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE)) {

            byte[] buffer = new byte[BUFFER_SIZE];
            int read;
            while ((read = zis.read(buffer)) != -1) {
                bos.write(buffer, 0, read);
            }
        }
    }

使用实例:

unZip("/system/media/xxx.zip", "/storage/emulated/0/");

包自己导入一下就行了

相关推荐
不会聊天真君6477 分钟前
基础语法·中(golang笔记第二期)
开发语言·笔记·golang
m0_569881477 分钟前
C++中的适配器模式变体
开发语言·c++·算法
第二层皮-合肥14 分钟前
基于C#的工业测试控制软件-总体框架
开发语言·c#
lsx20240618 分钟前
ionic 单选框操作详解
开发语言
用户51722315748027 分钟前
android资源类型与布局资源详细介绍
android
飞Link28 分钟前
Python Pydantic V2 核心原理解析与企业级实战指南
开发语言·python
比昨天多敲两行1 小时前
C++ 多态
开发语言·c++
、BeYourself1 小时前
Scala 字面量
开发语言·后端·scala
Amumu121381 小时前
JS:ES6~ES11基础语法(二)
开发语言·前端·javascript