package {{basePackage}}.utils.compress;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.compress.CompressUtil;
import cn.hutool.extra.compress.extractor.Extractor;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;

import java.io.File;

/**
 * dcrenl: 2023-09-07 13:27:41
 * 压缩文件操作
 */
public class ZipUtils {

    /**
     * dcrenl: 2023-09-07 13:40:31
     * 压缩目录
     * @param sourcePath
     * @param compressFilePath
     */
    public static void create(String sourcePath, String compressFilePath) {
        File compressfile = FileUtil.file(compressFilePath);
        File sourceFile = FileUtil.file(sourcePath);
        CompressUtil.createArchiver(CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.SEVEN_Z, compressfile)
                .add(sourceFile)
                .finish()
                .close();
    }

    /**
     * 解压文件
     * @param compressFilePath
     * @param releasePath
     */
    public static void release(String compressFilePath, String releasePath) {
        File compressFile = FileUtil.file(compressFilePath);
        File releaseFile = FileUtil.file(releasePath);

        Extractor extractor = CompressUtil.createExtractor(CharsetUtil.defaultCharset(), compressFile);
        extractor.extract(releaseFile);
    }

}
