title: Thread blocking sometimes while unzipping png files
body: <p>I am developing an android app that requires downloading a zip file (around 1,5 MB max) with a small amount of logos (png files of 20-30KB average size) from  a webserver.</p>
  
  <p>I have encapsulated the process of downloading and unzipping the files into android internal storage in an <code>AsyncTask</code>'s <code>doInbackground()</code> method.<br>
  The issue I have is that the <code>unZipIntoInternalStorage()</code> method I have developed (pasted down), <strong>sometimes</strong> runs forever. Usually it takes around 900ms  seconds to unzip and save the logos into internal storage, but for some unknown reason around 1 of 4 executions blocks during the loop (and stays there "for ever" taking more than 2 or 3 mins to decompress all png files): </p>
  
  <pre><code>while ((count = zipInputStream.read(buffer)) != -1) {
           outputStream.write(buffer, 0, count);
  }
  </code></pre>
  
  <p><strong>Edited</strong>: After doing some logging and debugging I found out that the line slowing down so much the execution is : <code>zipInputStream.read(buffer)</code> inside the while condition. Any ideas why sometimes it runs extremely fast and some others extremely slow?  </p>
  
  <p>Here is my complete method to unzip the downloaded files and save them into android internal storage. I also add the method where the <code>zipInputStream</code> is initialized, from the zip file downloaded (both methods executed inside <code>doInBackground()</code> ): </p>
  
  <pre><code>        private void unZipIntoInternalStorage(ZipInputStream zipInputStream) {
          long start = System.currentTimeMillis();
          Log.i(LOG_TAG, "Unzipping started ");
          try {
              File iconsDir = context.getDir("icons", Context.MODE_PRIVATE);
              ZipEntry zipEntry;
              byte[] buffer = new byte[1024];
              int count;
              FileOutputStream outputStream;
  
              while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                  File icon = new File(iconsDir, zipEntry.getName());
                  outputStream = new FileOutputStream(icon);
  
                  while ((count = zipInputStream.read(buffer)) != -1) {
                      outputStream.write(buffer, 0, count);
                  }
                  zipInputStream.closeEntry();
                  outputStream.close();
              }
              zipInputStream.close();
          } catch (Exception e) {
              Log.e(LOG_TAG + " Decompress", "unzip error ", e);
              e.printStackTrace();
          }
          Log.i(LOG_TAG, "Unzipping completed time required: " + (System.currentTimeMillis() - start) + " ms");
      }
  
      private ZipInputStream httpDownloadIconsZip(String zipUrl) {
  
          URLConnection urlConnection;
          try {
              URL finalUrl = new URL(zipUrl);
              urlConnection = finalUrl.openConnection();
              return new ZipInputStream(urlConnection.getInputStream());
          } catch (IOException e) {
              Log.e(LOG_TAG, Log.getStackTraceString(e));
              return null;
          }
      }
  </code></pre>
  
  <p>To clarify, after testing this method several times and debugging, the blocking for ever always happens in the nested while loop I described previously. But I can't find the reason (see edited clarification)
  Also I have already tried this method using <code>BufferedOutputStream</code> class and with the same results: nested while loop running forever sometimes and others unzipping successfully in less than a second. </p>
  
  <p>Hope I have been as clear as possible, since I have spent long hours looking for posible causes to the issue in several post regarding unzipping files or java I/O methods with no success. </p>
  
  <p>Any help appreciated. Thanks   </p>