springboot 中 java.util.zip 文件的壓縮


package com.geotmt.billingcenter.common.utils;
 
import org.datanucleus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * @Description 壓縮與解壓工具
 * @Author      yanghanwei
 * @Date        18:42 2019-11-20
 * @Version     v1
 **/
public class ZipUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
 
     /**
     * 壓縮 zip 
     * @param filePath  文件夾 全路徑
     * @param fileName  文件夾名稱
     * @param outPath   壓縮文件保存路徑
     */
    public static void zipFile(String filePath, String fileName, String outPath) {
        logger.info("filePath:{}, fileName:{}, outPath:{}", filePath, fileName, outPath);
        try {
            //創建Test.zip文件
            OutputStream is = new FileOutputStream(outPath);
            //檢查輸出流,采用CRC32算法,保證文件的一致性
            CheckedOutputStream cos = new CheckedOutputStream(is, new CRC32());
            //創建zip文件的輸出流
            ZipOutputStream zos = new ZipOutputStream(cos);
            //需要壓縮的文件或文件夾對象
            File file = new File(filePath);
            //壓縮文件的具體實現函數
            zipFilePost(zos,file,filePath,fileName,outPath);
            zos.close();
            cos.close();
            is.close();
            System.out.println("壓縮完成");
        } catch (Exception e) {
            logger.error("壓縮失敗zipFile,Exception:" + e);
        }
    }
 
    /**
     * 壓縮文件
     * @param zos       zip文件的輸出流
     * @param file      需要壓縮的文件或文件夾對象
     * @param filePath  壓縮的文件路徑
     * @param fileName  需要壓縮的文件夾名
     * @param outPath   縮完成后保存為Test.zip文件
     */
    private static void zipFilePost(ZipOutputStream zos, File file, String filePath, String fileName, String outPath){
 
        try{
            String path = file.getPath();
            String zosName = "";
            if(!StringUtils.isEmpty(path)){
                zosName = path.substring(path.indexOf(fileName));
            }
            File[] files = file.listFiles();
            if(file.isDirectory() && files != null && files.length > 0) {
                // 創建壓縮文件的目錄結構
                zos.putNextEntry(new ZipEntry(zosName + File.separator));
                for(File f : files) {
                    zipFilePost(zos, f, filePath, fileName, outPath);
                }
            } else {
                logger.info("正在壓縮文件:{}",file.getName());
                // 創建壓縮文件
                zos.putNextEntry(new ZipEntry(zosName));
                // 用字節方式讀取源文件
                InputStream is = new FileInputStream(file.getPath());
                // 創建一個緩存區
                BufferedInputStream bis = new BufferedInputStream(is);
                // 字節數組,每次讀取1024個字節
                byte [] b = new byte[1024];
                // 循環讀取,邊讀邊寫
                while(bis.read(b)!=-1) {
                    // 寫入壓縮文件
                    zos.write(b);
                }
                //關閉流
                bis.close();
                is.close();
            }
        } catch (Exception e) {
            logger.error("壓縮文件失敗zipFilePost,Exception:" + e);
        }
    }
 
 
    public static void main(String[] args) throws Exception{
        String filePath = "/var/folders/88/jh37h0fj59l1f302jdryz4780000gn/T/201908月小微平台消耗-1574300435525/";
        // 需要壓縮的文件夾名
        String fileName = "201908月小微平台消耗-1574300435525";
        // 壓縮完成后保存為Test.zip文件,名字隨意
        String outPath = "/var/folders/88/jh37h0fj59l1f302jdryz4780000gn/T/Test3.zip";
        zipFile(filePath, fileName, outPath);
    }
}

 

轉自:https://blog.csdn.net/qq_29323645/article/details/103202158


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM