zoukankan      html  css  js  c++  java
  • Java之解压流(ZipInputStream)

      一、ZipInputStream相对于ZipOutputStream而言,使用上面简单的多了,相对的,既然存在压缩流,就会存在,解压的方式。

      二、解压文件,流的使用过程中也是很常用的,在读取文件,根据文件类型进行处理,这样,就可以做到,最低成本的数据传输了

      三、解压例子

    /**
         * 提供给用户使用的解压工具
         * @param srcPath
         * @param outPath
         * @throws IOException
         */
        public static void decompressionFile(String srcPath, String outPath) throws IOException {
            //简单判断解压路径是否合法
            if (!new File(srcPath).isDirectory()) {
                //判断输出路径是否合法
                if (new File(outPath).isDirectory()) {
                    if (!outPath.endsWith(File.separator)) {
                        outPath += File.separator;
                    }
                    //zip读取压缩文件
                    FileInputStream fileInputStream = new FileInputStream(srcPath);
                    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
                    //解压文件
                    decompressionFile(outPath, zipInputStream);
                    //关闭流
                    zipInputStream.close();
                    fileInputStream.close();
                } else {
                    throw new RuntimeException("输出路径不合法!");
                }
            } else {
                throw new RuntimeException("需要解压的文件不合法!");
            }
        }
    
        /**
         * ZipInputStream是逐个目录进行读取,所以只需要循环
         * @param outPath
         * @param inputStream
         * @throws IOException
         */
        private static void decompressionFile(String outPath, ZipInputStream inputStream) throws IOException {
            //读取一个目录
            ZipEntry nextEntry = inputStream.getNextEntry();
            //不为空进入循环
            while (nextEntry != null) {
                String name = nextEntry.getName();
                File file = new File(outPath+name);
                //如果是目录,创建目录
                if (name.endsWith("/")) {
                    file.mkdir();
                } else {
                    //文件则写入具体的路径中
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    int n;
                    byte[] bytes = new byte[1024];
                    while ((n = inputStream.read(bytes)) != -1) {
                        bufferedOutputStream.write(bytes, 0, n);
                    }
                    //关闭流
                    bufferedOutputStream.close();
                    fileOutputStream.close();
                }
                //关闭当前布姆
                inputStream.closeEntry();
                //读取下一个目录,作为循环条件
                nextEntry = inputStream.getNextEntry();
            }
        }

      四、测试:

    public static void main(String[] args) throws IOException {
            decompressionFile("D:\srv.zip", "D:\test");
    }

  • 相关阅读:
    前后端交互, 安装drf, restful接口规范, pycharm断点调试
    django中文设置, axios, CORS, 全局js配置, Vue配置jq + bs
    js原型, Vue项目环境搭建, Vue项目目录结构, Vue项目生命周期, 小组件使用, 全局样式, 路由跳转, 组件的生命周期钩子, 路由传参
    Vue组件
    Vue表单指令, 条件指令, 循环指令, 成员
    question1 赋值运算操作符
    CH15 面向对象程序设计
    CH12 动态内存
    CH11 关联容器
    CH10 泛型算法
  • 原文地址:https://www.cnblogs.com/ll409546297/p/9340129.html
Copyright © 2011-2022 走看看