RarArchive

Inheritance: java.lang.Object, com.aspose.zip.ILicenseStateProvider

All Implemented Interfaces: com.aspose.ms.System.IDisposable, com.aspose.zip.IArchive, java.lang.AutoCloseable

public class RarArchive extends ILicenseStateProvider implements System.IDisposable, IArchive, AutoCloseable

This class represents RAR archive file. Use it to extract RAR archives.

Constructors

ConstructorDescription
RarArchive(String path)Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.
RarArchive(String path, RarArchiveLoadOptions loadOptions)Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.
RarArchive(InputStream sourceStream)Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.
RarArchive(InputStream sourceStream, RarArchiveLoadOptions loadOptions)Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

Methods

MethodDescription
close(){@inheritDoc}
dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
extractToDirectory(String destinationDirectory)Extracts all the files in the archive to the directory provided.
getEntries()Gets entries of RarArchiveEntry type constituting the rar archive.
getFileEntries()Gets entries of IArchiveFileEntry type constituting the rar archive.

RarArchive(String path)

public RarArchive(String path)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

The following example extract an archive, then decompress first entry to a MemoryStream.


     ByteArrayOutputStream extracted = new ByteArrayOutputStream();
     try (RarArchive archive = new RarArchive("data.rar")) {
         try (InputStream decompressed = archive.getEntries().get(0).open()) {
             byte[] b = new byte[8192];
             int bytesRead;
             while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
                 extracted.write(b, 0, bytesRead);
         } catch (IOException ex) {
         }
     }
 

This constructor does not decompress any entry. See RarArchiveEntry.open() method for decompressing.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe fully qualified or the relative path to the archive file.

RarArchive(String path, RarArchiveLoadOptions loadOptions)

public RarArchive(String path, RarArchiveLoadOptions loadOptions)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

The following example extract an archive, then decompress first entry to a MemoryStream.


     ByteArrayOutputStream extracted = new ByteArrayOutputStream();
     try (RarArchive archive = new RarArchive("data.rar")) {
         try (InputStream decompressed = archive.getEntries().get(0).open()) {
             byte[] b = new byte[8192];
             int bytesRead;
             while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
                 extracted.write(b, 0, bytesRead);
         } catch (IOException ex) {
         }
     }
 

This constructor does not decompress any entry. See RarArchiveEntry.open() method for decompressing.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe fully qualified or the relative path to the archive file.
loadOptionsRarArchiveLoadOptionsOptions to load existing archive with.

RarArchive(InputStream sourceStream)

public RarArchive(InputStream sourceStream)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

The following example decipher and decompress first entry to a MemoryStream.


     try (FileInputStream fs = new FileInputStream("encrypted.rar")) {
         ByteArrayOutputStream extracted = new ByteArrayOutputStream();
         RarArchiveLoadOptions options = new RarArchiveLoadOptions();
         options.setDecryptionPassword("p@s$");
         try (RarArchive archive = new RarArchive(fs, options)) {
             try (InputStream decompressed = archive.getEntries().get(0).open()) {
                 byte[] b = new byte[8192];
                 int bytesRead;
                 while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
                     extracted.write(b, 0, bytesRead);
             }
         }
     } catch (IOException ex) {
     }
 

This constructor does not decompress any entry. See RarArchiveEntry.open() method for decompressing.

Parameters:

ParameterTypeDescription
sourceStreamjava.io.InputStreamThe source of the archive.

RarArchive(InputStream sourceStream, RarArchiveLoadOptions loadOptions)

public RarArchive(InputStream sourceStream, RarArchiveLoadOptions loadOptions)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

The following example decipher and decompress first entry to a MemoryStream.


     try (FileInputStream fs = new FileInputStream("encrypted.rar")) {
         ByteArrayOutputStream extracted = new ByteArrayOutputStream();
         RarArchiveLoadOptions options = new RarArchiveLoadOptions();
         options.setDecryptionPassword("p@s$");
         try (RarArchive archive = new RarArchive(fs, options)) {
             try (InputStream decompressed = archive.getEntries().get(0).open()) {
                 byte[] b = new byte[8192];
                 int bytesRead;
                 while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
                     extracted.write(b, 0, bytesRead);
             }
         }
     } catch (IOException ex) {
     }
 

This constructor does not decompress any entry. See RarArchiveEntry.open() method for decompressing.

Parameters:

ParameterTypeDescription
sourceStreamjava.io.InputStreamThe source of the archive.
loadOptionsRarArchiveLoadOptionsOptions to load existing archive with.

close()

public void close()

dispose()

public final void dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

extractToDirectory(String destinationDirectory)

public final void extractToDirectory(String destinationDirectory)

Extracts all the files in the archive to the directory provided.


    try (RarArchive archive = new RarArchive("archive.rar")) {
        archive.extractToDirectory("C:\\extracted");
    }
 

If the directory does not exist, it will be created.

Parameters:

ParameterTypeDescription
destinationDirectoryjava.lang.StringThe path to the directory to place the extracted files in.

getEntries()

public final List<RarArchiveEntry> getEntries()

Gets entries of RarArchiveEntry type constituting the rar archive.

Returns: java.util.List<com.aspose.zip.RarArchiveEntry> - entries of RarArchiveEntry type constituting the rar archive.

getFileEntries()

public final Iterable<IArchiveFileEntry> getFileEntries()

Gets entries of IArchiveFileEntry type constituting the rar archive.

Returns: java.lang.Iterable<com.aspose.zip.IArchiveFileEntry> - entries of IArchiveFileEntry type constituting the rar archive.