主頁 > 企業開發 > Java實作打包壓縮檔案或檔案夾生成zip以實作多檔案批量下載

Java實作打包壓縮檔案或檔案夾生成zip以實作多檔案批量下載

2023-05-31 08:41:58 企業開發

有時候在系統中需要一次性下載多個檔案,但逐個下載檔案比較麻煩,這時候,最好的解決辦法是將所有檔案打包成一個壓縮檔案,然后下載這個壓縮檔案,這樣就可以一次性獲取所有所需的檔案了,

下面是一個名為CompressUtil的工具類的代碼,它提供了一些方法來處理檔案壓縮和下載操作:

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.zip.*;

/**
 * @author fhey
 * @date 2023-05-11 20:48:28
 * @description: 壓縮工具類
 */

public class CompressUtil {

    private static final Logger logger = LoggerFactory.getLogger(CompressUtil.class);

    /**
     * 將檔案打包到zip并創建檔案
     *
     * @param sourceFilePath
     * @param zipFilePath
     * @throws IOException
     */
    public static void createLocalCompressFile(String sourceFilePath, String zipFilePath) throws IOException {
        createLocalCompressFile(sourceFilePath, zipFilePath, null);
    }

    /**
     * 將檔案打包到zip并創建檔案
     *
     * @param sourceFilePath
     * @param zipFilePath
     * @param zipName
     * @throws IOException
     */
    public static void createLocalCompressFile(String sourceFilePath, String zipFilePath, String zipName) throws IOException {
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            throw new RuntimeException(sourceFilePath + "不存在!");
        }
        if(StringUtils.isBlank(zipName)){
            zipName = sourceFile.getName();
        }
        File zipFile = createNewFile(zipFilePath + File.separator + zipName + ".zip");
        try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile)) {
            compressFile(sourceFile, fileOutputStream);
        }
    }

    /**
     * 獲取壓縮檔案流
     *
     * @param sourceFilePath
     * @return ByteArrayOutputStream
     * @throws IOException
     */
    public static OutputStream compressFile(String sourceFilePath, OutputStream outputStream) throws IOException {
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            throw new RuntimeException(sourceFilePath + "不存在!");
        }
        return compressFile(sourceFile, outputStream);
    }

    /**
     * 獲取壓縮檔案流
     *
     * @param sourceFile
     * @return ByteArrayOutputStream
     * @throws IOException
     */
    private static OutputStream compressFile(File sourceFile, OutputStream outputStream) throws IOException {
        try (CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new CRC32());
             ZipOutputStream zipOutputStream = new ZipOutputStream(checkedOutputStream)) {
            doCompressFile(sourceFile, zipOutputStream, StringUtils.EMPTY);
            return outputStream;
        }
    }

    /**
     * 處理目錄下的檔案
     *
     * @param sourceFile
     * @param zipOutputStream
     * @param zipFilePath
     * @throws IOException
     */
    private static void doCompressFile(File sourceFile, ZipOutputStream zipOutputStream, String zipFilePath) throws IOException {
        // 如果檔案是隱藏的,不進行壓縮
        if (sourceFile.isHidden()) {
            return;
        }
        if (sourceFile.isDirectory()) {//如果是檔案夾
            handDirectory(sourceFile, zipOutputStream, zipFilePath);
        } else {//如果是檔案就添加到壓縮包中
            try (FileInputStream fileInputStream = new FileInputStream(sourceFile)) {
                //String fileName = zipFilePath + File.separator + sourceFile.getName();
                String fileName = zipFilePath + sourceFile.getName();
                addCompressFile(fileInputStream, fileName, zipOutputStream);
                //String fileName = zipFilePath.replace("\\", "/") + "/" + sourceFile.getName();
                //addCompressFile(fileInputStream, fileName, zipOutputStream);
            }
        }
    }

    /**
     * 處理檔案夾
     *
     * @param dir         檔案夾
     * @param zipOut      壓縮包輸出流
     * @param zipFilePath 壓縮包中的檔案夾路徑
     * @throws IOException
     */
    private static void handDirectory(File dir, ZipOutputStream zipOut, String zipFilePath) throws IOException {
        File[] files = dir.listFiles();
        if (ArrayUtils.isEmpty(files)) {
            ZipEntry zipEntry = new ZipEntry(zipFilePath + dir.getName() + File.separator);
            zipOut.putNextEntry(zipEntry);
            zipOut.closeEntry();
            return;
        }
        for (File file : files) {
            doCompressFile(file, zipOut, zipFilePath + dir.getName() + File.separator);
        }
    }

    /**
     * 獲取壓縮檔案流
     *
     * @param documentList 需要壓縮的檔案集合
     * @return ByteArrayOutputStream
     */
    public static OutputStream compressFile(List<FileInfo> documentList, OutputStream outputStream) {
        Map<String, List<FileInfo>> documentMap = new HashMap<>();
        documentMap.put("", documentList);
        return compressFile(documentMap, outputStream);
    }

    /**
     * 將檔案打包到zip
     *
     * @param documentMap 需要下載的附件集合 map的key對應zip里的檔案夾名
     * @return ByteArrayOutputStream
     */
    public static OutputStream compressFile(Map<String, List<FileInfo>> documentMap, OutputStream outputStream) {
        CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new CRC32());
        ZipOutputStream zipOutputStream = new ZipOutputStream(checkedOutputStream);
        try {
            for (Map.Entry<String, List<FileInfo>> documentListEntry : documentMap.entrySet()) {
                String dirName = documentMap.size() > 1 ? documentListEntry.getKey() : "";
                Map<String, Integer> fileNameToLen = new HashMap<>();//記錄單個合同號檔案夾下每個檔案名稱出現的次數(對重復檔案名重命名)
                for (FileInfo document : documentListEntry.getValue()) {
                    try {
                        //防止單個檔案夾下檔案名重復 對重復的檔案進行重命名
                        String documentName = document.getFileName();
                        if (fileNameToLen.get(documentName) == null) {
                            fileNameToLen.put(documentName, 1);
                        } else {
                            int fileLen = fileNameToLen.get(documentName) + 1;
                            fileNameToLen.put(documentName, fileLen);
                            documentName = documentName + "(" + fileLen + ")";
                        }
                        String fileName = documentName + "." + document.getSuffix();
                        if (StringUtils.isNotBlank(dirName)) {
                            fileName = dirName + File.separator + fileName;
                        }
                        addCompressFile(document.getFileInputStream(), fileName, zipOutputStream);
                    } catch (Exception e) {
                        logger.info("filesToZip exception :", e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("filesToZip exception:" + e.getMessage(), e);
        }
        return outputStream;
    }

    /**
     * 將單個檔案寫入檔案壓縮包
     *
     * @param inputStream     檔案輸入流
     * @param fileName        檔案在壓縮包中的相對全路徑
     * @param zipOutputStream 壓縮包輸出流
     */
    private static void addCompressFile(InputStream inputStream, String fileName, ZipOutputStream zipOutputStream) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOutputStream.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = bufferedInputStream.read(bytes)) >= 0) {
                zipOutputStream.write(bytes, 0, length);
                zipOutputStream.flush();
            }
            zipOutputStream.closeEntry();
            //System.out.println("map size, value is " + RamUsageEstimator.sizeOf(zipOutputStream));
        } catch (Exception e) {
            logger.info("addFileToZip exception:", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 通過網路請求下載zip
     *
     * @param sourceFilePath       需要壓縮的檔案路徑
     * @param response            HttpServletResponse
     * @param zipName            壓縮包名稱
     * @throws IOException
     */
    public static void httpDownloadCompressFile(String sourceFilePath, HttpServletResponse response, String zipName) throws IOException {
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            throw new RuntimeException(sourceFilePath + "不存在!");
        }
        if(StringUtils.isBlank(zipName)){
            zipName = sourceFile.getName();
        }
        try (ServletOutputStream servletOutputStream = response.getOutputStream()){
            CompressUtil.compressFile(sourceFile, servletOutputStream);
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + zipName + ".zip\"");
            servletOutputStream.flush();
        }
    }

    public static void httpDownloadCompressFileOld(String sourceFilePath, HttpServletResponse response, String zipName) throws IOException {
        try (ServletOutputStream servletOutputStream = response.getOutputStream()){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] zipBytes = byteArrayOutputStream.toByteArray();
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + zipName + ".zip\"");
            response.setContentLength(zipBytes.length);
            servletOutputStream.write(zipBytes);
            servletOutputStream.flush();
        }
    }

    /**
     * 通過網路請求下載zip
     *
     * @param sourceFilePath       需要壓縮的檔案路徑
     * @param response            HttpServletResponse
     * @throws IOException
     */
    public static void httpDownloadCompressFile(String sourceFilePath, HttpServletResponse response) throws IOException {
        httpDownloadCompressFile(sourceFilePath, response, null);
    }


    /**
     * 檢查檔案名是否已經存在,如果存在,就在檔案名后面加上“(1)”,如果檔案名“(1)”也存在,則改為“(2)”,以此類推,如果檔案名不存在,就直接創建一個新檔案,
     *
     * @param filename 檔案名
     * @return File
     */
    public static File createNewFile(String filename) {
        File file = new File(filename);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            String base = filename.substring(0, filename.lastIndexOf("."));
            String ext = filename.substring(filename.lastIndexOf("."));
            int i = 1;
            while (true) {
                String newFilename = base + "(" + i + ")" + ext;
                file = new File(newFilename);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                i++;
            }
        }
        return file;
    }
}

FileInfo類代碼:

/**
 * @author fhey
 * @date 2023-05-11 21:01:26
 * @description: TODO
 */
@Data
public class FileInfo {
    private InputStream fileInputStream;

    private String suffix;

    private String fileName;
    
    private boolean isDirectory;
}

測驗壓縮并在本地生成檔案:

public static void main(String[] args) throws Exception {
        //在本地創建壓縮檔案
        CompressUtil.createLocalCompressFile("D:\\書籍\\電子書\\醫書", "D:\\test");
    }

壓縮并在本地生成檔案驗證結果:
壓縮并在本地生成檔案

壓縮檔案并通過http請求下載:

/**
 * @author fhey
 */
@RestController
public class TestController {

    @GetMapping(value = "https://www.cnblogs.com/testFileToZip")
    public void testFileToZip(HttpServletResponse response) throws IOException {
        String zipFileName = "myFiles";
        String sourceFilePath = "D:\\picture";
        CompressUtil.httpDownloadCompressFile(sourceFilePath,response, zipFileName);
    }
}

壓縮檔案并通過http請求下載驗證結果:
壓縮檔案并通過http請求下載

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/553881.html

標籤:JavaScript

上一篇:JS中的事件監聽

下一篇:返回列表

標籤雲
其他(160041) Python(38189) JavaScript(25466) Java(18161) C(15234) 區塊鏈(8268) C#(7972) AI(7469) 爪哇(7425) MySQL(7217) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5873) 数组(5741) R(5409) Linux(5344) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4579) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2434) ASP.NET(2403) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) .NET技术(1977) 功能(1967) Web開發(1951) HtmlCss(1950) C++(1927) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1878) .NETCore(1862) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • IEEE1588PTP在數字化變電站時鐘同步方面的應用

    IEEE1588ptp在數字化變電站時鐘同步方面的應用 京準電子科技官微——ahjzsz 一、電力系統時間同步基本概況 隨著對IEC 61850標準研究的不斷深入,國內外學者提出基于IEC61850通信標準體系建設數字化變電站的發展思路。數字化變電站與常規變電站的顯著區別在于程序層傳統的電流/電壓互 ......

    uj5u.com 2020-09-10 03:51:52 more
  • HTTP request smuggling CL.TE

    CL.TE 簡介 前端通過Content-Length處理請求,通過反向代理或者負載均衡將請求轉發到后端,后端Transfer-Encoding優先級較高,以TE處理請求造成安全問題。 檢測 發送如下資料包 POST / HTTP/1.1 Host: ac391f7e1e9af821806e890 ......

    uj5u.com 2020-09-10 03:52:11 more
  • 網路滲透資料大全單——漏洞庫篇

    網路滲透資料大全單——漏洞庫篇漏洞庫 NVD ——美國國家漏洞庫 →http://nvd.nist.gov/。 CERT ——美國國家應急回應中心 →https://www.us-cert.gov/ OSVDB ——開源漏洞庫 →http://osvdb.org Bugtraq ——賽門鐵克 →ht ......

    uj5u.com 2020-09-10 03:52:15 more
  • 京準講述NTP時鐘服務器應用及原理

    京準講述NTP時鐘服務器應用及原理京準講述NTP時鐘服務器應用及原理 安徽京準電子科技官微——ahjzsz 北斗授時原理 授時是指接識訓通過某種方式獲得本地時間與北斗標準時間的鐘差,然后調整本地時鐘使時差控制在一定的精度范圍內。 衛星導航系統通常由三部分組成:導航授時衛星、地面檢測校正維護系統和用戶 ......

    uj5u.com 2020-09-10 03:52:25 more
  • 利用北斗衛星系統設計NTP網路時間服務器

    利用北斗衛星系統設計NTP網路時間服務器 利用北斗衛星系統設計NTP網路時間服務器 安徽京準電子科技官微——ahjzsz 概述 NTP網路時間服務器是一款支持NTP和SNTP網路時間同步協議,高精度、大容量、高品質的高科技時鐘產品。 NTP網路時間服務器設備采用冗余架構設計,高精度時鐘直接來源于北斗 ......

    uj5u.com 2020-09-10 03:52:35 more
  • 詳細解讀電力系統各種對時方式

    詳細解讀電力系統各種對時方式 詳細解讀電力系統各種對時方式 安徽京準電子科技官微——ahjzsz,更多資料請添加VX 衛星同步時鐘是我京準公司開發研制的應用衛星授時時技術的標準時間顯示和發送的裝置,該裝置以M國全球定位系統(GLOBAL POSITIONING SYSTEM,縮寫為GPS)或者我國北 ......

    uj5u.com 2020-09-10 03:52:45 more
  • 如何保證外包團隊接入企業內網安全

    不管企業規模的大小,只要企業想省錢,那么企業的某些服務就一定會采用外包的形式,然而看似美好又經濟的策略,其實也有不好的一面。下面我通過安全的角度來聊聊使用外包團的安全隱患問題。 先看看什么服務會使用外包的,最常見的就是話務/客服這種需要大量重復性、無技術性的服務,或者是一些銷售外包、特殊的職能外包等 ......

    uj5u.com 2020-09-10 03:52:57 more
  • PHP漏洞之【整型數字型SQL注入】

    0x01 什么是SQL注入 SQL是一種注入攻擊,通過前端帶入后端資料庫進行惡意的SQL陳述句查詢。 0x02 SQL整型注入原理 SQL注入一般發生在動態網站URL地址里,當然也會發生在其它地發,如登錄框等等也會存在注入,只要是和資料庫打交道的地方都有可能存在。 如這里http://192.168. ......

    uj5u.com 2020-09-10 03:55:40 more
  • [GXYCTF2019]禁止套娃

    git泄露獲取原始碼 使用GET傳參,引數為exp 經過三層過濾執行 第一層過濾偽協議,第二層過濾帶引數的函式,第三層過濾一些函式 preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'] (?R)參考當前正則運算式,相當于匹配函式里的引數 因此傳遞 ......

    uj5u.com 2020-09-10 03:56:07 more
  • 等保2.0實施流程

    流程 結論 ......

    uj5u.com 2020-09-10 03:56:16 more
最新发布
  • Java實作打包壓縮檔案或檔案夾生成zip以實作多檔案批量下載

    有時候在系統中需要一次性下載多個檔案,但逐個下載檔案比較麻煩。這時候,最好的解決辦法是將所有檔案打包成一個壓縮檔案,然后下載這個壓縮檔案,這樣就可以一次性獲取所有所需的檔案了。 下面是一個名為CompressUtil的工具類的代碼,它提供了一些方法來處理檔案壓縮和下載操作: ```java impo ......

    uj5u.com 2023-05-31 08:41:58 more
  • JS中的事件監聽

    JavaScript的事件監聽是一種機制,用于在HTML檔案中的元素上注冊事件處理程式,以便在特定事件發生時執行相應的JavaScript代碼。 事件監聽的基本思想是將事件處理程式(也稱為事件回呼函式)系結到特定的事件上。所謂特定的事件是當用戶與頁面進行互動時,比如點擊按鈕、鍵盤輸入、滑鼠移動等,瀏 ......

    uj5u.com 2023-05-31 08:41:53 more
  • ENVI指定像元數量(行數與列數)裁剪柵格影像

    本文介紹基于**ENVI**軟體,實作柵格遙感影像按照**像元行列號與個數**進行**指定矩形區域裁剪**的方法。 一般的,如果我們需要裁剪某個具體的行政區域,按照對應區域的矢量圖層裁剪即可;如果需要裁剪某個大致的區域范圍,可以按照文章[ArcMap手動新建矢量要素的方式](https://www. ......

    uj5u.com 2023-05-31 08:40:49 more
  • ENVI指定像元數量(行數與列數)裁剪柵格影像

    本文介紹基于**ENVI**軟體,實作柵格遙感影像按照**像元行列號與個數**進行**指定矩形區域裁剪**的方法。 一般的,如果我們需要裁剪某個具體的行政區域,按照對應區域的矢量圖層裁剪即可;如果需要裁剪某個大致的區域范圍,可以按照文章[ArcMap手動新建矢量要素的方式](https://www. ......

    uj5u.com 2023-05-31 08:39:46 more
  • 【一步步開發AI運動小程式】九、姿態輔助除錯桌面工具折使用

    > 隨著人工智能技術的不斷發展,阿里體育等IT大廠,推出的“樂動力”、“天天跳繩”AI運動APP,讓**云上運動會、線上運動會、健身打卡、AI體育指導**等概念空前火熱。那么,能否將這些在APP成功應用的場景搬上小程式,分享這些概念的紅利呢?本系列文章就帶您一步一步從零開始開發一個AI運動小程式,本 ......

    uj5u.com 2023-05-30 07:56:48 more
  • JavaScript全決議——Express框架介紹與入門

    本文為千鋒資深前端教學老師帶來的【JavaScript全決議】系列,文章內含豐富的代碼案例及配圖,從0到1講解JavaScript相關知識點,致力于教會每一個人學會JS!文末有本文重點總結,可以收藏慢慢看\~ 更多技術類內容,主頁關注一波! ......

    uj5u.com 2023-05-30 07:56:43 more
  • html+css實作二級導航欄效果,簡單易看懂噢!

    這應該是這幾天以來看到的最簡單易懂的有二級選單欄的導航欄效果實作了 使用html+css實作,看了好幾天導航欄的實作方式,要么是太復雜的需要JS或者框架的,要么是太簡單僅僅使用div和超鏈接的, 再要么就是只有簡單的一級導航,沒有二級選單欄的,心情復雜 就想找一個有二級選單欄,使用html+css實 ......

    uj5u.com 2023-05-30 07:56:37 more
  • 【一步步開發AI運動小程式】九、姿態輔助除錯桌面工具折使用

    > 隨著人工智能技術的不斷發展,阿里體育等IT大廠,推出的“樂動力”、“天天跳繩”AI運動APP,讓**云上運動會、線上運動會、健身打卡、AI體育指導**等概念空前火熱。那么,能否將這些在APP成功應用的場景搬上小程式,分享這些概念的紅利呢?本系列文章就帶您一步一步從零開始開發一個AI運動小程式,本 ......

    uj5u.com 2023-05-30 07:56:19 more
  • JavaScript全決議——Express框架介紹與入門

    本文為千鋒資深前端教學老師帶來的【JavaScript全決議】系列,文章內含豐富的代碼案例及配圖,從0到1講解JavaScript相關知識點,致力于教會每一個人學會JS!文末有本文重點總結,可以收藏慢慢看\~ 更多技術類內容,主頁關注一波! ......

    uj5u.com 2023-05-30 07:56:14 more
  • html+css實作二級導航欄效果,簡單易看懂噢!

    這應該是這幾天以來看到的最簡單易懂的有二級選單欄的導航欄效果實作了 使用html+css實作,看了好幾天導航欄的實作方式,要么是太復雜的需要JS或者框架的,要么是太簡單僅僅使用div和超鏈接的, 再要么就是只有簡單的一級導航,沒有二級選單欄的,心情復雜 就想找一個有二級選單欄,使用html+css實 ......

    uj5u.com 2023-05-30 07:55:48 more