Java - Use stream() method to read zip file with ZipFile class

Introduction

stream() method returns a Stream of ZipEntry objects.

Demo

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
  public static void main(String[] args) throws IOException {
    // Create a ZipFile object using the ZIP file name
    ZipFile zf = new ZipFile("ziptest.zip");

    // Get the Stream of all zip entries and apply some actions on each of them
    Stream<? extends ZipEntry> entryStream = zf.stream();
    entryStream.forEach(entry -> {//from  w ww .ja v a  2 s .c  o m
      try {
        // Get the input stream for the current zip entry
        InputStream is = zf.getInputStream(entry);

        /* Read data for the entry using the is object */
      } catch (IOException e) {
        e.printStackTrace();
      }

      // Print the name of the entry
      System.out.println(entry.getName());
    });
  }
}

Related Topic