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

包自己导入一下就行了

相关推荐
勇闯逆流河20 小时前
【Linux】Linux基础开发工具(git、dbg)
linux·运维·服务器·开发语言·c++·git
填满你的记忆20 小时前
JVM 内存模型详解:Java 程序到底是如何运行的?
java·开发语言·jvm
RDCJM20 小时前
Plugin ‘org.springframework.bootspring-boot-maven-plugin‘ not found(已解决)
java·前端·maven
DJ斯特拉20 小时前
SpringBoot项目的基本构建
java·spring boot·后端
小小心愿家20 小时前
初识 maven,Spring boot,Spring MVC
java·后端·spring
身如柳絮随风扬20 小时前
Spring IOC容器的工作原理
java·spring
小温冲冲20 小时前
C++与QML交互指南:从基础到实战
开发语言·c++·交互
不会写DN20 小时前
Go中的泛型与any、interface有什么区别?
开发语言·后端·golang
yaoxin52112320 小时前
350. Java IO API - Java 文件操作:java.io.File 与 java.nio.file 功能对比 - 2
java·python·nio
凸头20 小时前
RPC超时原因
java