Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A reader for getting text lines from a zipped text file

Summary: Having the byte image of a.zip that contains a.txt, how can I get a clean and correct reader that returns lines of the text file?

I do download the image of a zip file from a web service into the byte[] content. I would like to write a method like

private BufferedReader contentToBufferedReader(byte[] content)

that would return a reader that can be used like

reader = contentToBufferedReader(content);
while ((line = reader.readLine()) != null) {
    processThe(line);
}
reader.close()

So far, I have (updated)

private BufferedReader contentToBufferedReader(byte[] content) {

    ByteArrayInputStream bais = new ByteArrayInputStream(content);
    ZipInputStream zipStream = new ZipInputStream(bais);
    BufferedReader reader = null;

    try {
        ZipEntry entry = zipStream.getNextEntry();

        // I need only the first (and the only) entry from the zip file.
        if (entry != null) {
            reader = new BufferedReader(new InputStreamReader(zipStream, "UTF-8"));
            System.out.println("contentToBufferedReader(): success");
        }
    }
    catch (IOException e) {
        System.out.println("contentToBufferedReader(): failed...");
        System.out.println(e.getMessage());
    }

    return reader;
}

I am not sure how to close all of the stream object when something fails. Moreover, I am not sure how to close them if the reader was successfully returned, used, and closed.

like image 340
pepr Avatar asked Nov 02 '22 13:11

pepr


1 Answers

This will get the bytes out all in one go (uses guava ByteStreams for convenience)

ZipEntry entry = zipStream.getNextEntry();
while (entry != null) {
  if (!entry.isDirectory()) {
    String filename = entry.getName();//this includes the path!
    byte[] data = ByteStreams.toByteArray(zipStream);
    //do something with the bytes 
  }
  entry = zipIn.getNextEntry();
}

you can get a reader like this:

InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(data)));

The zipStream advances when you call zipStream.getNextEntry(). I also think the stream doesn't support mark and reset iirc, which means you only get to read it once (hence the get it all out at once before passing it to other processing that may need random access)

like image 165
tom Avatar answered Nov 09 '22 04:11

tom