If you want multiple threads to read from the same zipfile in a scalable way, you must open one ZipFile
instance per thread. That way, the per-thread lock in the ZipFile
methods does not block all but one thread from reading from the zipfile at one time. It also means that when each thread closes the ZipFile
after they're done reading, they close their own instance, not the shared instance, so you don't get an exception on the second and subsequent close.
Protip: if you really care about speed, you can get more performance by reading all the ZipEntry
objects from the first ZipFile instance, and sharing them with all threads, to avoid duplicating work in reading ZipEntry
objects for each thread separately. A ZipEntry
object is not tied to a specific ZipFile
instance per se, ZipEntry
just records metadata that will work with any ZipFile
object representing the same zipfile that the ZipEntry
came from.