Java implements zip decompression and javazip Decompression

Source: Internet
Author: User

Java implements zip decompression and javazip Decompression

The program implements ZIP compression. There are two parts: compression and decompression)

General functions include using JAVA core technologies such as polymorphism and recursion to compress and decompress a single file and any cascading folder. You must customize the source input path and target output path in the code.

1. package com. han; 2. 3. import java. io. *; 4. import java.util.zip. *; 5. 6. /** 7. * The program implements ZIP compression. There are two parts: compression and decompression. * <p> 9. * General functions include using JAVA core technologies such as polymorphism and recursion to compress and decompress a single file and any cascading folder. You must customize the source input path and target output path in the code. 10. * <p> 11. * In this code segment, the compression part is implemented. For the Decompression part, see Decompression in this package. 12.*13. * @ author HAN 14.*15. */16. 17. public class MyZipCompressing {18. private int k = 1; // defines the number of recursion variable 19. 20. public MyZipCompressing () {21. // TODO Auto-generated constructor stub 22 .} 23. 24. /** 25. * @ param args 26. */27. public static void main (String [] args) {28. // TODO Auto-generated method stub 29. myZipCompressing book = new MyZipCompressing (); 30. try {31. book.zip ("C: \ U Sers \ Gaowen \ Desktop \ ZipTestCompressing.zip ", 32. new File ("C :\\ Users \ Gaowen \ Documents \ Tencent Files"); 33 .} catch (Exception e) {34. // TODO Auto-generated catch block 35. e. printStackTrace (); 36 .} 37. 38 .} 39. 40. private void zip (String zipFileName, File inputFile) throws Exception {41. system. out. println ("compressing... "); 42. zipOutputStream out = new ZipOutputStream (new FileOutputStream (43. zipFileName); 44. bufferedOutputStream bo = new BufferedOutputStream (out); 45. zip (out, inputFile, inputFile. getName (), bo); 46. bo. close (); 47. out. close (); // close the output stream 48. system. out. println ("COMPRESSED"); 49 .} 50. 51. private void zip (ZipOutputStream out, File f, String base, 52. bufferedOutputStream bo) throws Exception {// method overload 53. if (f. isDirectory () {54. file [] fl = f. listFiles (); 55. if (f L. length = 0) {56. out. putNextEntry (new ZipEntry (base + "/"); // create a zip file and compress it into the base 57. system. out. println (base + "/"); 58 .} 59. for (int I = 0; I <fl. length; I ++) {60. zip (out, fl [I], base + "/" + fl [I]. getName (), bo); // recursively traverse subfolders 61 .} 62. system. out. println ("th" + k + "th recursion"); 63. k ++; 64 .} else {65. out. putNextEntry (new ZipEntry (base); // create a zip file and compress it into the base point 66. system. out. println (base); 67. fil EInputStream in = new FileInputStream (f); 68. BufferedInputStream bi = new BufferedInputStream (in); 69. int B; 70. while (B = bi. read ())! =-1) {71. bo. write (B); // write the byte stream to the current zip directory 72 .} 73. bi. close (); 74. in. close (); // close the input stream 75 .} 76 .} 77 .}
1. package com. han; 2. 3. import java. io. *; 4. import java.util.zip. *; 5./** 6. * implements ZIP compression. Divided into two parts: 7. * compression and decompression 8. * <p> 9. * General functions include using JAVA core technologies such as polymorphism and recursion to compress and decompress a single file and any cascading folder. 10. * You must customize the source input path and target output path in the code. 11. * <p> 12. * In this code segment, extract the compressed part. For the compressed part, see the compression part in this package. 13. * @ author HAN 14.*15. */16. public class CopyOfMyzipDecompressing {17. 18. public static void main (String [] args) {19. // TODO Auto-generated method stub 20. long startTime = System. currentTimeMillis (); 21. try {22. zipInputStream Zin = new ZipInputStream (new FileInputStream (23. "C: \ Users \ HAN \ Desktop \ stock \ SpectreCompressed.zip"); // enter the source zip path 24. bufferedInputStream Bin = new BufferedInpu TStream (Zin); 25. string Parent = "C :\\ Users \ HAN \ Desktop"; // output path (Folder directory) 26. file Fout = null; 27. zipEntry entry; 28. try {29. while (entry = Zin. getNextEntry ())! = Null &&! Entry. isDirectory () {30. Fout = new File (Parent, entry. getName (); 31. if (! Fout. exists () {32. (new File (Fout. getParent ())). mkdirs (); 33 .} 34. fileOutputStream out = new FileOutputStream (Fout); 35. bufferedOutputStream Bout = new BufferedOutputStream (out); 36. int B; 37. while (B = Bin. read ())! =-1) {38. bout. write (B); 39 .} 40. bout. close (); 41. out. close (); 42. system. out. println (Fout + "decompressed"); 43 .} 44. bin. close (); 45. zin. close (); 46 .} catch (IOException e) {47. // TODO Auto-generated catch block 48. e. printStackTrace (); 49 .} 50 .} catch (FileNotFoundException e) {51. // TODO Auto-generated catch block 52. e. printStackTrace (); 53 .} 54. long endTime = System. currentTimeMillis (); 55. system. out. println ("Time consumed:" + (endTime-startTime) + "ms"); 56 .} 57. 58 .}

 

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.