title: Writing ZipOutputStream to OutPutStream blocking the connection
body: <p>Hi I am working on an android file explorer application. When a large folder is requested for download I am zipping the folder and writing it to the outputstream using the code below.But any further requests are blocked until the file is downloaded.What could be the problem ?Should I consider moving this to a thread?I have tried many variations already and none seem to work.Any pointers in the right direction would be greatly appreciated.</p>
  
  <pre><code>        if(f.isDirectory()&amp;&amp; action.equalsIgnoreCase("download")){
          File zip=new File(file);
  
          entity = new EntityTemplate(new ContentProducer() {
              public void writeTo(final OutputStream outstream) throws IOException {
                  action=null;
                  OutputStreamWriter writer=fileop.createZipFile(file, outstream);
                  writer.flush();
              }
          });response.setHeader("Content-Type", "application/zip");
          response.setHeader("Content-Disposition","attachment; filename=\"" +zip.getName()+".zip\"");
          response.setEntity(entity);
      }
  </code></pre>
  
  <p><strong>createZipFileMethod :</strong></p>
  
  <pre><code>     OutputStreamWriter createZipFile(String sourceDir,OutputStream os) {
      // TODO Auto-generated method stub
      OutputStreamWriter osw=null;
          try
      {
          //wrap object of OutputStream
          osw=new OutputStreamWriter(os,"UTF-8");                     
          //create object of ZipOutputStream from OutputStream
          ZipOutputStream zout = new ZipOutputStream(os);
  
          //create File object from source directory
          File fileSource = new File(sourceDir);
  
          addDirectory(zout, fileSource);
  
          //close the ZipOutputStream
          zout.close();
  
          System.out.println("Zip file has been created!");
  
      }
      catch(IOException ioe)
      {
          System.out.println("IOException :" + ioe);     
      }
  
      return osw;
  
  }
  
  private static void addDirectory(ZipOutputStream zout, File fileSource) {
  
      //get sub-folder/files list
      File[] files = fileSource.listFiles();
  
      System.out.println("Adding directory " + fileSource.getName());
  
      for(int i=0; i &lt; files.length; i++)
      {
          //if the file is directory, call the function recursively
          if(files[i].isDirectory())
          {
              addDirectory(zout, files[i]);
              continue;
          }
  
          /*
           * we are here means, its file and not directory, so
           * add it to the zip file
           */
  
          try
          {
              System.out.println("Adding file " + files[i].getName());
  
              //create byte buffer
              byte[] buffer = new byte[1024];
  
              //create object of FileInputStream
              FileInputStream fin = new FileInputStream(files[i]);
  
              zout.putNextEntry(new ZipEntry(files[i].getName()));
  
              /*
               * After creating entry in the zip file, actually
               * write the file.
               */
              int length;
  
              while((length = fin.read(buffer)) &gt; 0)
              {
                  zout.write(buffer, 0, length);
              }
  
              /*
               * After writing the file to ZipOutputStream, use
               *
               * void closeEntry() method of ZipOutputStream class to
               * close the current entry and position the stream to
               * write the next entry.
               */
  
              zout.closeEntry();
  
              //close the InputStream
              fin.close();
  
          }
          catch(IOException ioe)
          {
              System.out.println("IOException :" + ioe);                             
          }
      }
  }
  </code></pre>