Class RarArchive
- java.lang.Object
-
- com.aspose.zip.RarArchive
-
- All Implemented Interfaces:
IArchive,AutoCloseable
public class RarArchive extends Object implements IArchive, AutoCloseable
This class represents RAR archive file. Use it to extract RAR archives.
-
-
Constructor Summary
Constructors Constructor Description RarArchive(InputStream sourceStream)Initializes a new instance of theRarArchiveclass and composes an entry list can be extracted from the archive.RarArchive(InputStream sourceStream, RarArchiveLoadOptions loadOptions)Initializes a new instance of theRarArchiveclass and composes an entry list can be extracted from the archive.RarArchive(String path)Initializes a new instance of theRarArchiveclass and composes an entry list can be extracted from the archive.RarArchive(String path, RarArchiveLoadOptions loadOptions)Initializes a new instance of theRarArchiveclass and composes an entry list can be extracted from the archive.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidextractToDirectory(String destinationDirectory)Extracts all the files in the archive to the directory provided.List<RarArchiveEntry>getEntries()Gets entries ofRarArchiveEntrytype constituting the rar archive.Iterable<IArchiveFileEntry>getFileEntries()Gets entries ofIArchiveFileEntrytype constituting the rar archive.ArchiveFormatgetFormat()Gets the archive format.
-
-
-
Constructor Detail
-
RarArchive
public RarArchive(String path)
Initializes a new instance of the
RarArchiveclass and composes an entry list can be extracted from the archive.The following example extracts 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:
path- The fully qualified or the relative path to the archive file.- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.com.aspose.ms.System.NotSupportedException- File atpathcontains a colon (:) in the middle of the string.
-
RarArchive
public RarArchive(String path, RarArchiveLoadOptions loadOptions)
Initializes a new instance of the
RarArchiveclass and composes an entry list can be extracted from the archive.The following example extracts 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:
path- The fully qualified or the relative path to the archive file.loadOptions- Options to load existing archive with.- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.com.aspose.ms.System.NotSupportedException- File atpathcontains a colon (:) in the middle of the string.
-
RarArchive
public RarArchive(InputStream sourceStream)
Initializes a new instance of the
RarArchiveclass and composes an entry list can be extracted from the archive.
The following example decipher and decompress first entry to aMemoryStream.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:
sourceStream- The source of the archive.- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis not seekable.com.aspose.ms.System.IO.InvalidDataException- Wrong signature for archive. - or - The file is not a RAR archive.
-
RarArchive
public RarArchive(InputStream sourceStream, RarArchiveLoadOptions loadOptions)
Initializes a new instance of the
RarArchiveclass and composes an entry list can be extracted from the archive.
The following example decipher and decompress first entry to aMemoryStream.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:
sourceStream- The source of the archive.loadOptions- Options to load existing archive with.- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis not seekable.com.aspose.ms.System.IO.InvalidDataException- Wrong signature for archive. - or - The file is not a RAR archive.
-
-
Method Detail
-
getEntries
public final List<RarArchiveEntry> getEntries()
Gets entries of
RarArchiveEntrytype constituting the rar archive.- Returns:
- entries of
RarArchiveEntrytype constituting the rar archive.
-
getFileEntries
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of
IArchiveFileEntrytype constituting the rar archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the rar archive.
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
extractToDirectory
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.
- Specified by:
extractToDirectoryin interfaceIArchive- Parameters:
destinationDirectory- The path to the directory to place the extracted files in.- Throws:
com.aspose.ms.System.ArgumentNullException-destinationDirectoryis null.com.aspose.ms.System.IO.PathTooLongException- The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access the existing directory.com.aspose.ms.System.NotSupportedException- If the directory does not exist, the path contains a colon character (:) that is not part of a drive label ("C:\").com.aspose.ms.System.ArgumentException-destinationDirectoryis a zero-length string, contains only white space, or contains one or more invalid characters.com.aspose.ms.System.IO.IOException- The directory specified by path is a file. -or- The network name is not known.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceIArchive
-
-