The program implements ZIP compression. Divided into 2 parts: compression (compression) and decompression (decompression)
Approximate functions include the use of polymorphic, recursive and other Java core technology, can be a single file and any Cascade folder compression and decompression. You need to customize the source input path and the target output path in your code.
1. PackageCom.han; 2. 3.ImportJava.io.*; 4.Importjava.util.zip.*; 5. 6./**7. * The program implemented ZIP compression. Divided into 2 parts: compression (compression) and decompression (decompression) 8. * <p> 9. * Approximate functions include the use of polymorphic, recursive and other Java core technology, can be a single file and any Cascade folder compression and decompression. You need to customize the source input path and the target output path in your code. * <p> 11. * In this section of the code, the implementation is the compression section, the decompression section see the Decompression section of this package. 12. * 13. * @authorHAN 14. * 15. */16. 17. Public classmyzipcompressing {18.Private intK = 1;//defining recursive number variables19. 20. Publicmyzipcompressing () {21st.//TODO auto-generated Constructor stub22. } 23. 24./***@paramargs 26. */27. Public Static voidMain (string[] args) {28.//TODO auto-generated Method Stubmyzipcompressing book =Newmyzipcompressing (); 30.Try { Book.zip ("C:\\users\\gaowen\\desktop\\ziptestcompressing.zip", 32.NewFile ("C:\\users\\gaowen\\documents\\tencent Files")); 33.}Catch(Exception e) {34.//TODO auto-generated Catch block35. E.printstacktrace (); 36. } 37. 38. } 39. 40.Private voidZip (String zipfilename, File inputfile)throwsException {System.out.println ("in compression ..."); Zipoutputstream out =NewZipoutputstream (NewFileOutputStream (43. Zipfilename)); Bufferedoutputstream bo =NewBufferedoutputstream (out); 45. Zip (out, Inputfile, Inputfile.getname (), Bo); 46. Bo.close (); Out.close ();//output stream offSYSTEM.OUT.PRINTLN ("Compression complete"); 49. } 50. 51.Private voidZip (Zipoutputstream out, File F, String base,Bufferedoutputstream Bo)throwsException {//Method Overloading53.if(F.isdirectory ()) {File[] fl =F.listfiles (); 55.if(Fl.length = = 0) { Out.putnextentry (NewZipEntry (base + "/"));//Create a Zip compression entry point baseSystem.out.println (base + "/"); 58. } 59. for(inti = 0; i < fl.length; i++) { Zip (out, fl[i], base + "/" + Fl[i].getname (), Bo);//recursively traverse subfolders61. } System.out.println ("First" + K + "sub-recursion")); k++.; 64.}Else { Out.putnextentry (NewZipEntry (base));//Create a Zip compression entry point base66. System.out.println (base); FileInputStream in =NewFileInputStream (f); Bufferedinputstream bi =NewBufferedinputstream (in); 69.intb; 70. while((b = Bi.read ())! =-1) { Bo.write (b);//writes a stream of bytes to the current zip directory72. } 73. Bi.close (); In.close ();//input stream off75. } 76. } 77.}
1. PackageCom.han; 2. 3.ImportJava.io.*; 4.Importjava.util.zip.*; 5./**6. * The program implemented ZIP compression. Divided into 2 parts: 7. * Compression (compression) with decompression (decompression) 8. * <p> 9. * Approximate functions include the use of polymorphic, recursive and other Java core technology, can be a single file and any Cascade folder compression and decompression. 10. * You need to customize the source input path and the target output path in your code. One. * <p> 12. * In this section of the code, the implementation is the decompression section, the compression section is shown in the compression section of this package. *@authorHAN 14. * 15. */16. Public classcopyofmyzipdecompressing {17. 18. Public Static voidMain (string[] args) {19.//TODO auto-generated Method Stub20.LongStarttime=System.currenttimemillis (); 21st.Try { Zipinputstream zin=NewZipinputstream (NewFileInputStream ("C:\\users\\han\\desktop\\stock\\spectrecompressed.zip"));//input source Zip pathBufferedinputstream bin=NewBufferedinputstream (Zin); String parent= "C:\\users\\han\\desktop";//Output Path (folder directory). File fout=NULL; 27. ZipEntry entry; 28.Try { 29. while((Entry = Zin.getnextentry ())! =NULL&&!entry.isdirectory ()) { fout=.NewFile (Parent,entry.getname ()); 31.if(!fout.exists ()) { 32. (NewFile (Fout.getparent ())). Mkdirs (); 33. } FileOutputStream out=NewFileOutputStream (Fout); Bufferedoutputstream bout=NewBufferedoutputstream (out); 36.intb; 37. while((B=bin.read ())!=-1){ 38. Bout.write (b); 39. } 40. Bout.close (); 41. Out.close (); System.out.println (fout+ "Decompression succeeded"); 43. } 44. Bin.close (); 45. Zin.close (); 46.}Catch(IOException e) {47.//TODO auto-generated Catch block48. E.printstacktrace (); 49. } 50.}Catch(FileNotFoundException e) {51.//TODO auto-generated Catch block52. E.printstacktrace (); 53. } 54.LongEndtime=System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Endtime-starttime) + "MS"); 56. } 57. 58.}
Java Implementation Zip decompression