title: Dynamic ziping and downloading of zip files in java
body: <p>I am trying to create a zip file and download the same on click of a link in jsf.
  I am not able to view the zip file created nor able to download it. I am getting the below exceptions in the back end along with <strong>nullpointer exception.</strong>
  <strong>java.lang.IllegalStateException: Servlet response already use stream, Writer not possible</strong></p>
  
  <p>please go through the code below and help me in this regard.</p>
  
  <pre><code>//I am calling downloadZip2() method on click of the command link in jsf
  public void downloadZip2() {
      System.out.println("downloadZip.....................");
  
      crgAttList = nameDTO.getAttachmentsList();
  
      // File zipping implementation
      String directoryPath ="G:/attachment";
  
      logger.info("Directory-Path===" + directoryPath);
      File directoryToZip = new File(directoryPath);
  
      ArrayList&lt;String&gt; attachmentsList = new ArrayList&lt;String&gt;();
  
      for (int i = 0; i &lt; crgAttList .size(); i++)
      {           
          attachmentsList.add(crgAttList.get(i).getAttachmentNameWithPath());     
      }
  
      if (attachmentsList != null &amp;&amp; attachmentsList.size() &gt; 0) {
          logger.info("---Creating zip file");
          logger.info("Attachment-List-Size=====&gt;" + attachmentsList.size());
          zipAttachments(attachmentsList, directoryPath + ".zip");
      }
  
  }
  
  
  public void zipAttachments(List&lt;String&gt; attachmentsList, String zipFileName) {
      byte[] buf = null;
      ZipOutputStream out = null;
      FileInputStream in = null;  
      OutputStream os = null;     
  
      try {
          FacesContext context = FacesContext.getCurrentInstance();
          HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
  
           response.setContentType("application/zip");
              response.setHeader("Content-Disposition",
                      "attachment;filename=attachment.zip");
  
  
          System.out.println(" in zipAttachments before zipFile creation");
          buf = new byte[1024];
          os = new FileOutputStream(zipFileName);
          out = new ZipOutputStream(os);
  
          out = new ZipOutputStream(response.getOutputStream());
  
  
          for (int i = 0; i &lt; attachmentsList.size(); i++) {
              System.out.println(" in zipAttachments for before adding :: "
                      + attachmentsList.get(i) + " file to zip");
              in = new FileInputStream((String) attachmentsList.get(i));
              // Add ZIP entry to output stream.
  
              out.putNextEntry(new ZipEntry(attachmentsList.get(i).substring(
                      attachmentsList.get(i).indexOf(".") + 1)));
              // Transfer bytes from the file to the ZIP file
              int len = 0;
              while ((len = in.read(buf)) &gt; 0) {
                  out.write(buf, 0, len);
              }
              // Complete the entry               
  
              in.close();             
              out.flush();
              out.closeEntry();
  
  
              System.out.println(" in zipAttachments for:: "
                      + attachmentsList.get(i) + " file is added to zip");                    
          }                       
  
          System.out.println(" in zipAttachments after zipFile creation");            
  
          System.out.println(" in zipAttachments after out.close");
  
           out.close();
  
      } catch (Exception e) {
          System.out.println(" in zipAttachments error" + e);
          e.printStackTrace();
      }   
  
  }
  </code></pre>
  
  <p>exceptions</p>
  
  <pre><code>java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
  at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:207)
  at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:400)
  at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:557)
  at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
  at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
  at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
  at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
  at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
  at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
  at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
  at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  
  
  
  
  javax.servlet.ServletException
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
  at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
  at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
  at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
  at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
  
  
  
  
  Caused by: java.lang.NullPointerException
  at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:168)
  at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:145)
  at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:109)
  at com.sun.facelets.FaceletViewHandler.handleRenderException(FaceletViewHandler.java:677)
  at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:646)
  at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
  at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
  at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
  at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
  </code></pre>