1. Generate Zip
Public Static voidMain (string[] args) {Try {//testzip ("C:\\temp.txt", "C:\\temp4.zip");//testzip ("C:\\result.txt", "C:\\temp4.zip"); //Otherwise it will be overwritten by a file. //the key to compressing multiple files: Zipoutputstream out is passed as a parameter. //a stream, otherwise there is an overlay problem, that is, each time the new one, so external.Zipoutputstream Zos =NULL; Zos=NewZipoutputstream (NewFileOutputStream ("C:\\temp5.zip")); Testzip ("C:\\temp.txt", Zos); Testzip ("C:\\result.txt", Zos); //ExternalZos.closeentry (); Zos.close (); } Catch(Exception e) {e.printstacktrace (); } }/*** Duplicate Compressed files *@paramzipentryname//Remove String zipentryname, *@paramFilePath *@paramZippath *@throwsException*/ Public Static voidTestzip (String filepath,zipoutputstream Zos)throwsexception{//Compress Package /*Zipoutputstream zos = null;*/ //Bufferedoutputstream bos = null;//file ZipFile = new file (Zippath);//if (zipfile.exists () ==false) { /*Zos = new Zipoutputstream (new FileOutputStream (Zippath));*/ //BOS = new Bufferedoutputstream (ZOS); //Buffer ...//}else{// // }File F=NewFile (FilePath); // //Create ZipFileInputStream FIS =NewFileInputStream (f);//Bufferedinputstream bis = new Bufferedinputstream (FIS); //set the file name in the. zip file//Zos.putnextentry (New ZipEntry (Zipentryname));Zos.putnextentry (NewZipEntry (F.getname ())); //Set the DeclearZos.setcomment ("by Zip test!");//byte[] b = new byte[1024];//While (true) {//int len = Bis.read (b);//if (len = =-1)//Break ;//Bos.write (b, 0, Len);//System.out.println (New String (b, 0, Len));// }//Bos.flush (); //This line is important, otherwise txt is a blank file. byte[] buffer =New byte[1024]; intLen = 0 ; //read the contents of a file, package it into a zip file while(len = fis.read (buffer)) > 0) {zos.write (buffer,0, Len); } fis.close (); /*Zos.closeentry (); Zos.close ();*/ }
2. Unzip
Public Static voidMain (string[] args)throwsException {//get a zip file instanceFile File =NewFile ("C:\\temp5.zip"); //get a ZipFile instanceZipFile ZipFile =Newzipfile (file); //Create a Zipinputstream instanceZipinputstream ZiS =NewZipinputstream (Newfileinputstream (file)); //Create a ZipEntry instance, lay the every file from//Decompress file temporarilyZipEntry entry =NULL; //a circle to get every file while((Entry = Zis.getnextentry ())! =NULL) {System.out.println ("Decompress file:" +entry.getname ()); //define the path to set the fileFile OutFile =NewFile ("c:\\zip\\" +entry.getname ()); //if the file ' s parent directory wasn ' t exits, than//Create the directory if(!Outfile.getparentfile (). exists ()) {Outfile.getparentfile (). mkdir (); } //If the file is not exits, than create the file if(!outfile.exists ()) {Outfile.createnewfile (); } //Create an input stream read fileBufferedinputstream bis =NewBufferedinputstream (Zipfile.getinputstream (entry)); //Create an output stream write fileBufferedoutputstream BOS =NewBufferedoutputstream (NewFileOutputStream (outFile)); byte[] B =New byte[1024]; while(true) { intLen =Bis.read (b); if(len = =-1) Break; Bos.write (b,0, Len); } //Close StreamBis.close (); Bos.close (); } zis.close (); }
Java generates zip compressed files, unzip files