Location>code7788 >text

Java decompress rar5 compatible with rar4

Popularity:927 ℃/2024-07-30 19:45:27

The RAR file format was developed by WinRAR and is widely used for file compression and archiving. As technology evolves, RAR5, a newer version, introduces several improvements to increase compression efficiency and data security.

  • Compression Efficiency: By increasing the dictionary size to 32MB, compared to RAR4's 4MB, RAR5 is able to find repetitive patterns in the data more efficiently, thus increasing the compression rate, especially when dealing with large files.
  • Enhanced Security: The 256-bit AES encryption algorithm used in RAR5 provides a higher level of data protection and is more difficult to crack compared to RAR4's encryption standard.
  • Internationalization of timestamps: RAR5 uses UTC time, which solves the problem of time zone confusion that may be caused by the use of local time in RAR4, and makes the timestamps of files consistent worldwide.
  • Compatibility Considerations: The RAR5 format is newer and may not be recognized by older versions of decompression software. In scenarios where maximum compatibility needs to be ensured, it may still be necessary to use the RAR4 format.
  • Recovery Volume Improvements: The number of recovery volumes supported by the RAR5 format has been greatly increased from 255 in RAR4 to 65,535, which provides greater flexibility and fault tolerance when working with multi-volume compressed files.
  • Error Correction Capability: RAR5's recovery logging is based on Reed-Solomon error correction codes, which significantly improves the ability of compressed files to repair themselves in case of damage.
  • Log file encoding: RAR5 uses UTF-16 little-endian byte order encoding, which ensures the correct storage and display of Unicode characters in log files and improves support for internationalized file names.

Java decompression implementation of RAR5

Decompression of RAR5 files in Java can be achieved with the help of thejava-unrarrespond in singingSevenZipJBindinglibrary. Below are the implementation steps and code examples.

1. Add dependencies: In the project'sfile to add relevant dependencies.

 <dependency>
            <groupId></groupId>
            <artifactId>java-unrar</artifactId>
            <version>1.7.0-8</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>sevenzipjbinding</artifactId>
            <version>16.02-2.01</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>sevenzipjbinding-all-platforms</artifactId>
            <version>16.02-2.01</version>
        </dependency>

2, write decompression tool class: CreateRar5DocExtractorclass that implements the decompression logic.

  1 package rar5;
  2 
  3 import .*;
  4 import ;
  5 
  6 import .*;
  7 import .*;
  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 (!()) {
 14             (); // Make sure the output directory exists
 15         }
 16 
 17         RandomAccessFile randomAccessFile = null;
 18         IInArchive inArchive = null;
 19         try {
 20             randomAccessFile = new RandomAccessFile(rarFile, "r");
 21             inArchive = (null, new RandomAccessFileInStream(randomAccessFile));
 22             int[] in = new int[()];
 23             for (int i = 0; i < ; i++) {
 24                 in[i] = i;
 25             }
 26             (in, false, new ExtractCallback(inArchive, (), extractedFiles));
 27         } finally {
 28             if (randomAccessFile != null) {
 29                 ();
 30             }
 31             if (inArchive != null) {
 32                 try {
 33                     ();
 34                 } catch (SevenZipException e) {
 35                     ();
 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         // Variables for tracking whether the stream needs to be closed
 48         private OutputStream fos = null;
 49         private boolean closeStreamAfterOperation = false; // Mark whether the stream needs to be closed
 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) (index, );
 69             // The File object is not created here, so that it is not duplicated when getStream is called multiple times.
 70             return new ISequentialOutStream() {
 71                 public int write(byte[] data) throws SevenZipException {
 72                     File file = new File(outDir, path);
 73                     try {
 74                         if ( == 0) return 0;
 75                         ().mkdirs(); // Make sure the catalog exists
 76                         if (fos == null) { // If this is the first write, initialize the output stream
 77                             fos = new FileOutputStream(file);
 78                             closeStreamAfterOperation = true; // Set a flag to indicate that the stream needs to be closed after the result of the operation
 79                         }
 80                         (data);
 81                         (); // Flush to ensure that data is written to disk
 82                         (file); // Add to Extract Files Collection
 83                     } catch (IOException e) {
 84                         throw new SevenZipException("Error writing data to file: " + path, e);
 85                     }
 86                     return ;
 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                     // Closing the output stream
100                     ();
101                 } catch (IOException e) {
102                     throw new SevenZipException("Error when closing file output stream.", e);
103                 } finally {
104                     // reset marker
105                     closeStreamAfterOperation = false;
106                     // Clear references for garbage collection
107                     fos = null;
108                 }
109             }
110         }
111 
112     }
113 }

3. Writing test classes: Create a test class to verify the decompression of RAR5 files.

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