HTTP Zip compressed file read and decompress

Source: Internet
Author: User

Prepare to read 10k each time from the input stream, but the actual length of each read is not always 10k, maybe even 0, and three test results are as follows:

First run result (read 10 times):

contentlength:52450

7872

88

10240

10240

176

3472

3472

10240

3472

3178

File name: Nvdcve-2.0-modified.xml

File Size: 711356


Second run result (read 11 times):

contentlength:52450

7872

6944

3472

3472

6944

3472

3472

3472

6944

3472

2914

File name: Nvdcve-2.0-modified.xml

File Size: 711356


Third run result (read 10 times):

contentlength:52450

7872

3472

3472

3472

3472

9660

10240

10240

220

330

File name: Nvdcve-2.0-modified.xml

File Size: 711356

Url url = new url ("Http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-Modified.xml.zip") ;//sun.net.www.protocol.http.httpurlconnectionhttpurlconnection con =  (HttpURLConnection)   Url.openconnection (); Con.setrequestproperty ("User-agent",  "mozilla/5.0 chrome/31.0.1650.63  safari/537.36 "),/*1, read compressed file stream */system.out.println (" ContentLength: " + con.getcontentlength ()); Nputstream is = con.getinputstream (); int len = -1; byte[] b =  new byte[10240];//ready to read into 10kbytearrayoutputstream os = new bytearrayoutputstream ();while  ((Len = is.read (b))  > -1)  {     System.out.println (len);//The length of each read is not always 10k    os.write (B, 0, len);} Con.disconnect ();/*2, decompression, can be extracted directly from the URL connection of the input stream decompression Zipinputstream (Con.getinputstream ()) */zipinputstream zis =  new zipinputstream (New bytearrayinputstream (OS. Tobytearray ())); Zipentry ze = zis.getnextentry (); System.out.println ("File name:"  + ze.getname ()); Bytearrayoutputstream zos = new bytearrayoutputstream ();while  (len =  Zis.read (b))  > -1)  {    zos.write (B, 0, len); SYSTEM.OUT.PRINTLN ("File Size:"  + zos.size ());//system.out.println (zos.tostring ());//Print file contents


HTTP Zip compressed file read and decompress

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.