123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package com.qmth.teachcloud.common.util;
- import cn.hutool.core.collection.CollectionUtil;
- import com.qmth.teachcloud.common.contant.SystemConstant;
- import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
- import net.lingala.zip4j.ZipFile;
- import net.lingala.zip4j.exception.ZipException;
- import net.lingala.zip4j.model.ZipParameters;
- import net.lingala.zip4j.model.enums.AesKeyStrength;
- import net.lingala.zip4j.model.enums.CompressionLevel;
- import net.lingala.zip4j.model.enums.CompressionMethod;
- import net.lingala.zip4j.model.enums.EncryptionMethod;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.util.CollectionUtils;
- import java.io.File;
- import java.util.List;
- import java.util.Objects;
- import java.util.Optional;
- /**
- * @Description: zip4j工具
- * @Param:
- * @return:
- * @Author: wangliang
- * @Date: 2022/8/12
- */
- public class Zip4jUtil {
- private final static Logger log = LoggerFactory.getLogger(Zip4jUtil.class);
- /**
- * 压缩zip
- *
- * @param scrPath
- * @param files
- */
- public static void zipFile(String scrPath, List<File> files) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
- throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
- }
- if (CollectionUtils.isEmpty(files)) {
- throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
- }
- commonZipFile(scrPath, null, files, null);
- }
- /**
- * 压缩zip
- *
- * @param scrPath
- * @param destPath
- */
- public static void zipFile(String scrPath, String destPath) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
- throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
- }
- Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
- commonZipFile(scrPath, destPath, null, null);
- }
- /**
- * 压缩zip带密码
- *
- * @param scrPath
- * @param files
- * @param password
- * @throws ZipException
- */
- public static void zipEncryptFile(String scrPath, List<File> files, String password) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
- throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
- }
- if (CollectionUtils.isEmpty(files)) {
- throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
- }
- Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
- commonZipFile(scrPath, null, files, password);
- }
- /**
- * 压缩zip带密码
- *
- * @param scrPath
- * @param destPath
- * @param password
- */
- public static void zipEncryptFile(String scrPath, String destPath, String password) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
- throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
- }
- Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
- // Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
- commonZipFile(scrPath, destPath, null, password);
- }
- /**
- * 解压zip
- *
- * @param scrPath
- * @param destPath
- */
- public static void unzip(String scrPath, String destPath) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
- commonUnZipFile(scrPath, destPath, null);
- }
- /**
- * 解压zip带密码
- *
- * @param scrPath
- * @param destPath
- * @param password
- */
- public static void unzipEncryptFile(String scrPath, String destPath, String password) {
- Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
- Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
- Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压密码不能为空"));
- commonUnZipFile(scrPath, destPath, password);
- }
- /**
- * 解压zip包
- *
- * @param scrPath
- * @param destPath
- * @param password
- */
- private static void commonUnZipFile(String scrPath, String destPath, String password) {
- try {
- File file = new File(scrPath);
- if (!file.exists()) {
- file.getParentFile().mkdirs();
- }
- ZipFile zipFile = new ZipFile(scrPath);
- if (Objects.nonNull(password) && zipFile.isEncrypted()) {
- zipFile.setPassword(password.toCharArray());
- }
- File currentFile = new File(destPath);
- if (!currentFile.exists()) {
- currentFile.getParentFile().mkdirs();
- // throw ExceptionResultEnum.ERROR.exception("待解压的路径不存在");
- }
- zipFile.extractAll(destPath);
- } catch (ZipException e) {
- log.error(SystemConstant.LOG_ERROR, e);
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
- }
- }
- /**
- * 压缩zip包
- *
- * @param scrPath
- * @param destPath
- * @param files
- * @param password
- */
- private static void commonZipFile(String scrPath, String destPath, List<File> files, String password) {
- try {
- File file = new File(scrPath);
- if (!file.exists()) {
- file.getParentFile().mkdirs();
- }
- ZipParameters parameters = new ZipParameters();
- // 压缩方法
- parameters.setCompressionMethod(CompressionMethod.DEFLATE);
- // 压缩级别
- parameters.setCompressionLevel(CompressionLevel.NORMAL);
- // 生成的压缩文件
- ZipFile zipFile;
- if (Objects.nonNull(password)) {
- parameters.setEncryptFiles(true);
- parameters.setEncryptionMethod(EncryptionMethod.AES);
- parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
- zipFile = new ZipFile(scrPath, password.toCharArray());
- } else {
- zipFile = new ZipFile(scrPath);
- }
- // 3. 添加文件夹到zip
- File folderToAdd = new File(destPath);
- if (folderToAdd.isDirectory()) {
- zipFile.addFolder(folderToAdd, parameters);
- } else {
- throw new IllegalArgumentException("提供的路径不是文件夹: " + destPath);
- }
- // File[] fs = null;
- // if (!CollectionUtil.isEmpty(files)) {
- // // 要打包的文件夹
- // fs = files.toArray(new File[files.size()]);
- // } else if (Objects.nonNull(destPath)) {
- // File currentFile = new File(destPath);
- // if (!currentFile.exists()) {
- // throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
- // }
- // fs = currentFile.listFiles();
- // }
- //
- // // 遍历test文件夹下所有的文件、文件夹
- // for (File f : fs) {
- // if (f.isDirectory()) {
- // zipFile.addFolder(f.getPath(), parameters);
- // } else {
- // zipFile.addFile(f, parameters);
- // }
- // }
- } catch (Exception e) {
- log.error(SystemConstant.LOG_ERROR, e);
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
- }
- }
- }
|