title: Grails Generating a zip file for the user to download
body: <p>I am a <strong>newbie</strong> to <strong>grails</strong> currently working on a web-app project that works with users uploading pictures. Users can create a "hunt" with a list of prompts. Each "prompt" is the objective for participants (eg: upload pictures of your favorite candy.)  Basically the web-app is a "scavenger hunt" tool for Photographers to socially share their work.</p>
  
  <p>Right now, I am having trouble trying write a function in my user controller to generate a zip file with all of the pictures that the user has uploaded. This is what my controller function looks like at the moment. 
  I used this example I found to start.
  <a href="http://thegrailstrail.blogspot.com/2012/07/generating-zip-files-from-multiple.html" rel="nofollow">Generating a zip file.</a></p>
  
  <pre><code>def downloadAlbum(){
  
      ByteArrayOutputStream baos = new ByteArrayOutputStream()
      ZipOutputStream zipFile = new ZipOutputStream(baos)
  
      //Instance of a user domain class
      def userInstance = User.findByLogin(auth.user())
  
      //pictures are uploaded to a prompt and stored as a photoInstance
      //this line of code gets the actual file stored as byte[]
          photoInstance.myFile = image.getBytes()
  
      //select all photos that belong to the user and store them into a list
      def photoInstanceList = userInstance ? Photo.findAllByMyUser(userInstance) : []
  
  
      //Mapping
      [userInstance: userInstance, photoInstanceList: photoInstanceList]      
  
        photoInstanceList.each {photo -&gt;
          if (photoInstance.myFile != "") {
            File file = new File(photoInstance.myFile)
            zipFile.putNextEntry(new ZipEntry(Photo.title+".jpeg"))
            file.withInputStream { i -&gt;
  
              zipFile &lt;&lt; i
  
            }
            zipFile.closeEntry()
           }
          }
          zipFile.finish()
          response.setHeader("Content-disposition", "filename=\"${login}.zip\"")
          response.contentType = "application/zip"
          response.outputStream &lt;&lt; baos.toByteArray()
          response.outputStream.flush()   
  
  }
  </code></pre>
  
  <p>I then use this code in the User view to generate a link that calls the function in the user controller. Am I close? Is there some missing piece in my code? </p>
  
  <p>By the way, This is the first question I have ever written on Stack Overflow. I appreciate the time that you have taken to read this entry. If anything is not clear enough, please where I can improve this question. I am using grails 2.1.1</p>
  
  <p>Thank you for your time!!</p>