I have byte array which was converted from zip file. I would like to check in the test whether its files contains proper content. So for example zip consists of 2 text files. First file contains "HEY" word, and the sceond "HEY2". I would like to after reconverting whether everything is all right.
I have made something like this:
and it prints:
Entry: 1.txt len 5 added 03/16/21
Entry: 2.txt len 6 added 03/16/21
ZipInputStream (and for that matter ZipOutputStream as well) are a bit odd, in that they reset themselves for each entry. That means that once you've used getNextEntry, you can start reading from the ZipInputStream and it will only read the contents of that entry. Its read methods will return -1 once the entry is read. When you call getNextEntry again, you can start reading from the ZipInputStream again, etc.
So in short, to read everything from every entry:
One warning: don't close the ZipInputStream, because it will close the entire ZipInputStream. You won't be able to read any more entries.
That last part can be annoying with ZipOutputStream when you need to wrap it into some OutputStream that needs to be closed to finalize its work (e.g. ObjectOutputStream). You can use CloseShieldOutputStream (again from Apache Commons IO) to wrap the ZipOutputStream, then wrap that. Closing the outer OutputStream will then stop at the CloseShieldOutputStream: