package com.daimafans.demo;

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

import java.io.File;

/**
 * 压缩文件夹
 *
 * @author daimafans.com
 * @date 2018-11-29
 */
public class ZipFiles
{
    public static void main(String[] args) throws ZipException
    {
        // 生成的压缩文件
        ZipFile zipFile = new ZipFile("D:\\test.zip");

        ZipParameters parameters = new ZipParameters();
        // 压缩方式
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // 压缩级别
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        // 要打包的文件夹
        File currentFile = new File("D:\\test");
        File[] files = currentFile.listFiles();

        // 遍历test文件夹下所有的文件、文件夹
        assert files != null;
        for (File file : files)
        {
            if (file.isDirectory())
            {
                zipFile.addFolder(file.getPath(), parameters);
            }
            else
            {
                zipFile.addFile(file, parameters);
            }
        }
    }
}