zoukankan      html  css  js  c++  java
  • apache ant进行zip解压缩操作示例分享

    导入maven依赖
    <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.10.9</version>
    </dependency>
     
    ZipUtils.java
    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.taskdefs.Expand;
    import org.apache.tools.zip.ZipEntry;
    
    import java.io.*;
    import java.util.zip.ZipOutputStream;
    
    public class ZipUtils {
    
        private static final String ENCODE = "UTF-8";
    
        /**
         * 压缩
         *
         * @param inputFilePath 压缩的路径,目录或文件
         * @param zipFileName   zip文件名
         */
        public static void zip(String inputFilePath, String zipFileName) {
            File inputFile = new File(inputFilePath);
            if (!inputFile.exists()) {
                throw new RuntimeException("原始文件不存在!!!");
            }
            File baseTarZipFile = new File(zipFileName).getParentFile();
            if (!baseTarZipFile.exists() && !baseTarZipFile.mkdirs()) {
                throw new RuntimeException("目标文件无法创建!!!");
            }
            BufferedOutputStream bos = null;
            FileOutputStream out = null;
            ZipOutputStream zOut = null;
            try {
                // 创建文件输出对象out,提示:注意中文支持
                out = new FileOutputStream(new String(zipFileName.getBytes(ENCODE)));
                bos = new BufferedOutputStream(out);
                // 將文件輸出ZIP输出流接起来
                zOut = new ZipOutputStream(bos);
                zip(zOut, inputFile, inputFile.getName());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (zOut != null) {
                    try {
                        zOut.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        private static void zip(ZipOutputStream zOut, File file, String base) {
            try {
                // 如果文件句柄是目录
                if (file.isDirectory()) {
                    // 获取目录下的文件
                    File[] listFiles = file.listFiles();
                    // 建立ZIP条目
                    zOut.putNextEntry(new ZipEntry(base + "/"));
                    base = (base.length() == 0 ? "" : base + "/");
                    if (listFiles != null && listFiles.length > 0) {
                        // 遍历目录下文件
                        for (File f : listFiles) {
                            // 递归进入本方法
                            zip(zOut, f, base + f.getName());
                        }
                    }
                } else {
                    // 如果文件句柄是文件
                    if (base == "") {
                        base = file.getName();
                    }
                    // 填入文件句柄
                    zOut.putNextEntry(new ZipEntry(base));
                    // 开始压缩
                    // 从文件入流读,写入ZIP 出流
                    writeFile(zOut, file);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void writeFile(ZipOutputStream zOut, File file) throws IOException {
            FileInputStream in = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(in);
            int len;
            byte[] buff = new byte[2048];
            while ((len = bis.read(buff)) != -1) {
                zOut.write(buff, 0, len);
            }
            zOut.flush();
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 解压
         *
         * @param zipPath         zip文件路径
         * @param destinationPath 解压的目的地点,不存在时会自动创建
         */
        public static void unZip(String zipPath, String destinationPath) {
            File zipFile = new File(zipPath);
            if (!zipFile.exists()) {
                throw new RuntimeException(zipPath + " 不存在");
            }
            Project project = new Project();
            Expand expand = new Expand();
            expand.setProject(project);
            expand.setTaskType("unzip");
            expand.setTaskName("unzip");
            expand.setSrc(zipFile);
            expand.setDest(new File(destinationPath));
            expand.setEncoding(ENCODE);
            expand.execute();
        }
    
        public static void main(String[] args) {
            String dir = "C:\Users\Nihaorz\Desktop\apache-tomcat-8.5.57";
            zip(dir, "C:\Users\Nihaorz\Desktop\tomcat-8.5.57.zip");
            unZip("C:\Users\Nihaorz\Desktop\tomcat-8.5.57.zip", "C:\Users\Nihaorz\Desktop\tomcat-8.5.57");
        }
    }

    参考链接:apache ant进行zip解压缩操作示例分享_java_脚本之家 (jb51.net)

  • 相关阅读:
    语音识别算法阅读之CTC
    语音识别模型阅读之CLDNN
    声纹识别算法阅读之self-attentive x-vector
    Git链接两个远程仓库
    tortoisegit提交不到远程库问题解决记录
    安装 Git 命令之后,本地的工作区中的文件没有小图标解决办法
    .NET CLS(Common Language System)简介
    .NET CTS(Common Type System)简介
    C# 中间语言
    .NET 程序执行流程
  • 原文地址:https://www.cnblogs.com/nihaorz/p/14331558.html
Copyright © 2011-2022 走看看