Java decompress RAR compressed files

Source: Internet
Author: User

I recently watched Java decompression and found that RAR has no public encryption algorithm. Therefore, Java did not provide API decompression internally. At that time, I felt depressed. I found some ideas on the Internet, you can call the system command to decompress the file. The following describes how to decompress the rarfile.

 

Package zip; import Java. io. bufferedreader; import Java. io. file; import Java. io. ioexception; import Java. io. inputstreamreader;/*** decompress rarfile * Note: Because the RAR algorithm is not public, we can only call the RAR installed by the system in the program to decompress it (WinRAR must be installed in the System) * @ author spring sky * Emal: vipa1888@163.com * QQ: 840950105 * my name: shi mingzheng */public class unrarfile {/*** system installation WinRAR location */Private Static final string winrar_path = "C: \ Program Files \ WinRAR \ winrar.exe "; /** * Decompression method * @ Param rarfilepath RAR path of the compressed file * @ Param unfilepath to be decompressed to the specified path * @ throws ioexception Io exception */public static void unrarfile (string rarfilepath, string unfilepath) throws ioexception {file F = new file (unfilepath); If (! F. exists () // if the specified decompressed path does not exist, create the directory {f. mkdirs ();} string cmd = winrar_path + "X-r-p-o" + rarfilepath + "" + unfilepath; // The runtime = runtime. getruntime (); // obtain the command object process = runtime.exe C (CMD); // obtain the stream returned during command execution. The content of the stream is printed below, check whether an exception exists */inputstreamreader ISR = new inputstreamreader (process. getinputstream (); bufferedreader BR = new bufferedreader (ISR); string STR = NULL; while (S Tr = Br. Readline ())! = NULL) {If (! "". Equals (Str. Trim () & Str! = NULL) // if the current row is not empty {system. Out. println (STR) ;}} Br. Close ();}

Test class:

 

 

/*** Test * @ Param ARGs */public static void main (string [] ARGs) {string rarpath = "D: \ a.rar"; string unrarpath = "D: \ ABC "; try {unrarfile. unrarfile (rarpath, unrarpath);} catch (ioexception e) {system. out. println ("an exception occurred .... "); E. printstacktrace ();}}

 

Note that the pop-up box below the running program

 

 

If there is no encryption, click "OK". If the encryption requires the password to be decompressed, you can directly enter the password to decompress the file .....

 

 

 

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.