ImportJava.io.*;ImportJava.util.*;ImportJava.util.zip.ZipOutputStream;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile; Public classFilemgr { Publicfilemgr () {}/*** Compressed Files *@paramsrcfile file[] List of files that need to be compressed *@paramZipFile File Compressed*/ Public Static voidzipfiles (java.io.file[] srcfile, Java.io.File zipfile) {byte[] buf =New byte[1024]; Try { //Create the ZIP fileZipoutputstream out =NewZipoutputstream (NewFileOutputStream (ZipFile)); //Compress the files for(inti = 0; i < srcfile.length; i++) {FileInputStream in=NewFileInputStream (Srcfile[i]); //Add ZIP entry to output stream.Out.putnextentry (NewZipEntry (Srcfile[i].getname ())); //Transfer bytes from the file to the ZIP file intLen; while(len = In.read (buf)) > 0) {out.write (buf,0, Len); } //Complete the entryOut.closeentry (); In.close (); } //Complete the ZIP fileOut.close (); System.out.println ("Compression complete."); } Catch(IOException e) {e.printstacktrace (); } } /*** Unzip *@paramZipFile file needs to be uncompressed *@paramDescdir String After extracting the target directory*/ Public Static voidunzipfiles (java.io.File zipfile, String descdir) {Try { //Open the ZIP fileZipFile ZF =NewZipFile (ZipFile); for(Enumeration entries =zf.entries (); entries.hasmoreelements ();) { //Get the entry nameZipEntry entry =( (ZipEntry) entries.nextelement ()); String Zipentryname=Entry.getname (); InputStream in=Zf.getinputstream (entry); //System.out.println (zipentryname);OutputStream out =NewFileOutputStream (Descdir +zipentryname); byte[] Buf1 =New byte[1024]; intLen; while(len = In.read (buf1)) > 0) {out.write (BUF1,0, Len); } //Close the file and streamIn.close (); Out.close (); System.out.println ("Unzip is complete."); } } Catch(IOException e) {e.printstacktrace (); } }}
Java version compression, decompression zip