其他分享
首页 > 其他分享> > 多文件byte[]压缩zip

多文件byte[]压缩zip

作者:互联网

public void downloadZipFile( HttpServletResponse response) throws IOException {
    byte[] bytes1=null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);
    //TODO 下面做循环压缩
    for(int i=1;i<=2;i++){
       bytes1 = new byte[8192];
       //添加到zip,一定要加目录
       zip.putNextEntry(new ZipEntry("test" + File.separator + "test"+i+".txt""));
       IOUtils.write(bytes1, zip);
       zip.closeEntry();
    }
    IOUtils.closeQuietly(zip);

    byte[] data = outputStream.toByteArray();

    response.reset();
    response.setContentType("application/octet-stream; charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename="+ new String(("test" + ".zip").getBytes(), "UTF-8"));

    IOUtils.write(data, response.getOutputStream());
}

标签:outputStream,zip,压缩,IOUtils,new,byte,response
来源: https://www.cnblogs.com/flameHe/p/12843591.html