首页>代码>Java解压缩文件,尤其对于Rar5.0版本的解压实现>/decompress/src/main/java/com/hgq/util/RarUtil.java
package com.hgq.util;

import com.github.junrar.Junrar;
import com.github.junrar.exception.RarException;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;

import java.io.*;
import java.util.Arrays;

/**
 * TODO 类的作用信息
 *
 * @author huguangquan
 * @date 22-8-18 018
 */
public class RarUtil {

    /**
     * 通过7z-java-banding 解压rar 支持Rar5版本
     * @param sourceZipFile
     * @param destinationDir
     */
    public static void unRar(String sourceZipFile, String destinationDir) {
        RandomAccessFile randomAccessFile = null;
        IInArchive inArchive = null;
        try {
            randomAccessFile = new RandomAccessFile(sourceZipFile, "r");
            inArchive = SevenZip.openInArchive(null,
                    new RandomAccessFileInStream(randomAccessFile));

            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

            //如果解压到的目标文件夹不存在,则新建
            File destinationFolder = new File(destinationDir);
            if (!destinationFolder.exists()) {
                new File(destinationDir).mkdirs();
            }
            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {

                final int[] hash = new int[]{0};
                if (!item.isFolder()) {
                    ExtractOperationResult result;
                    final long[] sizeArray = new long[1];
                    //如果压缩文件自带文件夹,在这里面创建文件夹
                    //另外,如果压缩文件中有空文件夹,空文件夹是无法解压出来的。
                    if (item.getPath().indexOf(File.separator) > 0) {
                        String path = destinationDir + File.separator
                                + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
                        File folderExisting = new File(path);
                        if (!folderExisting.exists()) {
                            new File(path).mkdirs();
                        }
                    }
                    //被解压的文件
                    OutputStream out = new FileOutputStream(destinationDir + File.separator + item.getPath());
                    //如果文件很大用这个方法可以解压完整,当然文件小用这个也没毛病
                    result = item.extractSlow(new ISequentialOutStream() {
                        @Override
                        public int write(final byte[] data) throws SevenZipException {
                            try {
                                out.write(data);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] |= Arrays.hashCode(data);
                            sizeArray[0] += data.length;
                            return data.length;
                        }
                    });
                    out.close();

                    if (result == ExtractOperationResult.OK) {
                        // 打印执行的压缩包中内容信息,根据实际情况是否打印
                        //System.out.println(String.format("%9X | %10s | %s", //
                        //        hash[0], sizeArray[0], item.getPath()));
                    } else {
                        System.out.println("Error extracting item: " + result);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (SevenZipException e) {
                    System.err.println("Error closing archive: " + e);
                    e.printStackTrace();
                }
            }
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Junrar 解压rar文件,不支持Rar5版本
     *
     * @param rarFileInputStream
     * @param destDir
     */
    public static void unRar(InputStream rarFileInputStream, String destDir) throws IOException, RarException {
        Junrar.extract(rarFileInputStream, new File(destDir));
    }

    /**
     * Junrar 解压rar文件,不支持Rar5版本
     * @param rarFile
     * @param destDir
     */
    public static void unRar(File rarFile, String destDir) throws IOException, RarException {
        FileInputStream rarFileInputStream = new FileInputStream(rarFile);
        Junrar.extract(rarFileInputStream, new File(destDir));
    }
}
最近下载更多
Seaskye  LV14 2023年11月10日
小幸运xiao  LV1 2023年11月3日
逐风796  LV2 2023年8月17日
wangguojie  LV1 2023年5月16日
最代码官方  LV167 2022年9月18日
最近浏览更多
cnng007 2023年11月21日
暂无贡献等级
Seaskye  LV14 2023年11月10日
小幸运xiao  LV1 2023年11月3日
graceful 2023年10月18日
暂无贡献等级
我是恁爹记住 2023年9月27日
暂无贡献等级
二进制2  LV3 2023年9月12日
逐风796  LV2 2023年8月17日
zw5097  LV23 2023年7月15日
sanye333  LV1 2023年6月13日
wangguojie  LV1 2023年5月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友