I am looking a ways to unzip .rar
files using Java and where ever I search i keep ending up with the same tool - JavaUnRar
. I have been looking into unzipping .rar
files with this but all the ways i seem to find to do this are very long and awkward like in this example
I am currently able to extract .tar
, .tar.gz
, .zip
and .jar
files in 20 lines of code or less so there must be a simpler way to extract .rar
files, does anybody know?
Just if it helps anybody this is the code that I am using to extract both .zip
and .jar
files, it works for both
public void getZipFiles(String zipFile, String destFolder) throws IOException {
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(zipFile)));
ZipEntry entry;
while (( entry = zis.getNextEntry() ) != null) {
System.out.println( "Extracting: " + entry.getName() );
int count;
byte data[] = new byte[BUFFER];
if (entry.isDirectory()) {
new File( destFolder + "/" + entry.getName() ).mkdirs();
continue;
} else {
int di = entry.getName().lastIndexOf( '/' );
if (di != -1) {
new File( destFolder + "/" + entry.getName()
.substring( 0, di ) ).mkdirs();
}
}
FileOutputStream fos = new FileOutputStream( destFolder + "/"
+ entry.getName() );
dest = new BufferedOutputStream( fos );
while (( count = zis.read( data ) ) != -1)
dest.write( data, 0, count );
dest.flush();
dest.close();
}
}