惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
Jina AI
Jina AI
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
C
Check Point Blog
GbyAI
GbyAI

博客园 - 龚明秋

JavaScript获取客户端IP和MAC地址 java实现算术表达式求值 浅拷贝与深拷贝的实现 - 龚明秋 - 博客园 包含中文的字符串截取 Table动态增加删除行 JavaScript校验日期格式 使用过滤器来处理Session超时和权限管理 jsp页面内容导出到Excel中 - 龚明秋 - 博客园 Java读取Excel内容 Excel中如何根据身份证号码获取年龄,性别 坦克大战游戏-Java版 Java实现的简易文本编辑器 VB.NET实现的文本编辑器 串的模式匹配算法之二:首尾匹配算法 串的模式匹配算法之一:简单算法 Union Two Lists 有趣的猜数字游戏 用C#实现的简易计算器 用C#实现约瑟夫问题
Java批量下载生成zip文件
龚明秋 · 2009-08-11 · via 博客园 - 龚明秋

经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,为了将问题简化,新建java项目,在根目录下随便放入两个文件来模拟我们要同时下载的文件(在本例中新建了result.txtsource.txt两个文件),通过如下代码即可实现同时下载这两个文件:

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream; 

public class ZipOutputStreamDemo {

    public static void main(String[] args) throws Exception {

       byte[] buffer = new byte[1024];

       //生成的ZIP文件名为Demo.zip

       String strZipName = "Demo.zip";

       ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));

       //需要同时下载的两个文件result.txt source.txt

       File[] file1 = {new File("result.txt"),new File("source.txt")};

       for(int i=0;i<file1.length;i++) {

           FileInputStream fis = new FileInputStream(file1[i]);

           out.putNextEntry(new ZipEntry(file1[i].getName()));

           int len;

           //读入需要下载的文件的内容,打包到zip文件

          while((len = fis.read(buffer))>0) {

           out.write(buffer,0,len);

          }

           out.closeEntry();

           fis.close();

       }

        out.close();

        System.out.println("生成Demo.zip成功");

    }

}