Online about compression and decompression zip package a large pile, I casually find one. Look, according to their own needs to change a bit, share with you, I hope that the great God:
Package Com.wangpeng.utill;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException ; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.util.enumeration;import Java.util.zip.zipentry;import Java.util.zip.zipexception;import Java.util.zip.zipfile;import java.util.zip.zipoutputstream;/** * @author Wangpeng */public class ToolOfZip {/** * Unzip zip package * @param inputpath The path of the unpacked Zip package * @param targetpath the folder extracted to * @param isclear if the folder is clear */public static void UnZip (String i Nputpath,string TargetPath, Boolean isclear) {try {if (null = = TargetPath | | "". Equals (TargetPath)) {TargetPath = inputpath.substring (0,inputpath.lastindexof ("."));} ZipFile zipfile = new ZipFile (new File (InputPath)); enumeration<? Extends zipentry> Entrys = Zipfile.entries (); while (Entrys.hasmoreelements ()) {ZipEntry entry = Entrys.nextelement () ; String name = Entry.getname ();//Aaa\testdir\xxxxx\eee.txtstring strtargetpath=targetpath+ "/" +name; String strtargetdir=strtargetpath.substring (0, Strtargetpath.lastindexof (file.separator)); if (Entry.isDirectory ()) { File dir = new file (Strtargetpath), if (!dir.exists ()) {dir.mkdirs ();}} else {file Dir = new File (Strtargetdir), if (!dir.exists ()) {dir.mkdirs ();} File File = new file (Strtargetpath), if (File.exists () && isclear) {file.delete ();} if (!file.exists ()) {InputStream in = Zipfile.getinputstream (entry); Tooloffile.copyfile (in, File.getabsolutepath ());}}}} catch (Zipexception ex) {ex.printstacktrace ()} catch (IOException ex) {ex.printstacktrace ();} catch (Exception e) {E.pri Ntstacktrace ();}} /** * Create zip file * * @param sourcepath * File or folder path * @param zippath * Generated zip file exists path (contains file name) */public STA tic void Createzip (String sourcepath, String zippath) {FileOutputStream fos = null; Zipoutputstream Zos = null;try {fos = new FileOutputStream (zippath); zos = new Zipoutputstream (FOS); Writezip (new File (sour Cepath), "", Zos);} catch (FileNotFoundException e) {e.printstacktrAce ();} Finally {try {if (Zos! = null) {Zos.close ()}} catch (IOException e) {e.printstacktrace ()}}} private static void Writezip (File inFile, String parentpath,zipoutputstream zipoutstream) {if (infile.exists ()) {if (infi Le.isdirectory ()) {Parentpath + = Infile.getname () + file.separator; file[] files = infile.listfiles (); for (file file:files) {writezip (file, Parentpath, Zipoutstream);}} else {FileInputStream Fileinstream = null;try {fileinstream = new FileInputStream (inFile); ZipEntry entry = new ZipEntry (Parentpath + infile.getname ()); Zipoutstream.putnextentry (entry); byte[] Buff = new byte[ 1024];int len;while (len = fileinstream.read (buff))! =-1) {zipoutstream.write (buff, 0, Len); Zipoutstream.flush ();}} catch (FileNotFoundException e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} finally {try {if (fil Einstream! = null) {Fileinstream.close ();}} catch (IOException e) {e.printstacktrace ();}}}} public static void Main (string[] args) {String OldPath = "f:/test/ddd/"; String NewPath = "f:/test/ccc.zip";//Toolofzip.createzip (OldPath, NewPath);//toolofzip.unzip (NewPath, NULL, FALSE); System.out.println (); System.out.println ("---------OK----------");}}
Java compression and decompression zip package