php zip_entry_read解压代码不全,http zip压缩文件读取和解压(流每次read大小不一样)...

准备每次从输入流读取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());//打印文件内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值