Extracting RAR Archives

Overview

Aspose.ZIP API lets extract archives in your applications without the need of any other 3rd party applications. Aspose.ZIP API provides  RarArchive class to work with RAR archives.  API provides the  RarArchiveEntry class to represents a single file within the RAR archive.

Creation of RAR archives is not possible.

Extract an Entry

The following code example demonstrates how to extract an entry using RarArchive instance.

 1try (RarArchive archive = new RarArchive("archive.rar")) {
 2    try (FileOutputStream destination = new FileOutputStream(dataDir + "firstEntry.txt")) {
 3        try (InputStream source = archive.getEntries().get(0).open()) {
 4            byte[] b = new byte[1024];
 5            int bytesRead;
 6            while (0 < (bytesRead = source.read(b, 0, b.length))) {
 7                destination.write(b, 0, bytesRead);
 8            }
 9        }
10    }
11} catch (IOException ex) {
12    System.out.println(ex);
13}

Extract an Encrypted Entry

The following code example demonstrates how to extract an encrypted entry using RarArchive instance.

1File fi = new File("encrypted.rar");
2try (RarArchive archive = new RarArchive(Files.newInputStream(fi.toPath()))) {
3    try (FileOutputStream destination = new FileOutputStream(dataDir + "firstEntry.txt")) {
4        archive.getEntries().get(0).extract(destination, "p@s$w0rd");
5    }
6} catch (IOException ex) {
7    System.out.println(ex);
8}

Extracting Compressed Directory

The following code example demonstrates how to all the files from RarArchive instance.

1try (RarArchive archive = new RarArchive("archive.rar")) {
2    archive.extractToDirectory("extracted");
3}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.