Java decompress the RAR compressed file with a password

Source: Internet
Author: User

RAR compression algorithms are not publicly available, so there are not many open-source projects in this regard

Fortunately, an open-source project named unrar supports extracting RAR files, but cannot compress rarfiles.

However, the direct use of unrar but does not support the rarfile decompression password, after multi-party search, finally found a support password on Google Code unrar version,: http://code.google.com/p/java-unrar/

This project depends on the jar package:

Commons-logging.jar is more commonly used, can download to the Apache Official Website

Gnu-crypto.jar can be downloaded at http://www.gnu.org/software/gnu-crypto/

The following is a simple decompression example:

Package com.ninemax.demo.rar; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstream; import Org. apache. commons. io. ioutils; import de. innosystec. unrar. archive; import de. innosystec. unrar. exception. rarexception; import de.innow.ec.unrar.rar file. fileheader;/*** RAR format compressed file decompression tool class * does not support RAR format compression * supports Chinese, support RAR compressed file Password * dependent jar package * commons-io.jar * commons-logging. Jar * java-unrar-decryption-supported.jar * gnu-crypto.jar ** @ author ninemax */public class rardecompressionutil {public static final string separator = file. separator; // ================================== RAR format ====== ==========================================// *** decompress the specified rarfile to the current folder * @ Param srcrar specifies the password set during decompression * @ Param password compression * @ throws ioexception */public static void unrar (string srcrar, string password) Throws ioexception {unrar (srcrar, null, password );} /*** decompress the specified RAR compressed file to the specified directory * @ Param srcrar specified RAR compressed file * @ Param destpath specified extract directory * @ Param password when compressing the file set password * @ throws ioexception */public static void unrar (string srcrar, string destpath, string password) throws ioexception {file srcfile = new file (srcrar); If (! Srcfile. exists () {return;} If (null = destpath | destpath. length () = 0) {unrar (srcfile, srcfile. getparent (), password); return;} unrar (srcfile, destpath, password );} /*** decompress the specified rarfile to the current folder * @ Param srcrarfile unzip the file * @ Param password the password set when compressing the file * @ throws ioexception */public static void unrar (File srcrarfile, string password) throws ioexception {If (null = srcrarfile |! Srcrarfile. exists () {Throw new ioexception ("the specified file does not exist. ");} unrar (srcrarfile, srcrarfile. getparent (), password );} /*** extract the specified rarfile to the specified path * @ Param srcrarfile the rarfile needs to be decompressed * @ Param destpath specifies the decompression path * @ Param password the password set when compressing the file * @ throws ioexception * /public static void unrar (File srcrarfile, string destpath, string password) throws ioexception {If (null = srcrarfile |! Srcrarfile. exists () {Throw new ioexception ("the specified compressed file does not exist.");} If (! Destpath. endswith (separator) {destpath + = separator;} Archive = NULL; outputstream unout = NULL; try {archive = new archive (srcrarfile, password, false ); fileheader = archive. nextfileheader (); While (null! = Fileheader) {If (! Fileheader. isdirectory () {// 1 obtain the corresponding destdirname and destfilenamestring destfilename = ""; string destdirname = ""; if (separator. equals ("/") {// non-Windows System destfilename = (destpath + fileheader. getfilenamew ()). replaceall ("\\\\", "/"); destdirname = destfilename. substring (0, destfilename. lastindexof ("/");} else {// Windows System destfilename = (destpath + fileheader. getfilenamew ()). replaceall ("/", "\\\\"); Destdirname = destfilename. substring (0, destfilename. lastindexof ("\");} // 2 create a folder file dir = new file (destdirname); If (! Dir. exists () |! Dir. isdirectory () {dir. mkdirs ();} // extract the compressed file unout = new fileoutputstream (new file (destfilename); archive. extractfile (fileheader, unout); unout. flush (); unout. close ();} fileheader = archive. nextfileheader ();} archive. close ();} catch (rarexception e) {e. printstacktrace ();} finally {ioutils. closequietly (unout );}}}

Related Resources:

Http://code.google.com/p/java-unrar/downloads/list

Or

Http://download.csdn.net/detail/zhangyihui1986/4415967

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.