create archive file in memory with zipArchive class

[email protected] (Ryan Sun)
Newsgroups php.general
Message-ID <[email protected]>
I want to generate credential zip file for user on the fly with
zipArchive and render it for download, so I created following code
---------------------------------------------------------
$zip = new ZipArchive();
$filename = '/tmp/xxx.zip';
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
     throw new Exception();
}
if($zip)
{
    $zip->addFromString('xxx.xx', $fileString);
}
$zip->close();
$fileString = file_get_contents($filename);
unlink($filename);

$this->getResponse()->setHeader('Content-Type', 'application/zip');
$this->getResponse()->setHeader('Content-Disposition','attachment;filename=xxx.zip');
$this->getResponse()->setBody($fileString);
-----------------------------------------------------
it works, but I think creating the file in memory is a better
approach, so I changed the 2nd lineI(using php 5.2.0) to
$filename = 'php://temp/xxx.zip';
then the php just won't archive the file and the file downloaded is
just a plain text file.

so
question 1, how to create zip Archive file in memory on the fly and
download it (I don't have to save it on disk)?
question 2, if there is no way to create in memory, is it safe to just
unlink() the file?

thanks in advance.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.