导出zip文件

思路:建临时文件夹将需要导出的数据添加到文件夹中,然后将文件夹打包以流的形式返给前端,最后将生成的文件夹以及zip包删除。
1、ZipUtil工具包
`package com.tincher.oa.common.util;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**

  • @author DHC
  • @date 2022-3-14 8:49
  •  压缩zip工具类
    

*/
public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024;

/**
 * 压缩成ZIP 方法1
 *
 * @param srcDir           压缩文件夹路径
 * @param out              压缩文件输出流
 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
 *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
 * @throws RuntimeException 压缩失败会抛出运行时异常
 */
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException {
    long start = System.currentTimeMillis();
    ZipOutputStream zos = null;
    try {
        zos = new ZipOutputStream(out);
        File sourceFile = new File(srcDir);
        compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
        long end = System.currentTimeMillis();
        System.out.println("压缩完成,耗时:" + (end - start) + " ms");
    } catch (Exception e) {
        throw new RuntimeException("zip error from ZipUtils", e);
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 压缩成ZIP 方法2
 *
 * @param srcFiles 需要压缩的文件列表
 * @param out      压缩文件输出流
 * @throws RuntimeException 压缩失败会抛出运行时异常
 */
public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
    long start = System.currentTimeMillis();
    ZipOutputStream zos = null;
    try {
        zos = new ZipOutputStream(out);
        for (File srcFile : srcFiles) {
            byte[] buf = new byte[BUFFER_SIZE];
            zos.putNextEntry(new ZipEntry(srcFile.getName()));
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            in.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("压缩完成,耗时:" + (end - start) + " ms");
    } catch (Exception e) {
        throw new RuntimeException("zip error from ZipUtils", e);
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 递归压缩方法
 *
 * @param sourceFile       源文件
 * @param zos              zip输出流
 * @param name             压缩后的名称
 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
 *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
 * @throws Exception
 */
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception {
    byte[] buf = new byte[BUFFER_SIZE];
    if (sourceFile.isFile()) {
        // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
        zos.putNextEntry(new ZipEntry(name));
        // copy文件到zip输出流中
        int len;
        FileInputStream in = new FileInputStream(sourceFile);
        while ((len = in.read(buf)) != -1) {
            zos.write(buf, 0, len);
        }
        // Complete the entry
        zos.closeEntry();
        in.close();
    } else {
        File[] listFiles = sourceFile.listFiles();
        if (listFiles == null || listFiles.length == 0) {
            // 需要保留原来的文件结构时,需要对空文件夹进行处理
            if (KeepDirStructure) {
                // 空文件夹的处理
                zos.putNextEntry(new ZipEntry(name + "/"));
                // 没有文件,不需要文件的copy
                zos.closeEntry();
            }

        } else {
            for (File file : listFiles) {
                // 判断是否需要保留原来的文件结构
                if (KeepDirStructure) {
                    // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                    // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                    compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                } else {
                    compress(file, zos, file.getName(), KeepDirStructure);
                }

            }
        }
    }
}

/**
 * 递归删除不为空的文件夹
 * @param file
 */
public static void deleteFile(File file){
    long start = System.currentTimeMillis();
    if(file.isFile()){//表示该文件不是文件夹
        file.delete();
    }else{
        //首先得到当前的路径
        String[] childFilePaths = file.list();
        for(String childFilePath : childFilePaths){
            File childFile=new File(file.getAbsolutePath()+"\\"+childFilePath);
            deleteFile(childFile);
        }
        file.delete();
    }
    long end = System.currentTimeMillis();
    System.out.println("删除完成,耗时:" + (end - start) + " ms");
}

public static void fileToZip(String filePath,ZipOutputStream zipOut) throws IOException {
    // 需要压缩的文件
    File file = new File(filePath);
    // 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName
    String fileName = file.getName();
    FileInputStream fileInput = new FileInputStream(filePath);
    // 缓冲
    byte[] bufferArea = new byte[1024 * 10];
    BufferedInputStream bufferStream = new BufferedInputStream(fileInput, 1024 * 10);
    // 将当前文件作为一个zip实体写入压缩流,fileName代表压缩文件中的文件名称
    zipOut.putNextEntry(new ZipEntry(fileName));
    int length = 0;
    // 最常规IO操作,不必紧张
    while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
        zipOut.write(bufferArea, 0, length);
    }
    //关闭流
    fileInput.close();
    // 需要注意的是缓冲流必须要关闭流,否则输出无效
    bufferStream.close();
    // 压缩流不必关闭,使用完后再关
}

}
2、导出Excel工具包
package com.tincher.oa.common.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

/**

  • @author DHC
  • @date 2022-3-15 16:19
    */
    public class ExcelUtils {
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
    // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
    if (wb == null) {
    wb = new HSSFWorkbook();
    }
    // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
    HSSFSheet sheet = wb.createSheet(sheetName);
    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
    HSSFRow row = sheet.createRow(0);
    // 第四步,创建单元格,并设置值表头 设置表头居中
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
    //设置单元格宽度自适应,在此基础上把宽度调至1.5倍
    for (int i = 0; i < title.length; i++) {
    sheet.autoSizeColumn(i, true);
    sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 15 / 10);
    }
    // 声明列对象
    HSSFCell cell = null;
    // 创建标题
    for (int i = 0; i < title.length; i++) {
    cell = row.createCell(i);
    cell.setCellValue(title[i]);
    cell.setCellStyle(style);
    sheet.autoSizeColumn(i);
    sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10);
    }
    // 创建内容
    for (int i = 0; i < values.length; i++) {
    row = sheet.createRow(i + 1);
    for (int j = 0; j < values[i].length; j++) {
    // 将内容按顺序赋给对应的列对象
    row.createCell(j).setCellValue(values[i][j]);
    }
    }
    return wb;
    }
    }
    3、Impl层 @Override
    public void exportZip(HttpServletRequest request, HttpServletResponse response) {
    long start = System.currentTimeMillis();
    //组装资料查询条件
    Specification specification = new Specification() {
    @Override
    public Predicate toPredicate(Root root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    List predicates = new ArrayList<>();
    Predicate[] predicateOr = null;
    List listOr = new ArrayList();
    predicates.add(criteriaBuilder.equal(root.get("isDelete"), 0));//未删除
    Predicate[] predicateAnd = new Predicate[predicates.size()];
    if (predicateOr != null) {
    return criteriaQuery.where(criteriaBuilder.and(predicates.toArray(predicateAnd)), criteriaBuilder.or(listOr.toArray(predicateOr))).getRestriction();
    } else {
    return criteriaQuery.where(criteriaBuilder.and(predicates.toArray(predicateAnd))).getRestriction();
    }
    }
    };
    try {
    //资料信息
    List dataUploads = dataUploadRepository.findAll(specification);
    List projectIds = dataUploads.stream().map(DataUpload::getProjectId).collect(Collectors.toList());//资料相关联的工程id
    //资料相关联的工程信息
    List projects = projectRepository.findAllByIdIn(projectIds);
    Map<String, YcnyProject> projectMap = projects.stream().collect(Collectors.toMap(YcnyProject::getId, Function.identity()));
    List dataUploadVos = CopyBeanUtil.convertList(dataUploads, DataUploadVo.class);
    List fileIdList = new ArrayList<>();
    for (DataUploadVo d : dataUploadVos) {
    //工程名称
    YcnyProject ycnyProject = projectMap.get(d.getProjectId());
    d.setProjectName(ycnyProject.getProjectname());
    //资料关联的附件id
    if (StringUtils.isNotEmpty(d.getFileId())) {
    String fileId = d.getFileId();
    String[] split = fileId.split(",");
    List list = Arrays.asList(split);
    fileIdList.addAll(list);
    }
    switch (d.getType()) {
    case "1":
    d.setType("施工资料");
    break;
    case "2":
    d.setType("监理资料");
    break;
    case "3":
    d.setType("设计资料");
    break;
    case "4":
    d.setType("验收资料");
    break;
    case "5":
    d.setType("检测资料");
    break;
    default:
    d.setType(null);
    break;
    }
    }
    //附件信息
    List files = reporFilesRepository.findAllByIdIn(fileIdList);
    Map<String, YcnyReportFiles> filesMap = files.stream().collect(Collectors.toMap(YcnyReportFiles::getId, Function.identity()));
    //建总文件夹:D:/YCNYOA_File/upload/zlgl
    String pathName = filePath + "zlgl";
    File file = new File(pathName);
    if (!file.exists()) {
    //如果文件夹不存在就创建
    file.mkdir();
    } else {
    //如果存在该文件夹,就删除了再创建
    ZipUtils.deleteFile(file);
    file.mkdir();
    }
    //将日志信息写入到Excel中,并将Excel表放到新建的文件夹中
    String resultName = "";
    String[] title = {"序号", "名称", "所属工程", "资料描述", "资料类型"};
    String sheetName = "资料管理表";
    String[][] content = new String[dataUploadVos.size()][5];
    for (int i = 0; i < dataUploadVos.size(); i++) {
    content[i][0] = String.valueOf(i + 1);
    content[i][1] = dataUploadVos.get(i).getName();
    content[i][2] = dataUploadVos.get(i).getProjectName();
    content[i][3] = dataUploadVos.get(i).getDescribe();
    content[i][4] = dataUploadVos.get(i).getType();
    }
    HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);
    String excelName = "zl.xlsx";
    String savePath = pathName + "/" + excelName;
    System.out.print(savePath);
    OutputStream os = new FileOutputStream(savePath); // 保存到当前路径savePath
    wb.write(os);
    os.flush();
    os.close();
    //遍历存在日志的工程进行分组
    for (YcnyProject y : projects) {
    //每一个工程建一个文件夹
    long l = System.currentTimeMillis();
    File f = new File(pathName + "/" + y.getProjectname() + l);
    f.mkdir();
    //该项目下的所有日志
    List dataUploadList = dataUploads.stream().filter(dataUpload -> y.getId().equals(dataUpload.getProjectId())).collect(Collectors.toList());
    for (DataUpload d : dataUploadList) {
    String fileId = d.getFileId();
    String[] split = fileId.split(",");
    List fileIds = Arrays.asList(split);
    for (String id : fileIds) {
    YcnyReportFiles ycnyReportFiles = filesMap.get(id);
    if (ycnyReportFiles != null){
    //组装下载地址
    String filepath = ycnyReportFiles.getFilepath();
    String filename = ycnyReportFiles.getFilename();
    //将文件从原地址拷贝到新建的文件夹:pathName + "/" + y.getProjectname() + l
    File ff = new File(filePath + "/" + filepath);//原文件路径:filePath + "/" + filepath
    if (ff.exists()) {//判断原文件是否存在,如果存在就向下执行
    InputStream in = new FileInputStream(filePath + "/" + filepath);
    byte[] b = new byte[in.available()];
    in.read(b);
    OutputStream out = new FileOutputStream(pathName + "/" + y.getProjectname() + l + "/" + filename);
    out.write(b);
    //关闭
    in.close();
    out.close();
    }
    }
    }
    }
    }
    //打包文件夹
    ZipUtil.zip(pathName, pathName + ".zip");
    //将文件以流的形式返给前端
    String fileName = new String(("资料管理.zip").getBytes(), "ISO-8859-1");
    response.setHeader("Content-Disposition", "attchment;filename=" + fileName);
    ServletOutputStream outputStream = response.getOutputStream();
    FileInputStream inputStream = new FileInputStream(pathName + ".zip");
    IOUtils.copy(inputStream, outputStream);
    inputStream.close();
    File fileTempZip = new File(pathName + ".zip");
    //删除zip文件
    fileTempZip.delete();
    //删除临时文件夹
    ZipUtils.deleteFile(file);
    long end = System.currentTimeMillis();
    System.out.println("完成,耗时:" + (end - start) + " ms");
    } catch (Exception e) {
    e.printStackTrace();
    }
    `
posted @ 2022-03-18 08:50  聖凯  阅读(243)  评论(0编辑  收藏  举报