java解压zip照片_java获取图片打成压缩包

java打压缩包时很简单的,使用ZipOutputStream写出来的流就是压缩包的流了,需要获取压缩包的zip的byte[]字节流,就写到ByteArrayOutputStream上,需要写到硬盘上就写到FileOutputStream上

@Test

public void imgToZip(){

//百度随便搜的图片

List list = Arrays.asList("http://f.hiphotos.baidu.com/image/pic/item/b151f8198618367aa7f3cc7424738bd4b31ce525.jpg",

"http://a.hiphotos.baidu.com/image/pic/item/aa18972bd40735fa4e6e4b2194510fb30e24088f.jpg",

"http://c.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ced43db9a0bc003af33b87b24c.jpg");

//需要获取byte字节流就用ByteArrayOutputStream,指向内存的流可以不用关闭,指向存储卡/硬盘的流一定要关闭

//ByteArrayOutputStream out = new ByteArrayOutputStream();

try (FileOutputStream out = new FileOutputStream("D:/1.zip");

ZipOutputStream zipOutputStream = new ZipOutputStream(out)) {

zipOutputStream.setMethod(ZipOutputStream.DEFLATED);

for (int i = 0 ; i < list.size(); i++) {

try (InputStream inputStream = new URL(list.get(i)).openStream()) {

if (inputStream != null && inputStream.available() > 0) {

zipOutputStream.putNextEntry(new ZipEntry(i+".png"));

byte[] b = new byte[1024];

int l;

while ((l = inputStream.read(b)) != -1) {

zipOutputStream.write(b, 0, l);

}

zipOutputStream.closeEntry();

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值