Java实现解压RAR5算法压缩的rar文件与判断是否rar5加

2023-08-11  本文已影响0人  爱学习的蹭蹭

1、参考文件 Java解压RAR5

RAR5加密算法并未公布,所以很多开源工具包都只支持rar4,在解压rar5格式时,会报出不支持rar5格式的错误,比如常用的junara 经过仔细的翻阅Google,找到了解决方案

2.pom文件导入jar

<dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>java-unrar</artifactId>
            <version>1.7.0-8</version>
        </dependency>
        <dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding</artifactId>
            <version>16.02-2.01</version>
        </dependency>
        <dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding-all-platforms</artifactId>
            <version>16.02-2.01</version>
        </dependency>

3、代码实现ExtractCallback


import net.sf.sevenzipjbinding.*;

import java.io.*;

public class ExtractCallback implements IArchiveExtractCallback {

    private int index;
    private IInArchive inArchive;
    private String ourDir;

    public ExtractCallback(IInArchive inArchive, String ourDir) {
        this.inArchive = inArchive;
        this.ourDir = ourDir;
    }

    @Override
    public void setCompleted(long arg0) {
    }

    @Override
    public void setTotal(long arg0) {
    }

    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        this.index = index;
        final String path = (String) inArchive.getProperty(index, PropID.PATH);
        final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
        return data -> {
            try {
                if (!isFolder) {
                    File file = new File(ourDir + path);
                    saveFile(file, data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data.length;
        };
    }

    @Override
    public void prepareOperation(ExtractAskMode arg0) {
    }

    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) {

    }

    public static void saveFile(File file, byte[] msg) {
        OutputStream fos = null;
        try {
            File parent = file.getParentFile();
            if ((!parent.exists()) && (!parent.mkdirs())) {
                return;
            }
            fos = new FileOutputStream(file, true);
            fos.write(msg);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4、代码实现ExtractCallback

import java.io.*;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

public class FileUtil {
 /**
     * 经过测试,可以解压rar5算法的压缩文件
     * @param rarDir 解压文件的路径
     * @param outDir 解压之后输出的路径
     */
    public static void unRarfile(String rarDir, String outDir) {
        try {
            // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
            //r代表以只读的方式打开文本,也就意味着不能用write来操作文件
            RandomAccessFile randomAccessFile = new RandomAccessFile(rarDir, "r");

            IInArchive archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
            int[] in = new int[archive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            //拼接输出目录文件路径
            String dest = outDir.substring(0, outDir.lastIndexOf("\\"))+"/";

            archive.extract(in, false, new ExtractCallback(archive, dest));
            archive.close();
            randomAccessFile.close();
        } catch (Exception e) {
            err.println(e.getMessage());
        }
    }
}

5、带密码与不带密码的解压

推荐用此方法即可

/**
     * @param rarDir   rar解压文件路径包含文件名称
     * @param outDir   rar存储文件路径不含文件名称
     * @param passWord 密码
     * @return
     */
    public static boolean unRar(String rarDir, String outDir, String passWord) {
        RandomAccessFile randomAccessFile = null;
        IInArchive inArchive = null;
        try {
            // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
            randomAccessFile = new RandomAccessFile(rarDir, "r");
            if (StringUtils.isNotBlank(passWord)){
                inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord);
            }else{
                inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
            }

            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

            int fileCout = 0;
            //int failCout = 0;
            int itemCout = simpleInArchive.getArchiveItems().length;

            //String fail = "";
            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {

                final int[] hash = new int[]{0};
                //PDF是否
                if (!item.isFolder()) {

                    ExtractOperationResult result;
                    final long[] sizeArray = new long[1];

                    String dest = outDir.substring(0, outDir.lastIndexOf("\\")) + "\\";

                    File outFile = new File(dest + item.getPath());
                    File parent = outFile.getParentFile();
                    if ((!parent.exists()) && (!parent.mkdirs())) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(passWord)) {
                        result = item.extractSlow(data -> {
                            try {
                                IOUtils.write(data, new FileOutputStream(outFile, true));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                        }, passWord);
                    } else {
                        result = item.extractSlow(data -> {
                            try {
                                IOUtils.write(data, new FileOutputStream(outFile, true));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                        });
                    }

                    if (result == ExtractOperationResult.OK) {
                        //LoggerHelper.info("解压rar成功...." + String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
                        //return true;
                        fileCout++;
                    } else {
                        //failCout++;
                        //fail += "解压rar失败...." + item.getPath() + "\n";
                        break;
                    }
                }
            }
            //去掉一个文件夹名称就正确
            if (fileCout - 1 == itemCout) {
                String fileName = new File(rarDir).getName();
               // LoggerHelper.info("解压rar成功....【" + fileName + "】总共有:【" + fileCout + "】个文件");
                return true;
            } else {
                //LoggerHelper.info("解压rar失败....【" + fail + "】总共有:【" + failCout + "】个文件");
                return false;
            }
        } catch (Exception e) {
            
            //LoggerHelper.info("\n unRar--" + e.getMessage());
            return false;
        } finally {
            try {
                if (inArchive != null && randomAccessFile != null) {
                    inArchive.close();
                    randomAccessFile.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

6、判断rar压缩文件是否是rar5算法

 /**
     * 创建RAR5判断方式是否检测密码方式
     *
     * @param rarFilePath
     * @return
     */
    public static boolean checkPwdRar5(String rarFilePath) {
        IInStream inStream = null;
        IInArchive inArchive = null;
        try {
            // 创建RAR文件对象
            File rarFile = new File(rarFilePath);

            // 创建RandomAccessFile用于读取RAR文件
            RandomAccessFile randomAccessFile = new RandomAccessFile(rarFile, "r");

            // 创建RandomAccessFileInStream
            inStream = new RandomAccessFileInStream(randomAccessFile);

            // 打开压缩文件
            inArchive = SevenZip.openInArchive(null, inStream);

            // 获取压缩文件中的所有项
            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
            ISimpleInArchiveItem[] archiveItems = simpleInArchive.getArchiveItems();


            // 检查文件是否有密码
            boolean hasPassword = Arrays.stream(archiveItems)
                    .anyMatch(item -> {
                        try {
                            return item.isEncrypted();
                        } catch (SevenZipException e) {
                            throw new RuntimeException(e);
                        }
                    });

            // 输出检测结果
            if (hasPassword) {
                System.out.println("RAR文件带有密码!");
                return true;
            } else {
                System.out.println("RAR文件没有密码。");
                return false;
            }

        } catch (Exception e) {
            System.out.println("检测rar5异常" + e.getMessage());
            return false;
        } finally {
            // 关闭RandomAccessFileInStream和压缩文件
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

7、代码运行

public class DoMain {
    public static void main(String[] args) {
        String rarPath = "D:\\test\\test.rar";
        //替换文件后缀作为一个输出目标路径.
        String destPath = rarPath.replace(".rar", "-file");
        FileUtil.unRarfile(rarPath,destPath);
        
        String pwd="1221222";
        if(checkPwdRar5(rarPath)){
            unRar(rarPath,destPath,pwd);
        }else{
            unRar(rarPath,destPath,"");
        }
  }
}

8 总结与推荐学习文章

推荐看sevenzipjbinding官方例子

rarlab解析信息

RAR5 文件格式解析

上一篇 下一篇

猜你喜欢

热点阅读