Grails Cookbook - A collection of tutorials and examples

Grails Example Application - Download / Export ZIP File

This example application will show how to export ZIP file in a Grails application. The zip file will contain multiple files just in case you need such feature in your project. The output of the finished application will look like this:

A simple page with download link is shown:
w0
On click, a download confirmation will be shown instead of the browser trying to render the content:
w1
The zip file contains two files:
w2
The first file is just a plain text file:
w3

And also the second file:
w4

Render the download page

The controller and view code to display the download page should be simple:

SampleZipController:

package asia.grails.zip
class SampleZipController {
    def index() { }
}

index.gsp:

<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main"/>
    <title>Simple Zip</title>
</head>
<body>
<g:link action="downloadSampleZip">Download Sample Zip</g:link>
</body>
</html>

Download ZIP Code

The complete code that contains the logic to generate the ZIP file is this:

package asia.grails.zip
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
class SampleZipController {
    def index() { }
    def downloadSampleZip() {
        response.setContentType('APPLICATION/OCTET-STREAM')
        response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"')
        ZipOutputStream zip = new ZipOutputStream(response.outputStream);
        def file1Entry = new ZipEntry('first_file.txt');
        zip.putNextEntry(file1Entry);
        zip.write("This is the content of the first file".bytes);
        def file2Entry = new ZipEntry('second_file.txt');
        zip.putNextEntry(file2Entry);
        zip.write("This is the content of the second file".bytes);
        zip.close();
    }
}

Tell the browser that your output is binary and the filename for download:

response.setContentType('APPLICATION/OCTET-STREAM')
response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"')

Creating a ZIP file for download is easy. Just instantiate ZipOutputStream passing response.outputStream:

ZipOutputStream zip = new ZipOutputStream(response.outputStream);

Creating a file inside the zip is accomplished by instantiating a ZipEntry and put it inside the ZIP file. You just provide the contents of the file by providing it's binary:

def file1Entry = new ZipEntry('first_file.txt')
zip.putNextEntry(file1Entry)
zip.write("This is the content of the first file".bytes)

Lastly, cloze the ZIP:

zip.close();

Remarks

The above code is an example on how to create a ZIP file on the fly and send to the user's browser. Based from this, it is easy to modify the sample to put different types of files inside the output ZIP.
The full source code for this example can be viewed here or can be downloaded here.

List of Example Applications
  • Simple Ajax Chat
  • Simple Document Management System
  • Save Documents To Database
  • Download / Export Excel File
  • Download / Export ZIP File
  • Grails Forum Application

  • Tags: download, example, zip