美文网首页
25.IO (4)操作Zip

25.IO (4)操作Zip

作者: 呦丶耍脾气 | 来源:发表于2025-06-29 09:14 被阅读0次

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

┌───────────────────┐
│    InputStream    │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│ FilterInputStream │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│InflaterInputStream│
└───────────────────┘
          ▲
          │
┌───────────────────┐
│  ZipInputStream   │
└───────────────────┘
          ▲
          │
┌───────────────────┐
│  JarInputStream   │
└───────────────────┘

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

读取zip包

读取zip,要创建一个ZipInputStream,通常是传入一个FileInputStream作为数据源,然后,循环调用getNextEntry(),直到返回null,表示zip流结束。

一个ZipEntry表示一个压缩文件或目录,如果是压缩文件,我们就用read()方法不断读取,直到返回-1:

try (ZipInputStream zip = new ZipInputStream(new FileInputStream("C:\\Users\\computer\\Downloads\\test.zip"))) {
    ZipEntry entry = null;
    while ((entry = zip.getNextEntry()) != null) {
        String name = entry.getName();
        System.out.println(name);// 输出文件或目录名称
        if (!entry.isDirectory()) {
            int n;
            while ((n = zip.read()) != -1) {
                System.out.print((char) n);
            }
        }
    }
}

写入zip包

  • 创建一个ZipOutputStream,通常是包装一个FileOutputStream
  • 写入一个文件前,先调用putNextEntry()
  • write()写入byte[]数据,写入完毕后调用closeEntry()结束这个文件的打包。
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("C:\\Users\\computer\\Downloads\\test.zip"))) {
    File[] files = new File[]{
            new File("C:\\Users\\computer\\Downloads\\mcbtcrq2-70h9c9aez8g.png"),
            new File("C:\\Users\\computer\\Downloads\\image.html"),
    };
    for (File file : files) {
        zip.putNextEntry(new ZipEntry(file.getName()));
        zip.write(Files.readAllBytes(file.toPath()));
        zip.closeEntry();
    }
}

如果压缩包一致,会导致原压缩包中所有文件都会丢失

相关文章

网友评论

      本文标题:25.IO (4)操作Zip

      本文链接:https://www.haomeiwen.com/subject/nfpugjtx.html