import java.io.File;
import java.util.UUID;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
public class ZipUtils {
/**
* 压缩文件
*
* @param sourceDirectory 源目录
* @param destFile 目标文件
*/
public static void zip(String sourceDirectory, String destFile) {
try {
File sourceFile = new File(sourceDirectory);
if (!sourceFile.exists()) {
System.out.println("[" + sourceDirectory + "]不存在!!!!!!");
return;
}
FileSet fileSet = new FileSet();
fileSet.setDir(sourceFile);
Project project = new Project();
Zip zip = new Zip();
zip.setProject(project);
zip.addFileset(fileSet);
zip.setDestFile(new File(destFile));
zip.execute();
System.out.println("文件压缩成功!!!!!!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 解压文件
*
* @param sourceFile 源文件
* @param destDirectory 目标目录
*/
public static void unZip(String sourceFile, String destDirectory) {
try {
File src = new File(sourceFile);
if (!src.exists()) {
System.out.println("[" + sourceFile + "]不存在!!!!!!");
return;
}
Project project = new Project();
Expand expand = new Expand();
expand.setOverwrite(false);
expand.setProject(project);
expand.setSrc(src);
expand.setDest(new File(destDirectory));
expand.setEncoding("utf-8");
expand.execute();
System.out.println("文件解压成功!!!!!!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
// ZipUtils.zip("D:\\logs\\cz2-web\\", "D:\\log\\" + UUID.randomUUID().toString().toUpperCase() + ".war");
// ZipUtils.unZip("D:\\log\\9BD072CB-17E2-4709-ABE7-5AE17D1B8CDA.war", "D:\\logs\\9BD072CB-17E2-4709-ABE7-5AE17D1B8CDA");
}
}