Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using java to extract .rar files

Tags:

java

extract

rar

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();
    }
}
like image 233
flexinIT Avatar asked Jul 25 '12 10:07

flexinIT


People also ask

How do I open an RAR file with Java?

RAR is a proprietary archive file format. RAR license does not allow to include it into software development tools like Java SDK. The best way to unrar your files will be using 3rd party libraries such as junrar. You can find some references to other Java RAR libraries in SO question RAR archives with java.

What is Java RAR file?

A resource adapter archive (RAR) is a Java™ archive (JAR) file used to package a resource adapter for the Connector Architecture for WebSphere® Application Server.


3 Answers

You are able to extract .gz, .zip, .jar files as they use number of compression algorithms built into the Java SDK.

The case with RAR format is a bit different. RAR is a proprietary archive file format. RAR license does not allow to include it into software development tools like Java SDK.

The best way to unrar your files will be using 3rd party libraries such as junrar.

You can find some references to other Java RAR libraries in SO question RAR archives with java. Also SO question How to compress text file to rar format using java program explains more on different workarounds (e.g. using Runtime).

like image 85
Tom Avatar answered Oct 06 '22 22:10

Tom


You can use the library junrar

<dependency>
   <groupId>com.github.junrar</groupId>
   <artifactId>junrar</artifactId>
   <version>0.7</version>
</dependency>

Code example:

            File f = new File(filename);
            Archive archive = new Archive(f);
            archive.getMainHeader().print();
            FileHeader fh = archive.nextFileHeader();
            while(fh!=null){        
                    File fileEntry = new File(fh.getFileNameString().trim());
                    System.out.println(fileEntry.getAbsolutePath());
                    FileOutputStream os = new FileOutputStream(fileEntry);
                    archive.extractFile(fh, os);
                    os.close();
                    fh=archive.nextFileHeader();
            }
like image 36
Jeremias Santos Avatar answered Oct 07 '22 00:10

Jeremias Santos


you could simply add this maven dependency to you code:

<dependency>
    <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>0.7</version>
</dependency>

and then use this code for extract rar file:

        File rar = new File("path_to_rar_file.rar");
    File tmpDir = File.createTempFile("bip.",".unrar");
    if(!(tmpDir.delete())){
        throw new IOException("Could not delete temp file: " + tmpDir.getAbsolutePath());
    }
    if(!(tmpDir.mkdir())){
        throw new IOException("Could not create temp directory: " + tmpDir.getAbsolutePath());
    }
    System.out.println("tmpDir="+tmpDir.getAbsolutePath());
    ExtractArchive extractArchive = new ExtractArchive();
    extractArchive.extractArchive(rar, tmpDir);
    System.out.println("finished.");
like image 40
adramazany Avatar answered Oct 06 '22 22:10

adramazany