ZIP file decompression (supports Chinese file names)

Source: Internet
Author: User
Tags getmessage
The file must be supported by the ant. jar package.

  1. Package com. amarsoft. CEB. util;
  2. Import java. Io .*;
  3. Import java. util. arraylist;
  4. Import java. util. enumeration;
  5. Import org.apache.tools.zip. zipentry;
  6. Import org.apache.tools.zip. zipfile;
  7. Public class ziptools {
  8. Private arraylist list = new arraylist (); // stores the extracted file name
  9. /**
  10. * Generate the file path
  11. * 1. If the sub-file is empty and the directory does not exist, create a directory
  12. * 2. If directory and subdirectory are not empty, press d to create a directory.
  13. * @ Param directory
  14. * @ Param subdirectory
  15. */
  16. Private void createdirectory (string directory, string subdirectory ){
  17. String dir [];
  18. File FL = new file (directory );
  19. Try {
  20. If (subdirectory. Equals ("") & FL. exists () = false ){
  21. Fl. mkdir ();
  22. } Else if (! Subdirectory. Equals ("")){
  23. Dir = subdirectory. Replace ('/', '/'). Split ("/");
  24. For (INT I = 0; I <dir. length; I ++ ){
  25. File subfile = new file (directory + file. Separator + dir [I]);
  26. If (subfile. exists () = false) subfile. mkdir ();
  27. Directory + = file. Separator + dir [I];
  28. }
  29. }
  30. }
  31. Catch (exception ex ){
  32. System. Out. println (ex. getmessage ());
  33. }
  34. }
  35. /**
  36. *
  37. * @ Param zipfilename ZIP file name
  38. * @ Param outputdirectory: output directory
  39. * @ Return array (two elements). The first table indicates the unzipping flag, and the second is the message.
  40. * @ Throws exception
  41. */
  42. Public String [] unzip (string zipfilename, string outputdirectory ){
  43. String [] sreturn = new string [2];
  44. Boolean readflag = false; // read the file ID to prevent Empty files from being decompressed.
  45. Try {
  46. Zipfile = new zipfile (zipfilename );
  47. Enumeration E = zipfile. getentries ();
  48. Zipentry = NULL;
  49. Createdirectory (outputdirectory, ""); // generate the output directory
  50. While (E. hasmoreelements () {// if there is a file in the compressed file, decompress
  51. String upzipedfilename;
  52. Readflag = true;
  53. Zipentry = (zipentry) E. nextelement ();
  54. Upzipedfilename = zipentry. getname ();
  55. System. Out. println ("unziping:" + upzipedfilename );
  56. List. Add (upzipedfilename); // put the file name in a list for return calls.
  57. If (zipentry. isdirectory () {// if it is a directory, create it
  58. String name = zipentry. getname ();
  59. Name = Name. substring (0, name. Length ()-1 );
  60. File F = new file (outputdirectory + file. Separator + name );
  61. F. mkdir ();
  62. System. Out. println ("create directory:" + outputdirectory + file. Separator + name );
  63. } Else {
  64. String filename = zipentry. getname ();
  65. Filename = filename. Replace ('//','/');
  66. If (filename. indexof ("/")! =-1 ){
  67. Createdirectory (outputdirectory, filename. substring (0, filename. lastindexof ("/")));
  68. Filename = filename. substring (filename. lastindexof ("/") + 1, filename. Length ());
  69. }
  70. File F = new file (outputdirectory + file. Separator + zipentry. getname ());
  71. F. createnewfile ();
  72. Inputstream in = zipfile. getinputstream (zipentry );
  73. Fileoutputstream out = new fileoutputstream (f );
  74. Byte [] by = new byte [1024];
  75. Int C;
  76. While (C = in. Read ())! =-1 ){
  77. Out. Write (by, 0, C );
  78. }
  79. Out. Close ();
  80. In. Close ();
  81. }
  82. }
  83. If (readflag) {// determine whether there are files in the zip package
  84. Sreturn [0] = "1 ";
  85. Sreturn [1] = "successful ";
  86. } Else {
  87. Sreturn [0] = "0 ";
  88. Sreturn [1] = "no file exists in the zip package ";
  89. }
  90. } Catch (exception ex ){
  91. Sreturn [0] = "0 ";
  92. Sreturn [1] = ex. getmessage ();
  93. }
  94. Return sreturn;
  95. }
  96. Public arraylist getfilenames (){
  97. Return list;
  98. }
  99. ///**
  100. // * Test Method
  101. // * @ Param ARGs
  102. //*/
  103. // Public static void main (string [] ARGs ){
  104. // String szipfilename = "D: // parttest.zip ";
  105. // String sdirectory = "D: // tmp ";
  106. // String sflag = "";
  107. // String MSG = "";
  108. // String [] sreturn;
  109. // Ziptools DBC = new ziptools ();
  110. // System. Out. println ("message: start extracting ......");
  111. // Sreturn = DBC. Unzip (szipfilename, sdirectory );
  112. // Sflag = sreturn [0];
  113. // MSG = sreturn [1];
  114. // If (sflag. Equals ("1 ")){
  115. // System. Out. println ("message: decompressed successfully ");
  116. //} Else if (sflag. Equals ("0 ")){
  117. // System. Out. println ("message: Decompression failed ");
  118. // System. Out. println ("Reason:" + MSG );
  119. //}
  120. //
  121. //}
  122. }

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.