Java 实现文件批量文件打包下载

在Javaweb项目中经常会有根据复选框勾选后,同时下载多个文件的功能,以下代码实现了批量文件打包下载

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class Test {
    public static void main(String[] args) {
        //需要压缩的文件--包括文件地址和文件名
        String []path ={"C:\\Documents and Settings\\db2admin\\桌面\\EngineerNetworkQuery.xls",
                "C:\\Documents and Settings\\db2admin\\桌面\\NetworkInfQuery.xls"};
        // 要生成的压缩文件地址和文件名称
        String desPath = "C:\\Documents and Settings\\db2admin\\桌面\\DownLoad.zip";
        File zipFile = new File(desPath);
        ZipOutputStream zipStream = null;
        FileInputStream zipSource = null;
        BufferedInputStream bufferStream = null;
        try {
            //构造最终压缩包的输出流
            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
            for(int i =0;i                 File file = new File(path[i]);
              //将需要压缩的文件格式化为输入流
                zipSource = new FileInputStream(file);
                //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
                ZipEntry zipEntry = new ZipEntry(file.getName());
                //定位该压缩条目位置,开始写入文件到压缩包中

                zipStream.putNextEntry(zipEntry);

             //输入缓冲流
             bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
             int read = 0;
             //创建读写缓冲区
             byte[] buf = new byte[1024 * 10];
             while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
            {
                zipStream.write(buf, 0, read);
            }

            }
          
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
                //关闭流
                try {
                    if(null != bufferStream) bufferStream.close();
                    if(null != zipStream) zipStream.close();
                    if(null != zipSource) zipSource.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

你可能感兴趣的:(JAVA)