title: Memory-mapped zip file in Java
body: <p>Here is the problem I'm trying to solve:</p>
  
  <p>I have about 100 binary files (in total 158KB and they are roughly the same size +/- 50% of each other). I need to selectively parse only a few of these files (in the worst case maybe 50, in other cases as little as 1 to 5). This is on an Android device, by the way.</p>
  
  <p><em>What is the fastest way to do this in Java?</em></p>
  
  <p>One way could be combining everything into one file and then using file seek to get to the each individual file. That way file open would only need to be called once and that is usually slow. However, in order to know where each file is there would need to be some sort of table in the beginning of the file -- which could be generated using a script -- but the files would also need to be indexed in the table in the order that they were concatenated so file seek wouldn't have to do much work (correct me if I'm wrong).</p>
  
  <p>A better way would be to make the file memory-mapped and then the table wouldn't have to be in sorted order of concatenation because the memory-mapped file would have random access (again correct me if I'm wrong).</p>
  
  <p>Creating that table would be an unnecessary if zip compression was used because zip compression already makes a table. In addition, all the files wouldn't have to be concatenated. I could zip the directory and then access each of the individual files by their entries in the zip file. Problem solved.</p>
  
  <p>Except if the zip file isn't memory-mapped, it will be slower to read, since system calls are slower than direct memory access (correct me if I'm wrong). <strong>So I came to the conclusion that the best solution would be to use a memory-mapped zip archive.</strong></p>
  
  <p>However, the <code>ZipFile</code> entries return an <code>InputStream</code> to read the contents of the entry. And the <code>MappedByteBuffer</code> needs a <code>RandomAccessFile</code> which takes a filename as input, not an <code>InputStream</code>.</p>
  
  <p><em>Is there anyway to memory-map a zip file for fast reads? Or is there a different solution to this problem of reading a selection of files?</em></p>
  
  <p>Thanks</p>
  
  <p>EDIT: I tested speeds of open, close, and parsing of the files here are the statistics that I found:</p>
  
  <p><code>Number of Files: 25 (24 for parse because garbage collection interrupted timing)</code><br>
  <code>Total Open Time: 72ms</code><br>
  <code>Total Close Time: 1ms</code><br>
  <code>Total Parse Time:  515ms</code>  </p>
  
  <p>(this is skewed in Parse's favor because Parse is missing a file)<br>
  <code>%Total time Open takes: 12%</code><br>
  <code>%Total time Close takes: 0.17%</code><br>
  <code>%Total time Parse takes: 88%</code>  </p>
  
  <p><code>Avg time Open takes per file: 2.88ms</code><br>
  <code>Avg time Close takes per file: 0.04ms</code><br>
  <code>Avg time Parse takes per file: 21.46ms</code>  </p>