Reputation: 303
I have a zip file with 3 relatively large (several mb) binary files I need to read into a buffer to perform some regex operations on.
//For entry in zip.getentries loop above
long size = entry.getSize();
byte[] bytes = new byte[(int) size];
int numBytesRead = zipFile.getInputStream(entry).read(bytes);
if (numBytesRead != size) {
Logger.getLogger(this.getClass()).debug(
"Read " + numBytesRead + " bytes from zip entry but size of the
zip entry is " + size + " bytes");
//continue; - If I do not comment this out the size check fails and nothing happens
}
FileUtils.writeByteArrayToFile(extractFile, bytes);
For some reason I am only getting about 1/15 of the data or so into the byte array (43 out of 642 records in one example). I originally thought it was an issue perhaps casting size to int, but size is typically between 1,000,000 - 10,000,000 or so so that isn't a problem. Any thoughts?
Upvotes: 1
Views: 248
Reputation: 303
I ended up using IOUtils to solve the problem. Thanks for the pointers Flavio, Duncan
Upvotes: 0
Reputation: 11977
InputStream.read
is not required to read all the data. It will read some data, with an amount varying between 1 and the size of the array.
You will have to call the function repeatedly until you read all the data you need.
For reference, check the API docs: they explicitly say "a smaller number may be read".
Upvotes: 3