Question

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?

Was it helpful?

Solution

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".

OTHER TIPS

I ended up using IOUtils to solve the problem. Thanks for the pointers Flavio, Duncan

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top