-3

I have this functioning code and i just want to download it as zip instead of its orginal file Here is my code:

def downloadFile() {
    def filePath=ServletContextHolder.servletContext.getRealPath("/") + "Audio/"
    def sub = AudioClips.get(params.id)
    filePath+=sub.fileName
    def file = new File(filePath);
    if (file.exists())
    {
        response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
        response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
        response.outputStream << file.bytes
        redirect(controller: "category",action: "index")
    }
The Anish
  • 19
  • 1
  • 5
  • What have you tried? What isn't working about what you have tried? SO isn't a place to ask people to write code for you. Put forth some effort. Here is an example of creating a zip file to get you started: http://groovy-almanac.org/create-a-zipfile/ – Joshua Moore Nov 07 '14 at 09:46

1 Answers1

1
def downloadAudioZipFile = {
    def filePath = ServletContextHolder.servletContext.getRealPath("/") + "Audio/"
    def sub = AudioClips.get(params.id)
    filePath += sub.fileName
    def file = new File(filePath);
    if (file.exists()) {
        response.setContentType("application/octet-stream")

        response.setHeader("Content-Disposition", "Attachment;Filename=\"${file.name}\".zip")


        ZipOutputStream zip = new ZipOutputStream(response.outputStream);
        def file2Entry = new ZipEntry("${file.name}");
        zip.putNextEntry(file2Entry);
        zip.write(file.bytes)
        zip.close();
    }
}
The Anish
  • 19
  • 1
  • 5