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

包自己导入一下就行了

相关推荐
喵星人工作室5 分钟前
C++传说:神明之剑0.4.5装备机制彻底完成
开发语言·c++·游戏
秦jh_7 分钟前
【Qt】系统相关(下)
开发语言·qt
东木月12 分钟前
使用python获取Windows产品标签
开发语言·windows·python
pumpkin8451413 分钟前
Go 基础语法全景
开发语言·后端·golang
hqwest14 分钟前
码上通QT实战18--监控页面10-获取设备数据
开发语言·qt·湿度·modbus功能码·寄存器地址·从站数据·0103
星火开发设计26 分钟前
C++ multiset 全面解析与实战指南
开发语言·数据结构·c++·学习·set·知识
rannn_11134 分钟前
【Java项目】中北大学Java+数据库课设|校园食堂智能推荐与反馈系统
java·数据库·后端·课程设计·中北大学
似霰37 分钟前
HIDL Hal 开发笔记10----添加硬件访问服务(Java 层调用 HIDL)
android·framework·hal
NE_STOP42 分钟前
SpringBoot-shiro-jwt-dubbo-redis分布式统一权限系统(完结)
java
zfj3211 小时前
java函数式接口 @FunctionalInterface用法
java·函数·function