JAVA-文件压缩

package com.MakeZIP;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class MyZIP {
    private static File file;
    private MyZIP(String outPath){
        boolean f = isName(outPath);
        if(f){
            file = new File(outPath);
            if(file.exists()){
                System.err.println("压缩文件已存在");
            }
        }else{
            System.err.println("压缩文件格式错误");
        }
    }
    
    public static void corress(String inPath){
        File file2 = new File(inPath);
        if(file2.exists() && file2.isDirectory()){
            System.out.println("压缩中。。。。。。");
            Zip zip = new Zip();
            Project pro = new Project();
            zip.setProject(pro);
            zip.setDestFile(file);
            FileSet fileSet = new FileSet();
            fileSet.setProject(pro);
            fileSet.setDir(file2);
            zip.add(fileSet);
            zip.execute();
            System.out.println("压缩完成");
        }else{
            System.err.println("要压缩的文件夹不存在");
        }
    }
    
    public static void main(String args[]){
        MyZIP my = new MyZIP("E:/a.zip");
        my.corress("E:/test");
    }
    
    public static boolean isName(String path){
        String name = path;
        name = name.substring(name.lastIndexOf("."), name.length());
        if(name.equals(".zip") || name.equals(".tar") || name.equals(".tgz")){
            return true;
        }
        return false;
    }
}
原文地址:https://www.cnblogs.com/zqzdong/p/4840353.html