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.