Generate multiple files, add them to a zip and then send the zip as a file download.

coldfusion, coldfusion-10, coldfusion-8, coldfusion-9

Solution

Yes there is: `<cfzip>`. There's a usage example in the docs, but I'll repro here so I don't get slapped for basically saying "RTFM":

<!--- This example shows how to zip the directory "c:\temp" into the ZIP file "e:\work\abc.zip". ---> 
<cfzip file="e:\work\abc.zip" source="c:\temp"> 

<!--- This example shows how to zip all the class files in a directory and add a subdirectory named "classes" to the JAR file entry name. ---> 
<cfzip file="e:\work\util.jar" action="zip" source="c:\src\util\" prefix="classes" filter="*.class"> 

<!---This example shows how to zip all of the log files in the ColdFusion directory and create a subdirectory called exception where zipped files are archived. 
<cfzip file="c:\zipTest\log2.zip" action="zip" source="c:\ColdFusion\" prefix="exception" filter="*.log"> 

<!--- This example shows how to overwrite all of the content of a ZIP file with the entries specified in the source. ---> 
<cfzip file="c:\currentApp.zip" source="c:\myApp\work" overwrite="yes">

Problem

I have a cfm page that generates multiple spreadsheet objects, right now if i try to send the files for download, only the last generated spreadsheet file is send as a download. Is there a way to write these files to a zip and send the zip file as a download? This is my existing logic: ``` for(VARIABLES.fileNumber = 0; VARIABLES.fileNumber < VARIABLES.maxFiles; VARIABLES.fileNumber = VARIABLES.fileNumber + 1) { /* code to create Spreadsheet here */ VARIABLES.context = getPageContext(); VARIABLES.context.setFlushOutput(true); VARIABLES.response = VARIABLES.context.getResponse().getResponse(); VARIABLES.response.reset(); VARIABLES.response.setContentType("application/msexcel"); VARIABLES.response.setContentLength(len(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj))); VARIABLES.response.setHeader("Content-Disposition","attachment;filename=MyComplianceStatusFile#VARIABLES.fileNumber#Of#VARIABLES.maxFiles#.xls"); VARIABLES.out = response.getOutputStream(); VARIABLES.out.write(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj)); VARIABLES.out.flush(); VARIABLES.out.close(); } ``` Now , this way I get only the last spreadsheet that was generated. Is there a way to get all the spreadsheets that are generated may be one by one or may be in a zip ?

Original source