日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

springboot Java实现多文件的zip压缩操作 + 通过浏览器下载文件的两种方式

發布時間:2024/9/27 HTML 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot Java实现多文件的zip压缩操作 + 通过浏览器下载文件的两种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注只適配utf-8的場景,待完善!
壓縮為zip文件

  • 通過java程序輸出文件
  • /*** 功能:壓縮多個文件成一個zip文件* @param srcfile:源文件列表* @param zipfile:壓縮后的文件*/ public static void zipFiles(File[] srcfile, File zipfile) {byte[] buf = new byte[1024];try {//ZipOutputStream類:完成文件或文件夾的壓縮ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));for (int i = 0; i < srcfile.length; i++) {FileInputStream in = new FileInputStream(srcfile[i]);// 給列表中的文件單獨命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();System.out.println("壓縮完成.");} catch (Exception e) {e.printStackTrace();} }

    2.1 通過瀏覽器下載文件(返回文件流) - 方式一

    /*** 功能:壓縮多個文件,輸出壓縮后的zip文件流* @param srcfile:源文件列表* @param zipFileName:壓縮后的文件名* @return*/ @GetMapping(value = "/downzip") public ResponseEntity<byte[]> zipFiles(File[] srcfile, String zipFileName) {byte[] buf = new byte[1024];// 獲取輸出流ByteArrayOutputStream bos = new ByteArrayOutputStream();try {// ZipOutputStream類:完成文件或文件夾的壓縮ZipOutputStream out = new ZipOutputStream(bos);for (int i = 0; i < srcfile.length; i++) {// 此處可用任意其他輸入流將FileInputStream取代,outputStream為其他步驟的輸出流// ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());FileInputStream in = new FileInputStream(srcfile[i]);// 給列表中的文件單獨命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();bos.close();System.out.println("壓縮完成.");} catch (Exception e) {e.printStackTrace();}// 設置http響應頭HttpHeaders header = new HttpHeaders();header.add("Content-Disposition", "attachment;filename=" + zipFileName + ".zip");return new ResponseEntity<byte[]>(bos.toByteArray(), header, HttpStatus.CREATED); }

    2.2 通過瀏覽器下載文件(返回文件流) - 方式二

    /*** 功能:壓縮多個文件,輸出壓縮后的zip文件流* @param srcfile:源文件列表* @param zipFileName:壓縮后的文件名* @param response: Http響應*/ @GetMapping(value = "/downzip") public void zipFiles(File[] srcfile, String zipFileName, HttpServletResponse response) {byte[] buf = new byte[1024];// 獲取輸出流BufferedOutputStream bos = null;try {bos = new BufferedOutputStream(response.getOutputStream());} catch (IOException e) {e.printStackTrace();}try {response.reset(); // 重點突出// 不同類型的文件對應不同的MIME類型response.setContentType("application/x-msdownload");response.setCharacterEncoding("utf-8");response.setHeader("Content-disposition", "attachment;filename=" + zipFileName + ".zip");// ZipOutputStream類:完成文件或文件夾的壓縮ZipOutputStream out = new ZipOutputStream(bos);for (int i = 0; i < srcfile.length; i++) {FileInputStream in = new FileInputStream(srcfile[i]);// 給列表中的文件單獨命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();bos.close();System.out.println("壓縮完成.");} catch (Exception e) {e.printStackTrace();} }

    以上兩種方式,都是以輸出文件流的形式,通過瀏覽器端進行下載,不同的是,第一種將流直接通過接口返回,第二種也是比較常見的一種,將文件流通過response進行輸出,兩種方式均可,這里留存記錄下。
    解壓縮zip文件

    /*** 功能:解壓縮* @param zipfile:需要解壓縮的文件* @param descDir:解壓后的目標目錄*/ public static void unZipFiles(File zipfile, String descDir) {try {ZipFile zf = new ZipFile(zipfile);for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {ZipEntry entry = (ZipEntry) entries.nextElement();String zipEntryName = entry.getName();InputStream in = zf.getInputStream(entry);OutputStream out = new FileOutputStream(descDir + zipEntryName);byte[] buf1 = new byte[1024];int len;while ((len = in.read(buf1)) > 0) {out.write(buf1, 0, len);}in.close();out.close();System.out.println("解壓縮完成.");}} catch (Exception e) {e.printStackTrace();} }

    總結

    以上是生活随笔為你收集整理的springboot Java实现多文件的zip压缩操作 + 通过浏览器下载文件的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。