Using java to extract .rar files

I am looking for ways to unzip .rar files using Java, and wherever I am, I continue to work with the same tool - JavaUnRar . I look through the unzipped .rar files with this, but all the methods that I think are found for this are very long and inconvenient, as in this example

Currently, I can extract .tar , .tar.gz , .zip and .jar in 20 lines of code or less, so there should be an easier way to extract .rar files, does anyone know

Just if this helps anyone, this is the code I use to extract the .zip and .jar , it works like for

  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(); } } 
+8
java extract rar
source share
1 answer

You can extract .gz , .zip , .jar because they use the number of compression algorithms built into the Java SDK.

The case with the RAR format is slightly different. RAR is an archive file format . the RAR license does not allow inclusion in software development tools such as the Java SDK.

The best way to unzip your files is to use third-party libraries such as junrar .

You can find links to other Java RAR libraries in the SO question RAR archives with java . Also, the SO question How to compress a text file to rar format using a java program explains more about different workarounds (for example, using Runtime ).

+12
source share

All Articles