import java.io.File
public class ZipCompressorByAnt {
private File zipFile
/**
* 压缩文件构造函数
* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile)
}
/**
* 执行压缩操作
* @param srcPathName 需要被压缩的文件/文件夹
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName)
File srcdir = new File(srcPathName)
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!")
}
Project prj = new Project()
Zip zip = new Zip()
zip.setProject(prj)
zip.setDestFile(zipFile)
FileSet fileSet = new FileSet()
fileSet.setProject(prj)
fileSet.setDir(srcdir)
//fileSet.setIncludes("**/*.java")//包括哪些文件或文件夹 eg:zip.setIncludes("*.java")
//fileSet.setExcludes(...)//排除哪些文件或文件夹
zip.addFileset(fileSet)
zip.execute()
}
}
public class TestZip {
public static void main(String[] args) {
ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\test1.zip ")
zca.compressExe("E:\\test1")
}
}
/*如果 出现ant 的 52 51 50 等版本问题 可以去找对应的ant-1.8.2.jar 我开始用的ant-1.10.1.jar 就是这个包版本高了 一直报verson 52 版本问题*/
public class ZipFile {public static void main(String[] args) throws Exception {
//zip -r qq.zip WEB-INF -x '*.DS_Store'
ZipFile zipFile = new ZipFile()
zipFile.start("zip -r qq.zip WEB-INF -x '*.DS_Store'")
}
private synchronized void start(String cmd) throws Exception {
Process pro = null
pro = Runtime.getRuntime().exec(cmd)
new DoOutput(pro.getInputStream()).start()
new DoOutput(pro.getErrorStream()).start()
}
private class DoOutput extends Thread {
public InputStream is
public DoOutput(InputStream is) {
this.is = is
}
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(this.is))
String str = null
StringBuilder sb = new StringBuilder()
try {
while ((str = br.readLine()) != null){
sb.append(str+"\n")
}
System.out.println(sb.toString())
} catch (Exception e) {
e.printStackTrace()
} finally {
if (br != null) {
try {
br.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
}
}
}