0
点赞
收藏
分享

微信扫一扫

Java 将多个文件打包为压缩包进行下载

止止_8fc8 2023-11-16 阅读 43

1:工具类



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

public class ZipUtils {
    /**
     * 导出
     * @param response
     * @param files 要进行压缩的文件数组
     * @param fileNames 压缩的文件要修改成别的名子数组
     * @param zipName 压缩包名称
     * @return zip文件
     *
     */
    public static void createZipFile(HttpServletResponse response, List<File> files, List<String> fileNames, String zipName)  {
        // 输出响应
        ZipOutputStream zipStream = null;
        response.setContentType("application/x-zip-compressed");
        response.setHeader("Cache-Control", "max-age=0");
        try {
            response.setHeader("Content-Disposition ", "attachment; filename=" + URLEncoder.encode(zipName+".zip", "UTF-8") );
            //1:定义为输出流
            OutputStream os = response.getOutputStream();
            //2.把输出流给压缩流
            zipStream = new ZipOutputStream(os);
            //3.把file文件,和要修改的名称 循环写入到 FileInputStream 输入流里面
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);
                compressZip(file, zipStream, fileNames.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (zipStream != null) {
                    zipStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void compressZip(File file, ZipOutputStream zipStream, String alias) throws Exception{
        FileInputStream input = null;
        try {
            //3.1.把file文件 写入到 FileInputStream 输入流里面
            input = new FileInputStream(file);
            //3.2.把 FileInputStream 输入流 写入到 byte[] 字节中
            zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void zip(InputStream input, ZipOutputStream zipStream, String zipEntryName) throws Exception{
        //byte[] 字节
        byte[] bytes = null;
        BufferedInputStream bufferStream = null;
        try {
            if(input == null) {
                throw new Exception("获取压缩的数据项失败! 数据项名为:" + zipEntryName);
            }
            // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
            ZipEntry zipEntry = new ZipEntry(zipEntryName);
            // 定位到该压缩条目位置,开始写入文件到压缩包中
            zipStream.putNextEntry(zipEntry);
            bytes = new byte[1024 * 5];// 读写缓冲区
            bufferStream = new BufferedInputStream(input);// 输入缓冲流
            int read = 0;
            while ((read = bufferStream.read(bytes)) != -1) {
                //压缩输出流写入bytes[] 数据
                zipStream.write(bytes, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferStream)
                    bufferStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2调用工具类下载

  File file1 = new File(path1);//path1需要下载的文件路径1
  File file2 = new File(path2);//path2需要下载的文件路径2
  List<File> files = new ArrayList<>();
  List<String> fileName = new ArrayList<>();
  files.add(file1);
  fileName.add(name1);//文件path1下载后的文件名
  files.add(file2);
  fileName.add(name2);//文件path2下载后的文件名
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  String date = sdf.format(new Date());//下载后压缩包的名称
  ZipUtils.createZipFile(response,files,fileName,date);


举报

相关推荐

0 条评论