Feature implementation.
Packagecom.test;ImportJava.io.File;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedInputStream;ImportJava.io.FileOutputStream;ImportJava.io.FileInputStream;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipInputStream;ImportJava.util.zip.ZipOutputStream;/*** This class implements folders compressed into Zip files and zip files extracted * *@authorAdministrator **/ Public classZip {PrivateZipinputstream Zipin;//Unzip zip PrivateZipoutputstream zipout;//Compress zip PrivateZipEntry ZipEntry; Private Static intBufSize;//size of bytes Private byte[] buf; Private intreadedbytes; PublicZip () { This(512); } PublicZip (intbufSize) { This. bufSize =bufSize; This. BUF =New byte[ This. BufSize]; } //compress files within a folder Public voidDozip (String zipdirectory) {//Zipdirectorypath: folder name that needs to be compressedfile file; File Zipdir; Zipdir=NewFile (zipdirectory); String Zipfilename= Zipdirectory + ". zip";//zip file name generated after compression Try { This. ZipOut =NewZipoutputstream (NewBufferedoutputstream (NewFileOutputStream (zipfilename)); Handledir (Zipdir, This. ZipOut); This. Zipout.close (); } Catch(Exception IoE) {ioe.printstacktrace (); } } //called by Dozip to complete the directory file read recursively Private voidHandledir (File dir, Zipoutputstream zipout)throwsException {FileInputStream Filein; file[] files; Files=Dir.listfiles (); if(Files.length = = 0) {//If the directory is empty, it is created separately. //in the ZipEntry isdirectory () method, the directory ends with "/". This. Zipout.putnextentry (NewZipEntry (dir.tostring () + "/")); This. Zipout.closeentry (); } Else{//If the directory is not empty, the directories and files are processed separately. for(File filename:files) {if(Filename.isdirectory ()) {Handledir (FileName, This. ZipOut); } Else{Filein=NewFileInputStream (fileName); String name=Dir.getname (); //The resulting compressed package is stored in the original directory This. Zipout.putnextentry (NewZipEntry (name + "/" +filename.getname (). toString ()); //This method is stored in the project directory//This.zipOut.putNextEntry (New//ZipEntry (filename.tostring ())); while(( This. readedbytes = Filein.read ( This. buf)) > 0) { This. Zipout.write ( This. buf, 0, This. readedbytes); } This. Zipout.closeentry (); } } } } //unzip the specified zip file Public voidUnZip (String unzipfilename) {//unzipfilename the zip file name to unzipFileOutputStream fileout; File file; String F= unzipfilename.substring (0, Unzipfilename.length ()-4); File FF=NewFile (f); Try { This. Zipin =NewZipinputstream (NewBufferedinputstream (NewFileInputStream (unzipfilename)); while(( This. ZipEntry = This. Zipin.getnextentry ())! =NULL) {file=NewFile ( This. Zipentry.getname ()); if( This. Zipentry.isdirectory ()) {File.mkdirs (); } Else { //if the directory for the specified file does not exist, it is created.File parent =File.getparentfile (); if(!parent.exists ()) {Parent.mkdirs (); } if(!ff.exists ()) {Ff.mkdir (); } fileout=NewFileOutputStream (f + "/" +file.getname ()); //fileout = new FileOutputStream (file); This method is stored in the project directory while(( This. readedbytes = This. Zipin.read ( This. buf)) > 0) {Fileout.write ( This. buf, 0, This. readedbytes); } fileout.close (); } This. Zipin.closeentry (); } } Catch(Exception IoE) {ioe.printstacktrace (); } } //Set Buffer size Public voidSetbufsize (intbufSize) { This. bufSize =bufSize; } //Test Zip class Public Static voidMain (string[] args)throwsException {Zip zip=NewZip (); Zip.dozip ("E:\\test");//zip.unzip ("C:\\test.zip"); }}
Java file compression and decompression