码迷,mamicode.com
首页 > Web开发 > 详细

http zip压缩文件读取和解压

时间:2015-06-23 18:27:29      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:

准备每次从输入流读取10k,但实际每次读取长度并不总是10k,甚至可能为0,三次测试结果如下:

第一次运行结果(read 10次):

ContentLength:52450

7872

88

10240

10240

176

3472

3472

10240

3472

3178

文件名称:nvdcve-2.0-modified.xml

文件大小:711356


第二次运行结果(read 11次):

ContentLength:52450

7872

6944

3472

3472

6944

3472

3472

3472

6944

3472

2914

文件名称:nvdcve-2.0-modified.xml

文件大小:711356


第三次运行结果(read 10次):

ContentLength:52450

7872

3472

3472

3472

3472

9660

10240

10240

220

330

文件名称:nvdcve-2.0-modified.xml

文件大小:711356

URL url = new URL("http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-Modified.xml.zip");
//sun.net.www.protocol.http.HttpURLConnection
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 Chrome/31.0.1650.63 Safari/537.36");

/*1、读取压缩文件流*/
System.out.println("ContentLength:" + con.getContentLength());
InputStream is = con.getInputStream();
int len = -1; byte[] b = new byte[10240];	//准备一次读入10k
ByteArrayOutputStream os = new ByteArrayOutputStream();
while ((len = is.read(b)) > -1) {
    System.out.println(len);	//每次读取长度并不总是10k
    os.write(b, 0, len);
}
con.disconnect();

/*2、解压,可以直接从URL连接的输入流解压ZipInputStream(con.getInputStream())*/
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(os.toByteArray()));
ZipEntry ze = zis.getNextEntry();
System.out.println("文件名称:" + ze.getName());
ByteArrayOutputStream zos = new ByteArrayOutputStream();
while ((len = zis.read(b)) > -1) {
    zos.write(b, 0, len);
}
System.out.println("文件大小:" + zos.size());
//System.out.println(zos.toString());//打印文件内容


http zip压缩文件读取和解压

标签:

原文地址:http://my.oschina.net/h2do/blog/469778

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!