阅读背景:

批量下载文件,将文件打包成压缩文件

来源:互联网 

 

批量压缩文件

 public static byte[] zipFiles(List<File> listfiles) throws IOException {
        byte[] buf = new byte[1024];
        ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(outPut);
        List<String> nameList = new ArrayList<>();
        for (File file : listfiles) {
            InputStream input = new ByteArrayInputStream(file.getContent());

            if (nameList.size() > 0 && CommonUtil.existsStrList(nameList, file.getFileName())) {
                out.putNextEntry(new ZipEntry(CommonUtil.returnChangeFileName(file.getFileName(), nameList.size())));
            } else {
                nameList.add(file.getFileName());
                out.putNextEntry(new ZipEntry(file.getFileName()));
            }

            int len;
            while ((len = input.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            input.close();
        }
        out.close();
        return outPut.toByteArray();
    } public static byte[] zipF



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: