使用 LZMA SDK 在 Java 中進行壓縮匯出(POI 如何快速導百萬級資料的 Excel)
阿新 • • 發佈:2021-10-28
//https://www.cnblogs.com/operationhome/p/12253549.html //https://cloud.tencent.com/developer/ask/57625(XZ 是基於 LZMA2(LZMA 的改進版本)的檔案格式,考慮客戶對 zip 壓縮軟體支援,暫時放棄) //壓縮檔案流(可以不用儲存在本地再輸出,通過 httpServletResponse 直接輸出) ZipOutputStream zipOutputStream = new ZipOutputStream(httpServletResponse.getOutputStream());//構造流入物件 ExcelWriter excelWriter = EasyExcel.write(zipOutputStream) .excelType(ExcelTypeEnum.XLSX) .head(ContractAccrualResponse.class) .build(); try { //檔名稱 String fileName = StringUtils.join("利息計提批量匯出(", contractAccrualQueryRequest.getCorpCode(), ")", ExcelTypeEnum.XLSX.getValue());//對檔名進行編碼處理中文問題 String zipFileName = new String(("利息計提批量匯出.zip").getBytes(), StandardCharsets.UTF_8); //設定響應頭 httpServletResponse.setCharacterEncoding("UTF-8"); // httpServletResponse.setContentType("application/vnd.ms-excel"); httpServletResponse.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(zipFileName, "UTF-8"));// for (int i = 0; i < 15; i++) { // contractAccrualResponseList.addAll(contractAccrualResponseList); // } //切割資料(XLSX 單個 sheet 只能容納 1048576 行資料) int exportMaxNum = 1000000; List<List<ContractAccrualResponse>> partition = Lists.partition(contractAccrualResponseList, exportMaxNum); for (int i = 0; i < partition.size(); i++) { String sheetName = StringUtils.join("Sheet_", String.valueOf(i * exportMaxNum + 1), "_", exportMaxNum <= partition.get(i).size() ? (i + 1) * exportMaxNum : (i + 1) * exportMaxNum + partition.get(i).size()); excelWriter.write(partition.get(i), EasyExcel.writerSheet(i, sheetName).build()); } //將檔案寫入 zip 內,即將檔案進行打包 zipOutputStream.putNextEntry((new ZipEntry(fileName))); zipOutputStream.setComment(fileName); } catch (Exception exception) { log.error("exception: {}", exception.getMessage()); } finally { if (ObjectUtils.isNotEmpty(excelWriter)) { excelWriter.finish(); } if (ObjectUtils.isNotEmpty(zipOutputStream)) { zipOutputStream.finish(); } }
XZ
FileInputStream inFile = new FileInputStream("src.tar"); FileOutputStream outfile = new FileOutputStream("src.tar.xz"); LZMA2Options options = new LZMA2Options(); options.setPreset(7); // play with this number: 6 is default but 7 works better for mid sized archives ( > 8mb) XZOutputStream out = new XZOutputStream(outfile, options); byte[] buf = new byte[8192]; int size; while ((size = inFile.read(buf)) != -1) out.write(buf, 0, size); out.finish();
http://tukaani.org/xz/java.html
https://www.cnblogs.com/operationhome/p/12253549.html
http://commons.apache.org/proper/commons-compress/examples.html
https://blog.csdn.net/weixin_45492007/article/details/118069006
POI 如何快速匯出千萬級資料的 Excel
https://www.v2ex.com/t/795136
https://alibaba-easyexcel.github.io/quickstart/write.html