java生成壓縮文件

最近寫了一個前臺選擇多個文件進行壓縮下載的功能,將多個文件壓縮成壓縮包在進行下載,下面貼代碼

@PostMapping(value = "/zip/download")
//List<ProductDTO.fileIn> bean 多個文件名格式:[{"fileName":"文件名"},{"fileName":"文件名"}]
    public void zipFile(@RequestBody List<ProductDTO.fileIn> bean, HttpServletResponse response){
        List<File> fileList = new ArrayList<File>();
        for(ProductDTO.fileIn filein: bean){
        //根據文件名獲取路徑,你按照你的獲取路徑方法
            Map<String, Object> 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<File> 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();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章