从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • JavaSE

    • 我的Java学习路线
    • 安装Java
    • Java数据类型

    • Java多版本配置
    • 面向对象

    • Java核心类

    • IO

      • IO介绍
      • File对象介绍
      • InputStream
      • OutputStream
      • Decorator模式
      • 读写zip文件
        • 继承关系图
        • 读取zip包
        • 写入zip包
      • Properties类
      • 读取classpath中的配置文件
      • 序列化与反序列化
      • Reader
      • Writer
      • PrintStream和PrintWriter
      • Scanner
      • 使用Files
      • Console
      • IO系列小结
    • Java与时间

    • 异常处理

    • 哈希和加密算法

    • Java8新特性

    • 网络编程

    • Java
  • JavaSenior

  • JavaEE

  • JavaWeb

  • Spring

  • 主流框架

  • SpringMVC

  • SpringBoot

  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • Java
  • JavaSE
  • IO
2023-02-02
目录

读写zip文件

# 05.读写zip文件

​ZipInputStream​是一种FilterInputStream​。它可以直接读取zip包的内容。

‍

# 继承关系图

┌───────────────────┐
│    InputStream    │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│ FilterInputStream │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│InflaterInputStream│
└───────────────────┘
          ▲
          │
┌───────────────────┐
│  ZipInputStream   │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│  JarInputStream   │
└───────────────────┘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

​JarInputStream​从ZipInputStream​派生,它增加的主要功能是直接读取jar文件里面的MANIFEST.MF​文件。因为本质上jar包就是zip包,只是额外附加了一些固定的描述文件。

‍

‍

# 读取zip包

我们先创建一个HelloWorld.txt文件,文件里的内容为HelloWorld,并通过压缩工具压缩该文件,形成一个压缩包

我们编写代码来读取其内容:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class IODemo7Zip {
  public static void main(String[] args) throws IOException{
    try ( 
      FileInputStream file = new FileInputStream("readme.zip");

      ZipInputStream zip = new ZipInputStream(file)) {
        ZipEntry entry = null;
        while(null != ( entry = zip.getNextEntry())){
          if(!entry.isDirectory()){
            int n;
            while( -1 != (n = zip.read())){
              System.out.println(n);
            }
          }
        }
    }
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

一个ZipEntry​表示一个压缩文件或目录,如果是压缩文件,我们就用read()​方法不断读取,直到返回-1​(表明已读完一个文件,准备读下一个文件了)。

‍

输出结果为HelloWorld的ASCII码:

72
101
108
108
111
87
111
114
108
100
1
2
3
4
5
6
7
8
9
10

‍

# 写入zip包

我们可以使用ZipOutputStream​将内容写入到zip包。ZipOutputStream​是一种FilterOutputStream​。

我们要先创建一个ZipOutputStream​,通常是包装一个FileOutputStream​,然后,每写入一个文件前,先调用putNextEntry()​,然后用write()​写入byte[]​数据,写入完毕后调用closeEntry()​结束这个文件的打包。

File file = new File("readme.txt");
File fileZip = new File("readme.zip");
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(fileZip))) {
  zip.putNextEntry(new ZipEntry(file.getPath()));
  zip.write(Files.readAllBytes(file.toPath()));
  zip.closeEntry();
} catch (Exception e) {
  e.printStackTrace();
} 
1
2
3
4
5
6
7
8
9

‍

运行后会在本目录下生成一个zip压缩包,打开压缩包可以看到有readme.txt。

‍

# 小结

​ZipInputStream​可以读取zip格式的流,ZipOutputStream​可以把多份数据写入zip包;

配合FileInputStream​和FileOutputStream​就可以读写zip文件。

在GitHub上编辑此页 (opens new window)
上次更新: 2023/2/10 08:54:20
Decorator模式
Properties类

← Decorator模式 Properties类→

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式