title: How to access a zipEntry from a streamed zip file in memory
body: <p>I'm currently implementing an Ereader library (<a href="http://skyepub.net/" rel="nofollow noreferrer">skyepub</a>) that requires that I implement a method that checks if a zipEntry exists or not. In their demo version, the solution is simple: </p>
  
  <pre><code>public boolean isExists(String baseDirectory,String contentPath) {
      setupZipFile(baseDirectory,contentPath);
      if (this.isCustomFont(contentPath)) {
          String path = baseDirectory +"/"+ contentPath;
          File file = new File(path);
          return file.exists();
      }
  
      ZipEntry entry = this.getZipEntry(contentPath);
      if (entry==null) return false;
      else return true;       
  }
  
  // Entry name should start without / like META-INF/container.xml 
  
  private ZipEntry getZipEntry(String contentPath) {
  
      if (zipFile==null) return null;
  
      String[] subDirs = contentPath.split(Pattern.quote(File.separator));
  
      String corePath = contentPath.replace(subDirs[1], "");
  
      corePath=corePath.replace("//", "");
  
      ZipEntry entry = zipFile.getEntry(corePath.replace(File.separatorChar, '/'));
  
      return entry;
  
  }
  </code></pre>
  
  <p>So as you can see, you can access the ZipEntry in question in O(1) time using <code>getZipEntry(contentPath);</code></p>
  
  <p>However, in my case I cannot read the zipfile straight from the file system (it must be read from in memory for security reasons).. And so my <code>ifExists</code> implementation actually goes through the zip file <em>one entry at a time</em>, until it finds the zipEntry in question, here is the relevant part:</p>
  
  <pre><code>try {
          final InputStream stream = dbUtil.getBookStream(bookEditionID);
          if( stream == null) return null;
  
          final ZipInputStream zip = new ZipInputStream(stream);
  
          ZipEntry entry;
          do {
              entry = zip.getNextEntry();
              if( entry == null) {
                  zip.close();
                  return null;
              }
          } while( !entry.getName().equals(zipEntryName));
  
      } catch( IOException e) {
          Log.e("demo", "Can't get content data for "+contentPath);
          return null;
      }
  
      return data;
  </code></pre>
  
  <p>and so if data exists, <code>ifExists</code>returns true, otherwise false if null.</p>
  
  <h1>Question</h1>
  
  <p>Is there a way I can find the zip entry in question from the entire ZipInputStream in O(1) time rather than O(n) time?</p>
  
  <h1>Related</h1>
  
  <p>See <a href="https://stackoverflow.com/questions/36768080/reading-a-decrypted-file-into-a-zipinputstream-truncates-first-file-sometimes">this</a> question
  and <a href="https://stackoverflow.com/a/36797263/766570">this</a> answer.</p>