首页 > 编程语言 >Java解压rar5兼容rar4

Java解压rar5兼容rar4

时间:2024-07-30 18:50:21浏览次数:6  
标签:extractedFiles Java RAR5 rar5 rar4 File new inArchive public

  RAR文件格式由WinRAR开发,广泛用于文件压缩和归档。随着技术的发展,RAR5作为更新的版本,引入了多项改进以提高压缩效率和数据安全性。

  •   压缩效率:RAR5通过增大字典大小至32MB,相较于RAR4的4MB,能够更有效地找到数据中的重复模式,从而提高压缩率,特别是在处理大型文件时。
  •   安全性增强:RAR5采用的256位AES加密算法,提供了更高级别的数据保护,相较于RAR4的加密标准,更难被破解。
  •   时间戳的国际化:RAR5使用UTC时间,解决了RAR4使用本地时间可能导致的时区混淆问题,使得文件的时间戳在全球范围内保持一致性。
  •   兼容性考虑:RAR5的格式较新,可能不被旧版本的解压软件识别。在需要确保最大兼容性的场景下,可能仍需使用RAR4格式。
  •   恢复卷的改进:RAR5格式支持的恢复卷数量大大增加,从RAR4的255个增加到65535个,这在处理多卷压缩文件时提供了更高的灵活性和容错性。
  •   错误纠正能力:RAR5的恢复记录基于Reed-Solomon错误纠正码,显著提高了压缩文件在受损情况下的自我修复能力。
  •   日志文件编码:RAR5使用UTF-16小端字节序编码,确保了日志文件中Unicode字符的正确存储和显示,提高了对国际化文件名的支持。

RAR5的Java解压实现

在Java中实现RAR5文件的解压,可以借助java-unrarSevenZipJBinding库。以下是具体的实现步骤和代码示例。

1、添加依赖:在项目的pom.xml文件中添加相关依赖。

 <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>

2、编写解压工具类:创建Rar5DocExtractor类,实现解压逻辑。

  1 package rar5;
  2 
  3 import net.sf.sevenzipjbinding.*;
  4 import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
  5 
  6 import java.io.*;
  7 import java.util.*;
  8 
  9 public class Rar5DocExtractor {
 10 
 11     public List<File> extractFiles(File rarFile, File outputDir) throws IOException {
 12         Set<File> extractedFiles = new HashSet<>();
 13         if (!outputDir.exists()) {
 14             outputDir.mkdirs(); // 确保输出目录存在
 15         }
 16 
 17         RandomAccessFile randomAccessFile = null;
 18         IInArchive inArchive = null;
 19         try {
 20             randomAccessFile = new RandomAccessFile(rarFile, "r");
 21             inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
 22             int[] in = new int[inArchive.getNumberOfItems()];
 23             for (int i = 0; i < in.length; i++) {
 24                 in[i] = i;
 25             }
 26             inArchive.extract(in, false, new ExtractCallback(inArchive, outputDir.getAbsolutePath(), extractedFiles));
 27         } finally {
 28             if (randomAccessFile != null) {
 29                 randomAccessFile.close();
 30             }
 31             if (inArchive != null) {
 32                 try {
 33                     inArchive.close();
 34                 } catch (SevenZipException e) {
 35                     e.printStackTrace();
 36                 }
 37             }
 38         }
 39         List<File> list=new ArrayList<>(extractedFiles);
 40         return list;
 41     }
 42 
 43     private static class ExtractCallback implements IArchiveExtractCallback {
 44         private IInArchive inArchive;
 45         private String outDir;
 46         private Set<File> extractedFiles;
 47         // 用于跟踪是否需要关闭流的变量
 48         private OutputStream fos = null;
 49         private boolean closeStreamAfterOperation = false; // 标记流是否需要关闭
 50 
 51         public ExtractCallback(IInArchive inArchive, String outDir, Set<File> extractedFiles) {
 52             this.inArchive = inArchive;
 53             this.outDir = outDir;
 54             this.extractedFiles = extractedFiles;
 55         }
 56 
 57         @Override
 58         public void setCompleted(long arg0) throws SevenZipException {
 59         }
 60 
 61         @Override
 62         public void setTotal(long arg0) throws SevenZipException {
 63         }
 64 
 65 
 66         @Override
 67         public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
 68             final String path = (String) inArchive.getProperty(index, PropID.PATH);
 69             // 这里不再创建 File 对象,避免多次调用 getStream 时重复创建
 70             return new ISequentialOutStream() {
 71                 public int write(byte[] data) throws SevenZipException {
 72                     File file = new File(outDir, path);
 73                     try {
 74                         if (data.length == 0) return 0;
 75                         file.getParentFile().mkdirs(); // 确保目录存在
 76                         if (fos == null) { // 如果这是第一次写入,初始化输出流
 77                             fos = new FileOutputStream(file);
 78                             closeStreamAfterOperation = true; // 设置标记,表示需要在操作结果后关闭流
 79                         }
 80                         fos.write(data);
 81                         fos.flush(); // 刷新以确保数据被写入磁盘
 82                         extractedFiles.add(file); // 添加到提取文件集合
 83                     } catch (IOException e) {
 84                         throw new SevenZipException("Error writing data to file: " + path, e);
 85                     }
 86                     return data.length;
 87                 }
 88             };
 89         }
 90 
 91         @Override
 92         public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
 93         }
 94 
 95         @Override
 96         public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
 97             if (closeStreamAfterOperation && fos != null) {
 98                 try {
 99                     // 关闭输出流
100                     fos.close();
101                 } catch (IOException e) {
102                     throw new SevenZipException("关闭文件输出流时报错", e);
103                 } finally {
104                     // 重置标记
105                     closeStreamAfterOperation = false;
106                     // 清除引用,以便垃圾回收
107                     fos = null;
108                 }
109             }
110         }
111 
112     }
113 }

3、编写测试类:创建测试类以验证RAR5文件的解压功能。

 1 package rar5;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.List;
 6 
 7 public class RAR5ExtractorTest {
 8 
 9     public static void main(String[] args) {
10         File rarDirFile = new File("src/main/resources/rar5Test06.rarbak");
11         File outDirFile = new File("src/main/resources/temp/rar5Test06.rar");
12 
13         Rar5DocExtractor extractor = new Rar5DocExtractor();
14         try {
15             List<File> extractedFiles = extractor.extractFiles(rarDirFile, outDirFile);
16             System.out.println("Extracted files:");
17             for (File file : extractedFiles) {
18                 System.out.println(file.getAbsolutePath());
19             }
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24 }

 

标签:extractedFiles,Java,RAR5,rar5,rar4,File,new,inArchive,public
From: https://www.cnblogs.com/jyt604743080/p/18333164

相关文章

  • Java正则表达式
    基本规则要点Test //131123456781[3-9]\\d{9} Stringregex1="1[3-9]\\d{9}"; System.out.println("13154654456".matches(regex1));//true System.out.println("03154654456".matches(regex1));//false System.out.printl......
  • orc使用java生成文件的示例代码
    包含了int等基本类型、string、数组importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;importorg.apache.hadoop.hive.ql.exec.vector.ListColumnVector;importorg.apache.......
  • Java中的变量
    变量目录变量变量的声明变量的分类局部变量成员变量(实例变量)类变量(静态变量)参数变量变量的声明在Java语言中,所有的变量在使用前必须声明。声明变量的基本格式如下:typeidentifier[=value][,identifier[=value]...];格式说明:type--数据类型。identifier--......
  • JavaScript 数据结构与基础算法
    数据结构全解参考:数据结构|博客园-SRIGT相关代码仓库查看:data-struct-js|Github-SR1GT0x00前置知识(1)类使用关键字class声明一个类classPerson{}JavaScript的类中通过constructor使用构建函数classPerson{constructor(name){this.name......
  • java @Cacheable生成的redisKey,出现两个连续的冒号::
    1、参考基于redis2.1.6实现springcache生成的key多出一个冒号2、解决需要对key进行处理,【重点】是computePrefixWith方法config=config.computePrefixWith(cacheName->{returncacheName+StrUtil.COLON;});以下是完整代码实现CacheK......
  • Java使用EasyExcel自定义合并(横纵合并)、自定义行高列宽、自适应行高列宽工具Excel导出
    目录一、自适应行高列宽工具类1、自适应行高2、自适应列宽二、自定义行高列宽工具类1、自定义行高2、自定义列宽三、自定义合并工具类四、自定义样式五、实现Excel的完整代码最近又开始写Excel导出的业务,之前写的自适应行高列宽工具类并不满足现下的需求需求是导出......
  • [RoarCTF 2019]Easy Java
    [RoarCTF2019]EasyJavaStep1点击help按钮后发现:URL变成:url/Download?filename=help.docx而回显:java.io.FileNotFoundException:{help.docx}而当我尝试尝试POST,发现文件成功下载:Step2发现可能的漏洞点后,结合WEB-INF相关知识(见文末)可以下载WEB-INF/web.xmlPOST参数......
  • javaweb面向切面aop编程-实现自定义填充
    实现自定义填充注解@AutoFill创建annotation包,编写注解类点击查看代码/***自定义注解,用于标识某个方法需要进行功能字段自动填充处理*/@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public@interfaceAutoFill{//数据库操作类型:UPDATEINSE......
  • java编译错误,找不到包的解决办法
    异常:D:\AC2024\20240729\delphiJIN_JAVA\JavaTest\bin\x64>javaMYclasses.JavaClassForDelphiTestExceptioninthread"main"java.lang.NoClassDefFoundError:com/sltas/front/third/util/CryptionUtilatMYclasses.JavaClassForDelphiTest.main(Jav......
  • JAVA介绍以及jdk、IDEA安装过程
    一.JavaME,JavaSE,JavaEEJavaME(MicroEdition):用途:JavaME是为嵌入式设备和移动设备(如手机、PDA)开发的版本。它专注于资源有限的设备和环境,提供了一个精简的Java运行时环境。特点:1.包含了一套精简的JavaAPI,使得开发者可以在资源受限的设备上运行Java应用程序。2.......