前言
Github:https://github.com/HealerJean
1、依賴
<!--zip壓縮-->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-apache-xalan2</artifactId>
<version>1.10.1</version>
</dependency>
2、工具類
package com.fintech.scf.utils.zip;
import lombok.extern.slf4j.Slf4j;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import java.io.File;
/**
* @author HealerJean
* @ClassName ZipUtils
* @Date 2019/11/14 20:48.
* @Description 壓縮工具類
*/
@Slf4j
public class ZipUtils {
/**
* 壓縮目錄
*/
public static void compress(String directoryPath) {
compress(directoryPath, directoryPath+".zip");
}
/**
* 壓縮目錄
* @param directoryPath 源目錄
* @param zipFilePath 目標壓縮文件
*/
public static void compress(String directoryPath, String zipFilePath) {
File directory = new File(directoryPath);
if (!directory.exists()) {
log.info("需要被壓縮的路徑:{}不存在", directoryPath);
throw new RuntimeException(directoryPath + "不存在!");
}
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
File zipFile = new File(zipFilePath);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(directory);
//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夾 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); //排除哪些文件或文件夾
zip.addFileset(fileSet);
zip.execute();
}
}
感興趣的,歡迎添加博主微信
哈,博主很樂意和各路好友交流,如果滿意,請打賞博主任意金額,感興趣的在微信轉賬的時候,備註您的微信或者其他聯繫方式。添加博主微信哦。
請下方留言吧。可與博主自由討論哦
微信 | 微信公衆號 | 支付寶 |
---|---|---|