1. compress files or entire directories
ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJava.io.StringWriter;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipOutputStream; Public classZipcompressor {PrivateString mfiledest; PrivateZipoutputstream Mzipoutputstream; Private byte[] buffer =New byte[1024]; Private intRET = 0; Private Static FinalString SEPARATOR =File.separator; //Public static void Main (string[] args) {// // //zipcompress zipcompress = new Zipcompress ("E://1.zip "); //zipcompressor zipcompressor = new Zipcompressor ("1.zip"); // //Zipcompressor.add ("1.txt"); //Zipcompressor.add ("."); //Zipcompressor.add ("2.txt"); //Zipcompressor.close (); // } /*** Name of path zip target file*/ Publiczipcompressor (String path) {mfiledest=NewFile (path). GetAbsolutePath (); FileOutputStream Fos; Try{fos=NewFileOutputStream (Mfiledest,true); Mzipoutputstream=NewZipoutputstream (FOS); } Catch(FileNotFoundException e) {printstacktrace (e); } } Public voidClose () {if(Mzipoutputstream! =NULL) { Try{mzipoutputstream.close (); } Catch(IOException e) {printstacktrace (e); } Mzipoutputstream=NULL; } } /*** FilePath file or directory to be compressed, can be relative directory*/ Public voidAdd (String filePath) {Try{File File=NewFile (FilePath); String Path= ""; if(File.isdirectory ()) {FilePath=File.getabsolutepath (); if(Filepath.endswith (".")) FilePath= filepath.substring (0, Filepath.length ()-1); if(Filepath.endswith (SEPARATOR)) FilePath= filepath.substring (0, Filepath.length ()-1); //System.out.println ("FilePath:" + filePath); intpos =Filepath.lastindexof (SEPARATOR); //System.out.println (filepath.substring (0, POS)); if(filepath.substring (0, POS). Contains (SEPARATOR)) path= filepath.substring (pos + 1, Filepath.length ())+SEPARATOR; //System.out.println ("path:" + path);} Add (Mzipoutputstream, Path, FilePath); } Catch(IOException e) {printstacktrace (e); } } /*** Zos Zip Compressed destination file path of the file or directory that is to be compressed within the path to the Zip folder that you want to create*/ Private voidAdd (zipoutputstream Zos, string path, string file)throwsFileNotFoundException {Try{File Inputfile=Newfile (file); if(Inputfile.isfile ()) {Add (Zos, Path, inputfile); } Else if(Inputfile.isdirectory ()) {//System.out.println ("Add dir:" + inputfile.getname ()); for(File subFile:inputFile.listFiles ()) {if(Subfile.isdirectory ()) {String NewPath= path + subfile.getname () +SEPARATOR; Add (Zos, NewPath, Subfile.getpath ()); } Else{Add (Zos, Path, subfile); } } } } Catch(IOException e) {printstacktrace (e); } } /*** Zos Zip Compressed target file path to be created in the Zip folder within the file to be compressed*/ Private voidAdd (zipoutputstream zos, String path, file file) {FileInputStream fis=NULL; Try{path.equalsignorecase (""); //prevents the destination zip file from being packaged in its own compression packageString src =File.getabsolutepath (); //System.out.println ("src:" + src); if(Mfiledest.equalsignorecase (src)) {//System.out.println ("error! It ' s dest file! "+ src); return; } Try{zipentry ZipEntry=NewZipEntry (path +file.getname ()); Zos.putnextentry (ZipEntry); FileInputStream Fin=Newfileinputstream (file); while(ret = fin.read (buffer))! =-1) {zos.write (buffer,0, ret); } fin.close (); Zos.closeentry (); } Catch(Exception e) {printstacktrace (e); } } Catch(Exception e) {printstacktrace (e); } finally { Try { if(FIS! =NULL) Fis.close (); } Catch(IOException e) {printstacktrace (e); } } } Private voidprintstacktrace (Exception Exception) {stringwriter SW=NewStringWriter (); PrintWriter PW=NewPrintWriter (SW); Exception.printstacktrace (PW); System.out.print (Sw.tostring ()); //e.printstacktrace (); }}
Java uses a zip compressed file or an entire directory