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/");

包自己导入一下就行了

相关推荐
z人间防沉迷k2 小时前
后端开发概念
java·后端
caihuayuan52 小时前
Vue3响应式数据: 深入分析Ref与Reactive
java·大数据·spring boot·后端·课程设计
星释2 小时前
Mac Python 安装依赖出错 error: externally-managed-environment
开发语言·python·macos
CodeWithMe3 小时前
【C/C++】线程状态以及转换
java·c语言·c++
苹果酱05673 小时前
Java设计模式:探索编程背后的哲学
java·vue.js·spring boot·mysql·课程设计
百锦再3 小时前
安卓无障碍脚本开发全教程
android·手机·脚本·开发·mobile·phone·无障碍
小迅先生3 小时前
AI开发 | Web API框架选型-FastAPI
开发语言·python·fastapi
五花肉村长3 小时前
Linux-读者写著问题和读写锁
linux·运维·服务器·开发语言·数据库·visualstudio
biubiubiu07063 小时前
windows中JDK切换版本
java·开发语言
qq_334060215 小时前
spring5-配外部文件-spEL-工厂bean-FactoryBean-注解配bean
java·spring·web