Cause: A time when you want to modify some files in a zip on the server
Originally thought very simple thing, looked for a lot of code on the net, the result is not very ideal.
Implementing objects: For various types of folders (including sub-files or subfolders)
Here is a description of their own comprehensive online code to write their own
The first thing to pour into a jar package: (0 points download)
Package Com.tzx.test2;import Java.io.File; Import java.io.FileNotFoundException; Import java.io.IOException; Import Org.apache.tools.zip.ZipOutputStream; Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import Java.io.InputStream; Import java.util.Enumeration; Import Org.apache.tools.zip.ZipEntry; Import Org.apache.tools.zip.ZipFile; /** * Compress unzip zip file * @author stven_king * @date: April 1, 2015 PM 4:09:11 * */public class Ziputil {/** * compressed folder * @param files Dirth destination File e:/a/b * @param zipfilepath The path name of the compressed file E:/a/b.zip * @param deletefile whether to delete folders and directories * @return Boolean compression succeeded * */public static Boolean Dozip (String Filesdirpath, String Zipfilepath,boolean deletefile) {return Dozip (new File (Filesdirpath), zipfilepath,deletefile); } private static Boolean Dozip (File inputfile, String zipfilename,boolean deletefile) {zipoutputstream ou t = null; try {out = new Zipoutputstream (new FileOutputStream (Zipfilename)); Boolean result = Dozip (out, Inputfile, ""); return result; } catch (FileNotFoundException ex) {ex.printstacktrace (); return false; } catch (IOException ex) {ex.printstacktrace (); return false; } finally {try {out.close (); if (DeleteFile) {deletedir (inputfile);} } catch (IOException ex) {ex.printstacktrace (); return false; }}} private static Boolean Dozip (Zipoutputstream out, File F, String Base) {try { if (F.isdirectory ()) {file[] fl = F.listfiles (); if (Fl.length = = 0) {out.putnextentry (new Org.apache.tools.zip.ZipEntry (base + "/")); } for (int i = 0; i < fl.length; i++) {Dozip (out, fl[i], base + "/" + FL[I].getname ()); }} else {Out.putnextentry (base)); FileInputStream in = new FileInputStream (f); int b; while ((b = In.read ())! =-1) {out.write (b); } in.close (); } return true; } catch (IOException ex) {ex.printstacktrace (); return false; }}/** * Unzip zip file * @param zipfilepath Compressed file path name E:/b/a.zip * @param base unzip file storage path E:/B/C * @param deletefile Whether to delete the compressed package * @return Boolean to extract success * */public static Boolean UnZip (String Zipfilepath, String Base, Boolean DeleteFile) {try {File file = new file (Zipfilepath); if (!file.exists ()) {throw new RuntimeException ("Unzip file does not exist!"); } zipfile ZipFile = new ZipFile (file); Enumeration E = Zipfile.getentries (); while (E.hasmoreelements ()) {ZipEntry ZipEntry = (zipentry) e.nextelement (); if (Zipentry.isdirectory ()) {String name = Zipentry.getname (); Name = name.substring (0, Name.length ()-1); File F = new file (base+ "/" + name); F.mkdirs (); } else {file F = new File (base + zipentry.getname ()); F.getparentfile (). Mkdirs (); F.createnewfile (); InputStream is = Zipfile.getinputstream (zipentry); FileOutputStream fos = new FileOutputStream (f); int length = 0; Byte[] B = new byte[1024]; while (length = Is.read (b, 0, 1024x768))! =-1) {fos.write (b, 0, length); } is.close (); Fos.close (); }} if (ZipFile! = null) {zipfile.close (); } if (DeleteFile) {file.deleteonexit (); } return true; } catch (IOException ex) {return false; }}/** * Recursively delete all files and subdirectories under directory * @param dir file directory to be deleted * @return Boolean */private static Boolean Deletedir (File dir) {if (Dir.isdirectory ()) {string[] children = dir.list ();//recursively delete subdirectories in directory for (int i=0; i<children.length; i++) {Boolean success = Deletedir (New File (dir, children[i])) ; if (!success) {return false; }}}//directory is empty at this time, you can delete return Dir.delete (); }}
Implementing Java Code Compression decompression