Packagecom.test;ImportJava.io.BufferedInputStream;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportCom.tool.zip.InflaterInputStream;ImportCom.tool.zip.ZipEntry;ImportCom.tool.zip.ZipInputStream; Public classTestzip {//private static String Tempzip = "F:\\tempzip.zip"; /** * * @paramstream of input zip file *@throwsException*/ Public voidUpload (InputStream input)throwsException {//system.setproperty ("sun.zip.encoding",//system.getproperty ("sun.jnu.encoding"));Zipinputstream ZiS=Newzipinputstream (input); ZipEntry entry=NULL; while((Entry = Zis.getnextentry ())! =NULL) { //System.out.printf ("Entry information: Name%1$b, size%2$d, compression time%3$d \ n",//entry.getname (), Entry.getsize (), Entry.gettime ()); if(Entry.isdirectory ()) {//is dir//System.out.println (Entry.getname () + "is a directory");File f =NewFile ("F:" + File.separator +entry.getname ()); if(!f.exists ()) f.mkdirs (); } Else{// byte[] data = GetByte (ZiS);//gets the byte array of the current entryInputStream is=NewBytearrayinputstream (data);//converts the current entry's byte data into a InputStream streamstring[] names = Entry.getname (). Split ("/"); String Path= "F:" +File.separator; Path+=Join (names, file.separator); //System.out.println (path);File File =NewFile (path); if(!file.exists ()) {File.createnewfile (); Towrite (is, file); } } } } /*** Write bytes to file files *@paramINS *@paramfile*/ Public Static voidTowrite (inputstream ins, file file) {Try{outputstream OS=Newfileoutputstream (file); intBytesread = 0; byte[] buffer =New byte[8192]; while((bytesread = ins.read (buffer, 0, 8192))! =-1) {os.write (buffer,0, Bytesread); } os.close (); Ins.close (); } Catch(Exception e) {e.printstacktrace (); } } /*** Get entry byte[] bytes *@paramZiS *@return */ Public byte[] GetByte (Inflaterinputstream zis) {Try{bytearrayoutputstream bout=NewBytearrayoutputstream (); byte[] temp =New byte[1024]; byte[] buf =NULL; intLength = 0; while(length = zis.read (temp, 0, 1024))! =-1) {bout.write (temp,0, length); } buf=Bout.tobytearray (); Bout.close (); returnbuf; } Catch(IOException e) {e.printstacktrace (); return NULL; } } Public Staticstring Join (object[] o, String flag) {StringBuffer Str_buff=NewStringBuffer (); for(inti = 0, len = o.length; i < Len; i++) {str_buff.append (string.valueof (o[i)); if(I < len-1) str_buff.append (flag); } returnstr_buff.tostring (); } //test Method Public Static voidMain (string[] args)throwsException {testzip test=NewTestzip (); String FilePath= "F:\\test.zip"; //File File = new file (FilePath);InputStream input =NewBufferedinputstream (NewFileInputStream (FilePath)); Test.upload (input); }}
Java.util.zip Gets the zip file entry InputStream