9

I found example from SUN site (http://java.sun.com/developer/technicalArticles/Programming/compression/), but it returns BufferedOutputStream. But I would like to get ZipEntry file as InputStream and then process next file. Is that possible? My program has no access to harddisk, so it cannot even temporarily save files.

import java.io.*;
import java.util.zip.*;

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
          FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
newbie
  • 24,286
  • 80
  • 201
  • 301
  • And if you want to do something similar with nested zip files (inside of other zip files), some details on that can be found here: https://stackoverflow.com/q/75096589/2657515 – JonathanDavidArndt Jan 13 '23 at 05:06

2 Answers2

16

Well, just change the part that writes the file into something you want to do with the data.

while((entry = zis.getNextEntry()) != null) {
    System.out.println("Extracting: " + entry);
    int count;
    byte[] data = new byte[BUFFER];
    String filename = entry.getName();
    System.out.println("Filename: " + filename);
    while ((count = zis.read(data, 0, BUFFER)) != -1) {
       // Do whatever you want with the data variable
       System.out.println(data);
    }
}
JayTheKay
  • 1,463
  • 12
  • 22
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • `byte[] data = new byte[BUFFER];` ?? – John Smith Jan 24 '18 at 19:53
  • @JohnSmith puth a 1024 int there it is the number of bytes you are going to read from thee zip entry each While iteration – Glabler Sep 26 '19 at 17:19
  • 1
    if one would like to convert the `data` into a String, don't forget to remove trailing empty bytes in the array! Convert it like this `new String(data).replaceAll("\u0000.*", "")`.This solution was taken from [this question](https://stackoverflow.com/questions/3857803/removing-extra-empty-characters-from-byte-array-and-converting-to-a-string) – JayTheKay Oct 23 '19 at 16:00
  • 1
    @Jay no, you take `count` bytes out of `data` to prevent those null characters in the first place! Thanks for the edit anyway. – CodeCaster Oct 23 '19 at 17:21
  • Right, makes sense, thanks for the clarification! So it should be `new String(data, 0, count)` which is much better :) – JayTheKay Oct 24 '19 at 11:51
0

This process reads the whole zip file and just dumps it to a stream, it does not read/split the individual files within the zip. Its sad that the ZIP library does not provide one with the inputstream of each individual file within the ZIP stream.

JakesIV
  • 41
  • 4