Java.util.zip Gets the zip file entry InputStream

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.