天天看點

Java壓縮檔案和檔案夾為zip格式

工具類ZipUtils 如下:

package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    /**
     *
     * 檔案夾壓縮成ZIP
     *
     * @param srcDir           壓縮檔案夾路徑
     * @param out              壓縮檔案輸出流
     * @param keepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
     *
     *                         false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)
     *
     * @throws RuntimeException 壓縮失敗會抛出運作時異常
     *
     */
    public static void toZip(String srcDir, OutputStream out, boolean keepDirStructure)
            throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile, zos, sourceFile.getName(), keepDirStructure);
            long end = System.currentTimeMillis();
            System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 多檔案壓縮成ZIP
     *
     * @param imageMap 需要壓縮的檔案清單,鍵值對為 <檔案名,檔案的位元組數組>,檔案名必須包含字尾
     * @param out      壓縮檔案輸出流
     * @throws RuntimeException 壓縮失敗會抛出運作時異常
     */
    public static void toZip(Map<String, byte[]> imageMap, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        try (ZipOutputStream zos = new ZipOutputStream(out)) {
            for (Map.Entry<String, byte[]> map : imageMap.entrySet()) {
                zos.putNextEntry(new ZipEntry(map.getKey()));
                zos.write(map.getValue());
                zos.closeEntry();
            }
            long end = System.currentTimeMillis();
            System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        }
    }


    /**
     * 多檔案壓縮成ZIP
     * @param srcFiles 需要壓縮的檔案清單
     * @param out           壓縮檔案輸出流
     * @throws RuntimeException 壓縮失敗會抛出運作時異常
     */
    public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
        long start = System.currentTimeMillis();
        try(ZipOutputStream zos= new ZipOutputStream(out)) {
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[2048];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1){
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("壓縮完成,耗時:" + (end - start) +" ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils",e);
        }
    }



    /**
     *
     * 遞歸壓縮方法
     * @param sourceFile       源檔案
     * @param zos              zip輸出流
     * @param name             壓縮後的名稱
     * @param keepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
     *
     *                         false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)
     * @throws Exception
     */
    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean keepDirStructure) throws Exception {
        byte[] buf = new byte[2048];
        if (sourceFile.isFile()) {
            // 向zip輸出流中添加一個zip實體,構造器中name為zip實體的檔案的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy檔案到zip輸出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原來的檔案結構時,需要對空檔案夾進行處理
                if (keepDirStructure) {
                    // 空檔案夾的處理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 沒有檔案,不需要檔案的copy
                    zos.closeEntry();
                }
            } else {
                for (File file : listFiles) {
                    // 判斷是否需要保留原來的檔案結構
                    if (keepDirStructure) {
                        // 注意:file.getName()前面需要帶上父檔案夾的名字加一斜杠,
                        // 不然最後壓縮包中就不能保留原來的檔案結構,即:所有檔案都跑到壓縮包根目錄下了
                        compress(file, zos, name + "/" + file.getName(), keepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), keepDirStructure);
                    }
                }
            }
        }
    }

}

           

測試:

1.壓縮檔案夾:

import utils.ZipUtils;
import java.io.*;

public class Test {
    public static void main(String[] args) {
   		 try {
           		 //定義輸出流和最終生成的檔案名,如;儲存到 F盤的 MyPic.zip
            	 FileOutputStream   fileOutputStream = new FileOutputStream(new File("F:/Myfolder.zip"));
           		 BufferedOutputStream  bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            	//開始壓縮 F盤下的test檔案夾
            	ZipUtils.toZip("F:/test", bufferedOutputStream,true);
       		 } catch (FileNotFoundException e) {
           			 e.printStackTrace();
       		 }
     }
}
           

2 .壓縮多檔案:

import java.io.*;
import java.util.ArrayList;
import utils.ZipUtils;
import java.util.List;

public class Test {
    public static void main(String[] args) {
   		try {
   			//要壓縮的檔案清單
            List<File> fileList = new ArrayList<>();
            fileList.add(new File("F:/test/pic1.jpg"));
            fileList.add(new File("F:/test/doc_20210203100237.pdf"));
            //壓縮後的檔案名和儲存路徑
            FileOutputStream fos2 = new FileOutputStream(new File("f:/test/Myzip.zip"));
            //開始壓縮
            ZipUtils.toZip(fileList, fos2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
     }
}
           

3.有時候我們可能隻有檔案流或者檔案的位元組數組,可通過字檔案節數組壓縮:

需要導入Apache Commons IO包利用 IOUtils.toByteArray 将 InputStream 轉換為 byte[]

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
</dependency>
           
import org.apache.commons.io.IOUtils;
import utils.ZipUtils;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        try {
            //讀取圖檔轉換為byte[]
            FileInputStream inputStream1 = new FileInputStream("F:/test/pic1.jpg");
            byte[] bytes1 = IOUtils.toByteArray(inputStream1);
            //讀取pdf轉換為byte[]
            FileInputStream inputStream2 = new FileInputStream("F:/test/doc_20210203100237.pdf");
            byte[] bytes2 = IOUtils.toByteArray(inputStream2);
            //讀取txt轉換為byte[]
            FileInputStream inputStream3 = new FileInputStream("F:/test/za.txt");
            byte[] bytes3 = IOUtils.toByteArray(inputStream3);

            //三個檔案存入picMap
            Map<String, byte[]> picMap = new HashMap<>();
            picMap.put("pic1.jpg", bytes1);
            picMap.put("doc_20210203100237.pdf", bytes2);
            picMap.put("za.txt", bytes3);

            //定義輸出流和最終生成的檔案名,如;MyPic.zip
            FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/test/MyPic.zip"));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            //開始壓縮
            ZipUtils.toZip(picMap, bufferedOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
           

參考:

Convert InputStream to byte array in Java

Java實作将檔案或者檔案夾壓縮成zip