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

包自己导入一下就行了

相关推荐
风清云淡_A2 分钟前
【JPA】spring集成jpa实战之数据增删改查入门教程(二)
java
让我上个超影吧3 分钟前
天机学堂——播放进度方案优化
java·spring boot·redis·spring cloud
月空MoonSky6 分钟前
解决使用Notepad++出现异型字或者繁体字体问题
java·javascript·notepad++
hqwest8 分钟前
码上通QT实战37--项目总结
开发语言·qt·软件开发·系统集成·设备选型
星迹7015 分钟前
C语言相关的数电知识
c语言·开发语言
hakesashou15 分钟前
python 如何使数组中的元素不重复
开发语言·python
2501_9444241216 分钟前
Flutter for OpenHarmony游戏集合App实战之消消乐下落填充
android·开发语言·flutter·游戏·harmonyos
Filotimo_17 分钟前
JWT的概念
java·开发语言·python
min18112345618 分钟前
软件升级全流程步骤详解
android·java·服务器
黎雁·泠崖19 分钟前
Java字符串系列总结篇|核心知识点速记手册
java·开发语言