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 .....