java生成压缩文件

最近写了一个前台选择多个文件进行压缩下载的功能,将多个文件压缩成压缩包在进行下载,下面贴代码

@PostMapping(value = "/zip/download")
//List bean 多个文件名格式:[{"fileName":"文件名"},{"fileName":"文件名"}]
    public void zipFile(@RequestBody List bean, HttpServletResponse response){
        List fileList = new ArrayList();
        for(ProductDTO.fileIn filein: bean){
        //根据文件名获取路径,你按照你的获取路径方法
            Map map = productService.getFileAbsolutePath(filein);
            if (map != null && map.containsKey("file")) {
            //将获取的路径循环放到fileList中
                File file = new File(String.valueOf(map.get("file")));
                fileList.add(file);
            } else {
                continue;
            }
        }
        //将文件压缩之后,压缩包放的路径
        String outZipFileName = "\\";
        outZipFileName += DateUtil.format(new Date(), "yyyy-MM-dd") + "_";
        String orgCode = sysConfigDao.unique(C.C0001).getCfgValue();
        outZipFileName += orgCode + "_";// 机构代码
        outZipFileName += String.valueOf(System.currentTimeMillis()).substring(5) + ".ZIP";
        String outZipFile = outZipFileName;
        String path = null;
        //我是跟文件放到一个文件夹中了,所以我这边是获取文件的路径
        if(!CollectionUtils.isEmpty(fileList)){
            for(File file : fileList){
                if(file.exists() || file.length() != 0){
                    path = file.getPath();
                    break;
                }
            }
        }else{
            throw new APIException(ResultCode.READ_NO);
        }
        String substring = path.substring(0,path.lastIndexOf("\\"));
        outZipFile = substring+outZipFile;
        
        try {
           //打包outZipFile 压缩包压缩到某个路径下,fileList
            zipFile(outZipFile, fileList, true);
            //下载
            try {
                productService.download(outZipFile, response);
            } catch (Exception e) {
                throw new APIException(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            log.error("FileUtil.zipFile 处理出错", e);
        }
    }
/**
     * 打包多个文件到zip
     *
     * @param outZipFile 输出打包文件(绝对路径)
     * @param files      要打包的文件
     * @param createOk   是否在同目录下产生ok文件
     */
    public static void zipFile(String outZipFile, List files, boolean createOk) {
        ZipUtil.zip(cn.hutool.core.io.FileUtil.file(outZipFile), false, files.toArray(new File[files.size()]));
        if (createOk) {
            cn.hutool.core.io.FileUtil.copy(new File(outZipFile), new File(outZipFile + ".OK"), true);
        }
    }
    //下载
public void download(String filePath, HttpServletResponse response) throws Exception {
        File file = new File(filePath);
        InputStream inputStream = new FileInputStream(file);
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("gb2312"), "ISO8859-1"));
        OutputStream os = response.getOutputStream();
        byte[] b = new byte[1024];
        int length;
        while ((length = inputStream.read(b)) > 0) {
            os.write(b, 0, length);
        }
        os.close();
        inputStream.close();
    }

你可能感兴趣的:(java)