Total Pageviews

2018/12/07

[Java] How to zip and extract file by zip4j

Add dependency into your pom.xml
    <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>1.3.2</version>
    </dependency>


Here has sample code:
package com.demo.util;

import java.io.File;
import java.util.ArrayList;

import com.google.common.base.Strings;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

public class ZipUtils {

    /**
     * 將檔案壓縮成 zip file.
     * 
     * @param files
     *            來源檔案
     * @param dest
     *            路徑 + zip file name
     * @param password
     *            密碼 (optional)
     * @throws ZipException
     *             若壓縮過程發生錯誤,會拋出 ZipException
     */
    public static void addFilesIntoZip(ArrayList<File> files, String dest, String password) throws ZipException {
        try {
            deleteExistingFile(dest);
            ZipFile zipFile = new ZipFile(dest);

            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            if (!Strings.isNullOrEmpty(password)) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
                parameters.setPassword(password);
            }

            zipFile.addFiles(files, parameters);
        } catch (ZipException e) {
            throw new ZipException("壓縮檔案發生錯誤,錯誤原因: " + e.getMessage(), e);
        }
    }
    
    /**
     * 將某個目錄下的檔案,壓縮成 zip file.
     * 
     * @param sourceDir
     *            來源目錄
     * @param dest
     *            路徑 + zip file name
     * @param password
     *            密碼 (optional)
     * @throws ZipException
     *             若壓縮過程發生錯誤,會拋出 ZipException
     */
    public static void addDirectoryIntoZip(String sourceDir, String dest, String password) throws ZipException {
        try {
            deleteExistingFile(dest);
            ZipFile zipFile = new ZipFile(dest);

            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            if (!Strings.isNullOrEmpty(password)) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
                parameters.setPassword(password);
            }

            zipFile.addFolder(sourceDir, parameters);
        } catch (ZipException e) {
            throw new ZipException("壓縮檔案發生錯誤,錯誤原因: " + e.getMessage(), e);
        }
    }

    /**
     * 刪除現有檔案.
     * 
     * @param file
     */
    private static void deleteExistingFile(String file) {
        File existingFile = new File(file);
        if (existingFile.exists()) {
            existingFile.delete();
        }
    }

    /**
     * 解壓縮檔案到指定目錄.
     * 
     * @param zip
     *            路徑 + zip file name
     * @param dest
     *            解壓縮到目的地的位置
     * @param password
     *            解壓縮檔案
     * @throws ZipException
     *             若解壓縮過程發生錯誤,會拋出 ZipException
     */
    public static void extractZipFile(String zip, String dest, String password) throws ZipException {
        try {
            ZipFile zipFile = new ZipFile(zip);
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(dest);
        } catch (ZipException e) {
            throw new ZipException("解壓縮檔案發生錯誤,錯誤原因: " + e.getMessage(), e);
        }
    }

}


No comments: