java批量下载打成zip

**

java批量下载打成zip

首先引入所需jar包

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8</version>
</dependency>

传入下载流,打包文件的集合

 public static void generateZip(OutputStream os, List<File> files)  {
        ZipOutputStream out = null;
        try {
            byte[] buffer = new byte[1024];
            out = new ZipOutputStream(os);
            for (File file : files) {
                FileInputStream fis = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                out.closeEntry();
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

结束

发布了5 篇原创文章 · 获赞 0 · 访问量 99

猜你喜欢

转载自blog.csdn.net/a734475064/article/details/105271881