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

包自己导入一下就行了

相关推荐
怀君几秒前
Uniapp——开发Android插件教程
android·uni-app
a***59263 分钟前
SpringBoot实现异步调用的方法
java·spring boot·spring
即将进化成人机5 分钟前
Spring Boot配置文件
java·开发语言·intellij-idea
龙智DevSecOps解决方案8 分钟前
Java开发基础:什么是Spring Boot?一文了解其优势、对比以及如何通过Perforce JRebel实现高效开发
java·开发语言·spring boot·jrebel·perforce·java开发
一直都在57210 分钟前
手写tomcat(1):Socket
java·tomcat
PPPPickup12 分钟前
easychat---创建,获取,获取详细,退群,解散,添加与移除群组
java·开发语言·后端·maven
luod13 分钟前
SpringBoot自动初始化数据
java·spring boot·spring
牛顿没有错14 分钟前
lombok中@Data、@AllArgsConstructor、@NoArgsConstructor不生效
java·spring boot·spring·intellij-idea
Lei活在当下30 分钟前
【Perfetto从入门到精通】1. 初识 Perfetto
android·性能优化·架构
南部余额39 分钟前
深入理解 Spring Boot:自动化配置类与 FactoryBean 的异同与协作
java·spring boot·自动化