title: java.util.zip - ZipInputStream v.s. ZipFile
body: <p>I have some general questions regarding the <code>java.util.zip</code> library. 
  What we basically do is an import and an export of many small components. Previously these components were imported and exported using a single big file, e.g.:</p>
  
  <pre><code>&lt;component-type-a id="1"/&gt;
  &lt;component-type-a id="2"/&gt;
  &lt;component-type-a id="N"/&gt;
  
  &lt;component-type-b id="1"/&gt;
  &lt;component-type-b id="2"/&gt;
  &lt;component-type-b id="N"/&gt;
  </code></pre>
  
  <p><em>Please note that the order of the components during import is relevant.</em></p>
  
  <p>Now every component should occupy its own file which should be <em>externally</em> versioned, QA-ed, bla, bla. We decided that the output of our export should be a zip file (with all these files in) and the input of our import should be a similar zip file. We do not want to explode the zip in our system. We do not want opening separate streams for each of the small files. My current questions:</p>
  
  <p>Q1. May the <code>ZipInputStream</code> guarantee that the zip entries (the little files) will be read in the same order in which they were inserted by our export that uses <code>ZipOutputStream</code>?  I assume reading is something like:</p>
  
  <pre><code>
  ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  ZipEntry entry;
  while((entry = zis.getNextEntry()) != null) 
  {
         //read from zis until available
  }
  </code></pre>
  
  <p>I know that the central zip directory is put at the end of the zip file but nevertheless the file entries inside have sequential order. I also know that relying on the order is an ugly idea but I just want to have all the facts in mind.</p>
  
  <p>Q2. If I use <code>ZipFile</code> (which I prefer) what is the performance impact of calling <code>getInputStream()</code> hundreds of times? Will it be much slower than the <code>ZipInputStream</code> solution? The zip is opened only once and <code>ZipFile</code> is backed by <code>RandomAccessFile</code> - is this correct? 
  I assume reading is something like:</p>
  
  <pre><code>
  ZipFile zipfile = new ZipFile(argv[0]);
  Enumeration e = zipfile.entries();//TODO: assure the order of the entries
  while(e.hasMoreElements()) {
          entry = (ZipEntry) e.nextElement();
          is = zipfile.getInputStream(entry));
  }
  </code></pre>
  
  <p>Q3. Are the input streams retrieved from the same <code>ZipFile</code> thread safe (e.g. may I read different entries in  different threads simultaneously)? Any performance penalties?</p>
  
  <p>Thanks for your answers!</p>