zip-rar4-7z本地解压预览

当前文件自行写下载逻辑

引入库

implementation 'org.apache.commons:commons-compress:1.23.0'
implementation 'com.github.junrar:junrar:7.5.4'//解压rar
implementation 'org.tukaani:xz:1.9'//解压.7z文件
需要

Java:优化后的

package cn.rongcloud.rce.kit;

import android.content.Context;
import android.os.Environment;

import com.github.junrar.Junrar;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import cn.rongcloud.common.log.RongLog;

/**
 * @Description
 * @Author:董昌起
 * @Date2023/8/9 19:24
 */
public class FileZipUtil {

    private static String TAG = "FileZipUtil";
    private static String ZIP = ".zip";
    private static String RAR = ".rar";
    private static String SEVEN_Z = ".7z";

    /**
     * 解压文件
     *
     * @return 返回解压后的文件所在的目录
     */
    public static String extractFiles(Context context, String url) {
        String filename = url.substring(url.lastIndexOf("/") + 1);
        RongLog.e(TAG, "解压文件 " + filename);
        if (filename != null) {
            //解压文件
            String outDir = null;
            if (filename.endsWith(ZIP)) {
                outDir = unzip(context, url);
            } else if (filename.endsWith(SEVEN_Z)) {
                outDir = un7z(context, url);
            } else if (filename.endsWith(RAR)) {
                outDir = unrar(context, url);
            }
            if (outDir != null) {
                RongLog.e(TAG, "解压完毕 " + outDir);
            }
            return outDir;
        }
        return null;
    }

    /**
     * 解压zip
     */
    private static String unzip(Context context, String zipFilePath) {
        File file = new File(zipFilePath);
        if (!file.exists()) {
            return null;
        }
        String appDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                + "/" + "unzipfiles" + "/" + System.currentTimeMillis();
        File af = new File(appDir);
        if (!af.exists()) {
            af.mkdirs();
        }

        try {
            ZipFile zipFile = new ZipFile(zipFilePath, "GBK");
//            List<ZipArchiveEntry> files = (List<ZipArchiveEntry>)zipFile.getEntries();
            Enumeration<ZipArchiveEntry> files = zipFile.getEntries();

            String outDir = appDir;
            List<ZipArchiveEntry> entryList = new ArrayList<>();

            while (files.hasMoreElements()) {
                ZipArchiveEntry z = files.nextElement();
                if (z.isDirectory()) {
                    outDir = appDir + "/" + z.getName();
                    File dir = new File(outDir);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                } else {
                    RongLog.e(TAG, "ZIP中包含的文件: " + z.getName());
                    entryList.add(z);
                }
            }
            for (ZipArchiveEntry z : entryList) {
                InputStream inputStream = zipFile.getInputStream(z);
                File of = new File(appDir + "/" + z.getName());
                File parentFile = of.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                of.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(of);
                int length;
                byte[] buffer = new byte[1024];
                while ((length = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, length);
                    fileOutputStream.flush();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
            zipFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        RongLog.e(TAG, "ZIP解压完毕");
        return appDir;
    }

    /**
     * 解压7z
     */
    private static String un7z(Context context, String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        String appDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                + "/" + "unzipfiles" + "/" + System.currentTimeMillis();
        File ad = new File(appDir);
        if (!ad.exists()) {
            ad.mkdirs();
        }

        try {
            SevenZFile _7zFile = new SevenZFile(file);
            List<SevenZArchiveEntry> files = (List<SevenZArchiveEntry>) _7zFile.getEntries();
            String outDir = appDir;
            List<SevenZArchiveEntry> entryList = new ArrayList<>();
            for (SevenZArchiveEntry s : files) {
                if (s.isDirectory()) {
                    outDir = appDir + "/" + s.getName();
                    File dir = new File(outDir);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                } else {
                    RongLog.e(TAG, "7Z中包含的文件: " + s.getName());
                    entryList.add(s);
                }
            }
            for (SevenZArchiveEntry s : entryList) {
                InputStream inputStream = _7zFile.getInputStream(s);
                File of = new File(appDir + "/" + s.getName());
                File parentFile = of.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                of.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(of);
                int length;
                byte[] buffer = new byte[1024];
                while ((length = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, length);
                    fileOutputStream.flush();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
            _7zFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        RongLog.e(TAG, "7Z解压完毕");
        return appDir;
    }

    /**
     * 解压rar
     */
    private static String unrar(Context context, String rarFilePath) {
        File rarFile = new File(rarFilePath);
        if (!rarFile.exists()) {
            return null;
        }
        long startTime = System.currentTimeMillis();
        String appDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                + "/" + "unzipfiles" + "/" + startTime;
        File tf = new File(appDir);
        if (!tf.exists()) {
            tf.mkdirs();
        }
        String outDir = appDir;

//        val files = Junrar.extract(rarFile, File(context.getAppDir()))
        try {
            List<File> files = Junrar.extract(rarFile, tf);
//            for (File f : files) {
//                if (f.isDirectory()) {
//                    outDir = f.getAbsolutePath();
//                    break;
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        long time = System.currentTimeMillis() - startTime;
        RongLog.e(TAG, "解压 rar 耗时 " + time + " ms");
//        return outDir
        return appDir;
    }

    /**
     * 删除文件夹及其文件
     *
     * @param path
     */
    public static void deleteFolder(String path) {
        File file = new File(path);
        deleteFile(file);
    }

    public static void deleteFile(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }

            for (File childFile : childFiles) {
                deleteFile(childFile);
            }
            file.delete();
        }
    }

    /**
     * 删除文件夹下的所有文件夹和文件,不包括当前文件夹
     */
    public static void deleteFolderDownFile(String folderPath) {
        File file = new File(folderPath);
        if (file.isFile()) {
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }

            for (File childFile : childFiles) {
                deleteFolderDownFile1(childFile);
            }
        }
    }

    public static void deleteFolderDownFile1(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }

            for (int i = 0; i < childFiles.length; i++) {
                deleteFile(childFiles[i]);
            }
            file.delete();
        }
    }
}

借鉴于
https://blog.csdn.net/jjf19891208/article/details/131694721
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值