Sidebar

Uncompress rar file

0 votes
1.8K views
asked Oct 10, 2013 by rich-c-2789 (17,490 points)

1 Answer

0 votes
 
Best answer

To extract the contents of a rar file we first need to add the junrar-0.7.jar  to QIE. Download via maven: com.github.junrar:junrar:0.7 or from http://mvnrepository.com/artifact/com.github.junrar/junrar

Now we can proceed to this basic example:

//Download a sample rar file from the internet and save it locally
//This example file is a pdf picture book taken from: http://www.philipp-winterberg.com/software/rar_faq_introduction_rar_compressed_files.php
var url = new java.net.URL("http://www.philipp-winterberg.com/download/example.rar");
org.apache.commons.io.FileUtils.copyURLToFile(url, new java.io.File("C:\\HL7\\qie.rar"));

//Start extracting the rar file
//define the source
var rarFile = new java.io.File("C:\\HL7\\qie.rar");
var archive = new com.github.junrar.Archive(rarFile);

var fileHeader = archive.nextFileHeader();
while (fileHeader !== null) {
//extract file only, ignore directory since it will be created along with file
if (!fileHeader.isDirectory()) {
//determine file name
var filename;

//get file name after determining the file header type. If you don't do this and use the wrong call, it will return null fro the name.
if (fileHeader.isUnicode()) {
filename = fileHeader.getFileNameW();
} else {
filename = fileHeader.getFileNameString();
}
//define where the file is extracted to
var stream = new java.io.FileOutputStream("C:\\HL7\\" + filename);
//extract it
archive.extractFile(fileHeader, stream);
stream.close();
}

// go to next file
fileHeader = archive.nextFileHeader();
}
archive.close();

Enjoy the story! 

answered Oct 10, 2013 by rich-c-2789 (17,490 points)
selected Dec 17, 2013 by ron-s-6919
...