JAVA生成多级目录并压缩下载

    public void export(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        //写入台账信息到excel ,下载文件到zip
        List<Map<String, String>> dataList = new ArrayList() {{
            add(new HashMap() {{
                put("archiveNo", "0001-2010-Y-0004");
                put("name", "区档案局传达学习“‘四责协同’机制建设推进会”精神");
                put("code", "0001");
            }});
            add(new HashMap() {{
                put("archiveNo", "0001-2010-Y-0003");
                put("name", "加强纪律教育 强化规矩意识 营造知敬畏、存戒惧、守底线的良好氛围");
                put("code", "0001");
            }});
        }};
        Map<String, String> contract = dataList.get(0);
        String fileName = contract.get("archiveNo") + ".zip";
        // 创建临时文件
        File zipFile;
        try {
            zipFile = File.createTempFile(contract.get("archiveNo") + "等" + dataList.size() + "条记录", ".zip");
            FileOutputStream f = new FileOutputStream(zipFile);
            // 作用是为任何OutputStream产生校验和
            //第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种)

            CheckedOutputStream checkedOutputStream = new CheckedOutputStream(f, new Adler32());
            // 用于将数据压缩成Zip文件格式
            ZipOutputStream zos = new ZipOutputStream(checkedOutputStream);
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建工作表对象
            HSSFSheet sheet = workbook.createSheet();
            //创建工作表的行
            //设置第一行,从零开始
            HSSFRow row = sheet.createRow(0);
            //第一行第一列为日期 这里的列 后面再加
            row.createCell(0).setCellValue("档号");
            row.createCell(1).setCellValue("题名");
            row.createCell(2).setCellValue("合同编号");
            //设置sheet的Name
            workbook.setSheetName(0, "条目");
            for (int k = 0; k < dataList.size(); k++) {
                Map<String, String> contract2 = dataList.get(k);
                HSSFRow row1 = sheet.createRow(k + 1);
                row1.createCell(0).setCellValue(contract2.get("archiveNo"));
                row1.createCell(1).setCellValue(contract2.get("name"));
                row1.createCell(2).setCellValue(contract2.get("code"));
                //-------- 写入数据到excel end
                //以档号命名文件夹
                zos.putNextEntry(new ZipEntry(contract2.get("archiveNo") + "\\"));
                String filePath = contract2.get("archiveNo") + "\\" + "文件" + "\\";
                zos.putNextEntry(new ZipEntry(filePath));
                // List<Map<String, String>> maps1 = contractFileService.selectByContractId(id);
                List<Map<String, String>> maps1 = new ArrayList(){{
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\开放鉴定WBS计划.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\附件7:市外差旅费报销单.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\附件6:市外差旅费报销单.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\开放鉴定相关sql.txt");
                    }});
                }};
                for (int i = 0; i < maps1.size(); i++) {
                    Map m = maps1.get(i);
                    String path = (String) m.get("path");
                    //name 后面追加 变成唯一名字
                    String name = path.substring(path.lastIndexOf("\\") + 1);
                    //在挡号下面创建文件
                    zos.putNextEntry(new ZipEntry(filePath + name));
                    InputStream inputStream = new FileInputStream(path);
                    int bytesRead = 0;
                    // 向压缩文件中输出数据
                    while ((bytesRead = inputStream.read()) != -1) {
                        zos.write(bytesRead);
                    }
                    inputStream.close();
                }
                //写入数据到 档号\\附件 这个文件夹里
                // List<Map<String, String>> maps2 = contractAttachmentService.selectByContractId(id);
                List<Map<String, String>> maps2 = new ArrayList(){{
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\开放鉴定WBS计划.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\附件7:市外差旅费报销单.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\附件6:市外差旅费报销单.xlsx");
                    }});
                    add(new HashMap(){{
                        put("path", "C:\\Users\\Barlow\\Desktop\\开放鉴定相关sql.txt");
                    }});
                }};
                String attachmentPath = contract2.get("archiveNo") + "\\" + "附件" + "\\";
                for (Map m : maps2) {
                    String path = (String) m.get("path");
                    String name = path.substring(path.lastIndexOf("\\") + 1);
                    zos.putNextEntry(new ZipEntry(attachmentPath + name));
                    InputStream inputStream = new FileInputStream(path);
                    int bytesRead = 0;
                    // 向压缩文件中输出数据
                    while ((bytesRead = inputStream.read()) != -1) {
                        zos.write(bytesRead);
                    }
                    inputStream.close();
                }
                // 当前文件写完
                zos.closeEntry();
            }
            zos.putNextEntry(new ZipEntry("条目.xls"));
            InputStream inputStream = new ByteArrayInputStream(workbook.getBytes());
            int bytesRead;
            // 向压缩文件中输出数据
            while ((bytesRead = inputStream.read()) != -1) {
                zos.write(bytesRead);
            }
            inputStream.close();
            zos.close();
            String header = httpServletRequest.getHeader("User-Agent").toUpperCase();
            if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
                fileName = URLEncoder.encode(fileName, "utf-8");
                //IE下载文件名空格变+号问题
                fileName = fileName.replace("+", "%20");
            } else {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            }
            httpServletResponse.reset();
            httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
            httpServletResponse.setHeader("Location", fileName);
            httpServletResponse.setHeader("Cache-Control", "max-age=0");
            httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            FileInputStream fis = new FileInputStream(zipFile);
            BufferedInputStream buff = new BufferedInputStream(fis);
            BufferedOutputStream out = new BufferedOutputStream(httpServletResponse.getOutputStream());
            byte[] car = new byte[4096];
            int l = 0;
            while (l < zipFile.length()) {
                int j = buff.read(car, 0, 1024);
                l += j;
                out.write(car, 0, j);
            }
            // 关闭流
            fis.close();
            buff.close();
            out.close();
            // 删除临时文件
            zipFile.delete();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值