Find these kinds of methods on the Internet, feel good, collect them, convenient for everyone to use
- JDK comes with the package java.util.zip.ZipOutputStream, shortcomings, file (folder) name with Chinese, garbled problem occurs, the implementation code is as follows
/*** Function: Compress all files in SourceDir directory into zip format, save as specified zip file *@paramSourceDir If it is a directory, Eg:d:\\myeclipse\\first\\testfile, all files under the compressed directory; * If it is a file, eg:d:\\myeclipse\\first\\testfile\\ Aa.zip, only the file is compressed *@paramZipFile the last compressed file path and name, Eg:d:\\myeclipse\\first\\testfile\\aa.zip*/ PublicFile dozip (String sourcedir, string zipfilepath)throwsIOException {File file=NewFile (SourceDir); File ZipFile=NewFile (Zipfilepath); Zipoutputstream Zos=NULL; Try { //creating a write-out flow operationOutputStream OS =NewFileOutputStream (ZipFile); Bufferedoutputstream Bos=Newbufferedoutputstream (OS); Zos=NewZipoutputstream (BOS); String BasePath=NULL; //Get directory if(File.isdirectory ()) {BasePath=File.getpath (); }Else{BasePath=file.getparent (); } zipfile (file, BasePath, Zos); }finally { if(Zos! =NULL) {zos.closeentry (); Zos.close (); } } returnZipFile;} /** * @paramSource file *@paramBasePath *@paramZos*/ Private voidZipFile (File source, String BasePath, Zipoutputstream Zos)throwsIOException {file[] files=NULL; if(Source.isdirectory ()) {Files=Source.listfiles (); } Else{Files=NewFile[1]; files[0] =source; } InputStream is=NULL; String PathName; byte[] buf =New byte[1024]; intLength = 0; Try{ for(File file:files) {if(File.isdirectory ()) {PathName= File.getpath (). substring (basepath.length () + 1) + "/"; Zos.putnextentry (NewZipEntry (pathName)); ZipFile (file, BasePath, Zos); }Else{pathName= File.getpath (). substring (basepath.length () + 1); is=Newfileinputstream (file); Bufferedinputstream bis=NewBufferedinputstream (IS); Zos.putnextentry (NewZipEntry (pathName)); while(length = Bis.read (buf)) > 0) {zos.write (buf,0, length); } } } }finally { if(Is! =NULL) {is.close (); } } }
- Using Org.apache.tools.zip.ZipOutputStream, the code is as follows
Public classZipcompressor {Static Final intBUFFER = 8192; PrivateFile ZipFile; Publiczipcompressor (String pathName) {ZipFile=NewFile (pathName); } Public voidCompress (String srcpathname) {File file=NewFile (srcpathname); if(!file.exists ())Throw NewRuntimeException (Srcpathname + "does not exist! "); Try{FileOutputStream FileOutputStream=NewFileOutputStream (ZipFile); Checkedoutputstream Cos=NewCheckedoutputstream (FileOutputStream,NewCRC32 ()); Zipoutputstream out=Newzipoutputstream (COS); String Basedir= ""; Compress (file, out, basedir); Out.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } Private voidCompress (file file, zipoutputstream out, String basedir) {/*determine whether a directory or a file*/ if(File.isdirectory ()) {System.out.println ("Compression:" + Basedir +file.getname ()); This. compressdirectory (file, out, basedir); } Else{System.out.println ("Compression:" + Basedir +file.getname ()); This. Compressfile (file, out, basedir); } } /**Compress a directory*/ Private voidcompressdirectory (File dir, zipoutputstream out, String basedir) {if(!dir.exists ())return; file[] Files=Dir.listfiles (); for(inti = 0; i < files.length; i++) { /*Recursive*/Compress (Files[i], out, Basedir+ dir.getname () + "/"); } } /**Compress a file*/ Private voidcompressfile (file file, zipoutputstream out, String basedir) {if(!file.exists ()) { return; } Try{Bufferedinputstream bis=NewBufferedinputstream (Newfileinputstream (file)); ZipEntry entry=NewZipEntry (Basedir +file.getname ()); Out.putnextentry (entry); intcount; byteData[] =New byte[BUFFER]; while(count = bis.read (data, 0, BUFFER))! =-1) {out.write (data,0, Count); } bis.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } }
- Can be implemented using ORG.APACHE.TOOLS.ANT.TASKDEFS.ZIP in Ant, which is much simpler
Public classZipcompressorbyant {PrivateFile ZipFile; Publiczipcompressorbyant (String pathName) {ZipFile=NewFile (pathName); } Public voidCompress (String srcpathname) {File Srcdir=NewFile (srcpathname); if(!srcdir.exists ())Throw NewRuntimeException (Srcpathname + "does not exist! "); Project prj=NewProject (); Zip Zip=NewZip (); Zip.setproject (PRJ); Zip.setdestfile (ZipFile); Fileset Fileset=NewFileset (); Fileset.setproject (PRJ); Fileset.setdir (Srcdir); //fileset.setincludes ("**/*.java"), including which files or folders eg:zip.setIncludes ("*.java"); //fileset.setexcludes (...); exclude which files or foldersZip.addfileset (Fileset); Zip.execute (); }} Test the Java code PackageNet.szh.zip; Public classTestzip { Public Static voidMain (string[] args) {zipcompressor Zc=NewZipcompressor ("E:\\szhzip.zip"); Zc.compress ("E:\\test"); Zipcompressorbyant Zca=NewZipcompressorbyant ("E:\\szhzipant.zip"); Zca.compress ("E:\\test"); } }
Java implementation Zip compressed file