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

包自己导入一下就行了

相关推荐
方也_arkling10 分钟前
【八股】JS中的事件循环
开发语言·前端·javascript·ecmascript
乌日尼乐13 分钟前
【Java】IO流完全指南
java·后端
你怎么知道我是队长15 分钟前
C语言---函数指针和回调函数
c语言·开发语言
坚持学习前端日记15 分钟前
原生Android开发与JS桥开发对比分析
android·开发语言·javascript
jiunian_cn18 分钟前
【C++11】C++11重要新特性详解
开发语言·c++
何中应22 分钟前
windows安装python环境
开发语言·windows·python
zh_xuan34 分钟前
kotlin 测试if表达式、数组等
开发语言·kotlin
问道飞鱼35 分钟前
【Rust编程】Cargo 工具详解:从基础到高级的完整指南
开发语言·后端·rust·cargo
zhaokuner37 分钟前
14-有界上下文-DDD领域驱动设计
java·开发语言·设计模式·架构
信码由缰41 分钟前
停止编写Excel规格文档:企业级Java开发的Markdown先行方法
java·ai编程·markdown