ZIP解压缩工具类

import java.io.File;
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;

/**
 * ZIP解压缩工具类
 */
public class Zipper {

    // ZIP解压缩时
    public final static String encoding = "GBK";

    /**
     * 压缩文件或文件夹
     * ------------------------------------
     * @param srcPathname 需要被压缩的文件或文件夹路径
     * @param zipFilepath 将要生成的ZIP文件路径
     */
    public static void zip(String srcPathname, String zipFilepath) throws Exception {
        System.out.println(String.format("start compress ( %s ).", srcPathname));

        // 检查文件是否存在
        File file = new File(srcPathname);
        if (!file.exists()) {
            throw new RuntimeException(String.format("source file or directory ( %s ) does not exist.", srcPathname));
        }

        // 创建项目
        Project proj = new Project();

        // 文件设置
        FileSet fileSet = new FileSet();
        fileSet.setProject(proj);
        if (file.isDirectory()) { // 如果是目录
            fileSet.setDir(file);
        } else { // 如果是单个文件
            fileSet.setFile(file);
        }

        // 压缩文件保存到目标地址
        Zip zip = new Zip();
        zip.setProject(proj);
        zip.setDestFile(new File(zipFilepath));
        zip.addFileset(fileSet);
        zip.setEncoding(encoding);
        zip.execute();

        System.out.println(String.format("compress successed of ( %s ). zip file is ( %s )", srcPathname, zipFilepath));
    }

    /**
     * 解压缩文件或文件夹
     * ------------------------------------
     * @param zipFilepath    需要被解压的ZIP文件路径
     * @param destDir          将要被解压到的目标文件夹
     */
    public static void unzip(String zipFilepath, String destDir) throws Exception {
        System.out.println(String.format("start uncompress ( %s ).", zipFilepath));

        // 判断要解压的ZIP包是否存在
        File file = new File(zipFilepath);
        if (!file.exists()) {
            throw new RuntimeException(String.format("zip file ( %s ) does not exist.", zipFilepath));
        }

        // 创建项目
        Project proj = new Project();

        // 解压设置
        Expand expand = new Expand();
        expand.setProject(proj);
        expand.setTaskType("unzip");
        expand.setTaskName("unzip");
        expand.setEncoding(encoding);

        expand.setSrc(new File(zipFilepath));
        expand.setDest(new File(destDir));

        // 执行解压
        expand.execute();

        System.out.println(String.format("uncompress successed of ( %s ).", zipFilepath));
    }

    /**
     * 测试解压缩方法
     */
    public static void main(String[] args) {

        /*try {
            String srcPathname = "D:\\test\\test";
            String zipFilepath = "D:\\test\\test.zip";
            Zipper.zip(srcPathname, zipFilepath);
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        try {
            String zipFilepath = "D:\\test\\test.zip";
            String destDir = "D:\\test\\upzip";
            Zipper.unzip(zipFilepath, destDir);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 

依赖jar

<dependency>
  <groupId>org.apache.ant</groupId>
  <artifactId>ant</artifactId>
  <version>1.9.7</version>
</dependency>

posted @ 2018-04-25 15:37  迷失之路  阅读(278)  评论(0编辑  收藏  举报