How to decompress the zip and rarfile in Java

Source: Internet
Author: User

1. decompress the rarfile.
Since WinRAR is a shared software and not open-source software, the premise for extracting the rarfile is that WinRAR has been installed in the system. For example, my installation path is:
C: // program files // WinRAR // winrar.exe
Then, we use the knowledge of Java. Lang. process to run the system command line to decompress the package.
WinRAR command line-related parameters can be searched by yourself for a lot of online information
Code:

Java code
  1. **
  2. * Decompress the rarfile (the system must install WinRAR software)
  3. * @ Author Michael sun
  4. */
  5. Public class unrarfile {
  6. /**
  7. * Decompress the rarfile
  8. *
  9. * @ Param targetpath
  10. * @ Param absolutepath
  11. */
  12. Public void unrarfile (string targetpath, string absolutepath ){
  13. Try {
  14. // System installation path for WinRAR
  15. String cmd = "C: // program files // WinRAR // winrar.exe ";
  16. String unrarcmd = cmd + "X-r-p-O +" + absolutepath + ""
  17. + Targetpath;
  18. Runtime RT = runtime. getruntime ();
  19. Process pre = rt.exe C (unrarcmd );
  20. Inputstreamreader ISR = new inputstreamreader (PRE. getinputstream ());
  21. Bufferedreader BF = new bufferedreader (ISR );
  22. String line = NULL;
  23. While (line = BF. Readline ())! = NULL ){
  24. Line = line. Trim ();
  25. If ("". Equals (line )){
  26. Continue;
  27. }
  28. System. Out. println (line );
  29. }
  30. BF. Close ();
  31. } Catch (exception e ){
  32. System. Out. println ("decompression exception ");
  33. }
  34. }
  35. /**
  36. * @ Param ARGs
  37. */
  38. Public static void main (string [] ARGs ){
  39. String targetpath = "D: // test // unrar //";
  40. String rarfilepath = "D: // test // test.rar ";
  41. Unrarfile unrar = new unrarfile ();
  42. Unrar. unrarfile (targetpath, rarfilepath );
  43. }
  44. }

Ii. decompress the ZIP file
Because zip is free of charge, the corresponding class is provided in JDK to implement the ZIP file:
Java.util.zip. *. For details, refer to Java API Java code.

  1. /**
  2. * Decompress the ZIP file.
  3. * @ Author Michael sun
  4. */
  5. Public class unzipfile {
  6. /**
  7. * Decompress the ZIP file.
  8. *
  9. * @ Param targetpath
  10. * @ Param zipfilepath
  11. */
  12. Public void unzipfile (string targetpath, string zipfilepath ){
  13. Try {
  14. File zipfile = new file (zipfilepath );
  15. Inputstream is = new fileinputstream (zipfile );
  16. Zipinputstream zis = new zipinputstream (is );
  17. Zipentry entry = NULL;
  18. System. Out. println ("START decompression:" + zipfile. getname () + "...");
  19. While (Entry = Zis. getnextentry ())! = NULL ){
  20. String zippath = entry. getname ();
  21. Try {
  22. If (entry. isdirectory ()){
  23. File zipfolder = new file (targetpath + file. Separator
  24. + Zippath );
  25. If (! Zipfolder. exists ()){
  26. Zipfolder. mkdirs ();
  27. }
  28. } Else {
  29. File file = new file (targetpath + file. Separator
  30. + Zippath );
  31. If (! File. exists ()){
  32. File pathdir = file. getparentfile ();
  33. Pathdir. mkdirs ();
  34. File. createnewfile ();
  35. }
  36. Fileoutputstream Fos = new fileoutputstream (File );
  37. Int bread;
  38. While (bread = Zis. Read ())! =-1 ){
  39. FOS. Write (bread );
  40. }
  41. FOS. Close ();
  42. }
  43. System. Out. println ("decompressed successfully:" + zippath );
  44. } Catch (exception e ){
  45. System. Out. println ("decompression" + zippath + "failed ");
  46. Continue;
  47. }
  48. }
  49. Zis. Close ();
  50. Is. Close ();
  51. System. Out. println ("decompress ");
  52. } Catch (exception e ){
  53. E. printstacktrace ();
  54. }
  55. }
  56. /**
  57. * @ Param ARGs
  58. */
  59. Public static void main (string [] ARGs ){
  60. String targetpath = "D: // test // unzip ";
  61. String zipfile = "D: // test // test.zip ";
  62. Unzipfile unzip = new unzipfile ();
  63. Unzip. unzipfile (targetpath, zipfile );
  64. }
  65. }

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.