zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    Java学习之Math类理解
    Java学习之集合框架的迭代器--Iteratorjk及ListItertor接口
    Java学习关于随机数工具类--Random类
    集合框架学习之List接口
    Java学习关于集合框架的基础接口--Collection接口
    Java基础学习之数组基本属性和方法
    Java学习关于时间操作的应用类--Date类、Calendar类及其子类
    Java学习--异常处理及其应用类
    LR百分比模式
    lr函数之lr_eval_string()函数的使用学习
  • 原文地址:https://www.cnblogs.com/zj0208/p/8945149.html
Copyright © 2011-2022 走看看