title: JSP to serve a zip corrupts the file
body: <p>I'm having a problem trying to serve a zip file in a JSP.  </p>
  
  <p>The zip file is always corrupt after it has finished downloading.  I've tried a few different methods for reading and writing, and none of them seem to do the trick.  </p>
  
  <p>I figure it is probably adding in ascii characters somewhere as the file will open and display all the filenames, but I can't extract any files.</p>
  
  <p>Here's my latest code:</p>
  
  <pre><code>&lt;%@ page import= "java.io.*" %&gt;
  
  &lt;% 
      BufferedReader bufferedReader = null;
  
      String zipLocation = "C:\\zipfile.zip"; 
  
      try
      {
          bufferedReader = new BufferedReader(new FileReader(zipLocation));
          response.setContentType("application/zip");
          response.setHeader( "Content-Disposition", "attachment; filename=zipfile.zip" );
  
          int anInt = 0;
          while((anInt = bufferedReader.read()) != -1)
          {
              out.write(anInt);
          }
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  %&gt;
  </code></pre>
  
  <p>EDIT:
  I moved the code to a servlet and it still didn't work.  I changed a bunch more stuff around, so here's the latest non-working code:</p>
  
  <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
  {
      try
      {
          String templateLocation = Config.getInstance().getString("Site.templateDirectory");
  
          response.setContentType("application/zip");
          response.setHeader("Content-Disposition", "attachment; filename=output.zip;");
  
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          BufferedOutputStream bos = new BufferedOutputStream(baos);
          FileInputStream fis = new FileInputStream(templateLocation);
  
          int len;
          byte[] buf = new byte[1024];
  
          while ((len = fis.read(buf)) &gt; 0)
          {
              bos.write(buf, 0, len);
          }
  
          bos.close();
          PrintWriter pr = response.getWriter();
          pr.write(baos.toString());
          pr.close();
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>This is the servlet code that I actually works.  Thank you to everyone!</p>
  
  <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
  {
      try
      {
          String templateLocation = Config.getInstance().getString("Site.templateDirectory");
  
          response.setContentType("application/zip");
          response.setHeader("Content-Disposition", "attachment; filename=output.zip;");
  
          BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
          FileInputStream fis = new FileInputStream(templateLocation);
  
          int len;
          byte[] buf = new byte[1024];
  
          while ((len = fis.read(buf)) &gt; 0)
          {
              bos.write(buf, 0, len);
          }
  
          bos.close();
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>