title: How to compare 2 InputStreams of every entry in a zip file
body: <p>I have 2 zip files that contain a folder with 2 text files. </p>
  
  <p>my first zip file:</p>
  
  <pre><code>fodler1
  |_ file1.txt
  |_ file2.txt
  </code></pre>
  
  <p>my second zip file:</p>
  
  <pre><code>folder1
  |_ file1.txt
  |_ file2.txt
  </code></pre>
  
  <p>Both zip files are passed as a Byte Array. I working on a way to compare every entry in those 2 zip files. So if files are named the same but have different content, the function should return false. This is my approach:</p>
  
  <pre><code>private void compareZip (byte[] zip1, byte[] zip2) throws IOException{
  
          ZipInputStream zipStream1 = new ZipInputStream(new ByteArrayInputStream(zip1));
          HashMap &lt;String, InputStream&gt; allEntries1 = new HashMap&lt;String, InputStream&gt;();
          ZipEntry entry1 = null;
  
          //Save InputStream of every entry in zip1
          while ((entry1 = zipStream1.getNextEntry()) != null) {
              allEntries1.put(entry1.getName(), zipStream1);
              zipStream1.closeEntry();
          }
  
          ZipInputStream zipStream2 = new ZipInputStream(new ByteArrayInputStream(zip2));
          HashMap &lt;String, InputStream&gt; allEntries2 = new HashMap&lt;String, InputStream&gt;();
          ZipEntry entry2 = null;
  
          //Save InputStream of every entry in zip2
          while ((entry2 = zipStream2.getNextEntry()) != null) {
              allEntries2.put(entry2.getName(), zipStream2);
              zipStream2.closeEntry();
          }
  
          //Returns true although files have different content
          isEqual(allEntries1.get("folder1/file1.txt"), allEntries2.get("folder1/file1.txt"));
  
  
          zipStream1.close();
          zipStream2.close(); 
  }
  </code></pre>
  
  <p>The function <code>isEqual</code> compares 2 InputStreams:</p>
  
  <pre><code>private static boolean isEqual(InputStream i1, InputStream i2)
                  throws IOException {
  
              ReadableByteChannel ch1 = Channels.newChannel(i1);
              ReadableByteChannel ch2 = Channels.newChannel(i2);
  
              ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
              ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);
  
              try {
                  while (true) {
  
                      int n1 = ch1.read(buf1);
                      int n2 = ch2.read(buf2);
  
                      if (n1 == -1 || n2 == -1) return n1 == n2;
  
                      buf1.flip();
                      buf2.flip();
  
                      for (int i = 0; i &lt; Math.min(n1, n2); i++)
                          if (buf1.get() != buf2.get())
                              return false;
  
                      buf1.compact();
                      buf2.compact();
                  }
  
              } finally {
                  if (i1 != null) i1.close();
                  if (i2 != null) i2.close();
              }
          }
  </code></pre>