Converting RAR to ZIP

Contents
[ Hide Show ]

If you want to convert RAR archive to ZIP read this article.

Convertation

Aspose.ZIP API provides  RarArchive class to extract RAR archives. We can extract an entry to memory without saving it into intermediate storage and pass into ZIP archive right away.

Make sure you have enough virtual memory to keep content of all entries.

Transfer an Entry

The following code example demonstrates how to extract entries from RAR archive and immediately put it to ZIP archive. Entries that are directories added with appended slash to specify their kind.

 1try (Archive zip = new Archive()) {
 2    try (RarArchive rar = new RarArchive("D:\\archvie.rar")) {
 3        for (int i = 0; i < rar.getEntries().size(); i++) {
 4            RarArchiveEntry entry = rar.getEntries().get(i);
 5            if (!entry.isDirectory()) {
 6                try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
 7                    entry.extract(out);
 8                    try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
 9                        zip.createEntry(entry.getName(), in);
10                    }
11                }
12            } else {
13                zip.createEntry(entry.getName() + "/", new ByteArrayInputStream(new byte[0]));
14            }
15        }
16    }
17    zip.save("output.zip");
18} catch (IOException ex) {
19    System.out.println(ex);
20}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.