Java之解压流(ZipInputStream)

摘要:
}//Zip读取压缩文件FileInputStreamfileInputStream=newFileInputStream(srcPath)//关闭流zipInputStream。close();ZipInputStreaminputStream)抛出IOException{//读取目录ZipEntrynextEntry=inputStream.getNextEntry();

  一、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");
}

Java之解压流(ZipInputStream)第1张Java之解压流(ZipInputStream)第2张

免责声明:文章转载自《Java之解压流(ZipInputStream)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DEV-C++支持C99标准设置方法oracle 存储过程(1)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

h5接入微信分享sdk,报错Cannot read property of undefined (reading 'title')

直接下载jweixin-1.6.0.js文件,引入文件 import wx from './jweixin-1.6.0.js'; 解决方法: 将jweixin-1.6.0.js文件中的this改为window就可以了 其他方法,参考: https://blog.csdn.net/illlllllllllll/article/details/112789...

Rocksdb Compaction原理

概述      compaction主要包括两类:将内存中imutable 转储到磁盘上sst的过程称之为flush或者minor compaction;磁盘上的sst文件从低层向高层转储的过程称之为compaction或者是major compaction。对于myrocks来说,compaction过程都由后台线程触发,对于minor compacti...

Windows Embedded CE 6.0开发环境的搭建

第一步,安装VS2k5,安装完成后要安装VS2k5 SP1补丁(KB926601),否则后面的Platform Builder SP1无法安装。       第二步,安装Windows Embedded CE 6.0,官方下载地址为: http://www.microsoft.com/downloads/details.aspx?familyid=7...

Linux 安装 docker 后的步骤

预计阅读时间: 16分钟 本节包含用于配置 Linux 主机以使其与 Docker 配合使用的可选过程。 以非 root 用户管理 Docker Docker 守护程序绑定到 Unix 套接字而不是 TCP 端口。默认情况下, Unix 套接字由用户拥有 root ,其他用户只能使用来访问它 sudo 。 Docker 守护程序始终以 root 用户身份运...

Apache Tomcat 7 安装与配置

下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz 安装与配置 安装之前需要确保已经安装JDK,若没有安装JDK请参考上一篇blog先正确安装JDK: Linux CentOS...

linux ls命令按时间显示文件

  本文介绍下,使用ls命令显示文件,并按时间排序的方法,供大家学习参考。 在linux系统中,使用ls命令按时间排序文件,其实很简单,如下: #ls -tr 即可按时间排序当前目录下的文件。 附,ls命令的参数中文详解: -a 列出目录下的所有文件,包括以 . 开头的隐含文件。 -b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式...