Section: Generating a ZIP file
« Sending a binary file | ^ Responses: generating content | Generating a user interface in XUL » |
jResponseZip can generate zip file and send it for download to the client. its alias is "zip":
$rep = $this->getResponse('zip');
You can now indicate $zipFilename property. it will be the zip name proposed to the user for download. Note that it may not be an existing file on the server hard disk.
$rep->zipFilename='myCrazyPackage.zip';
Then, you have to create the zip. $content property is a jZipCreator creator. All its methods can be used to add content to the zip result. See reference documentation of jZipCreator.
// add data/truc.txt (stored on the server) content to zip,
// and alias it with machin.txt name.
$rep->content->addFile(JELIX_VAR_PATH.'data/truc.txt', 'machin.txt');
// add whole "examples" directory content to zip
$rep->content->addDir(JELIX_VAR_PATH.'examples/', 'examples', true);
// create a file into zip, given a name and some content
$rep->addContentFile('dynamic.txt', 'generated content as we like');
And you're done :-)
The user will see a dialog asking him to save or open myCrazyPackage.zip, containing a machin.txt file, an examples directory, and a dynamic.txt file.