Extract a file using JUnrar

I asked a question about extracting RAR archives in Java, and someone pointed me to Unrara. The official site has been disabled, but it seems to be pretty widely used since I found a lot of discussion about this on the Internet.

Can someone show me how to use JUnrar to extract all files in an archive? I found a small snippet online, but it doesn't seem to work. It shows that each item in the archive is a directory, even if it is a file.

    Archive rar = new Archive(new File("C://Weather_Icons.rar"));
    FileHeader fh = rar.nextFileHeader();

    while(fh != null){
        if (fh.isDirectory()) {
             logger.severe("directory: " + fh.getFileNameString() ); 
        }

        //File out = new File(fh.getFileNameString());
        //FileOutputStream os = new FileOutputStream(out);
        //rar.extractFile(fh, os);
        //os.close();
        fh=rar.nextFileHeader();

    }

Thank.

+5
source share
1 answer

Perhaps you should also check out this code snippet.

+12
source

All Articles