Closed lukas9393 closed 4 years ago
Maybe the outputAsString
method is right for you.
If stream is needed, then there is nothing wrong with extending a class for a special case.
$zip = new class extends ZipFile{
/**
* @return resource
*/
public function saveAsMemory()
{
if (!($fp = fopen('php://temp', 'w+b'))) {
throw new \InvalidArgumentException('php://temp cannot open for write.');
}
$this->writeZipToStream($fp);
return $fp;
}
};
$zip->addDir(__DIR__);
$fp = $zip->saveAsMemory();
$zip->close();
rewind($fp);
Hi, I would like to create a zip archive in a memory stream to handle the stream without writing to the local filesystem. Unfortunately my first version does not work as expected:
The code does not run due to the method
saveAsStream
which terminates the stream L1641.I have found two solutions so that I can still use the memory stream: First, I create a Response Object, filled with the zip archive and then extract the
ResponseStream
from this object:Secondly, I overwrite the
protected
ofwriteZipToStream
L1761 and use the function as it is used insaveAsStream
, but without calling `fclose':Both are solutions that work, but are not quite correct. So I wanted to ask if you know a better solution to store a zip archive in a memory stream without writing to the filesystem? Or if not, if there is a possibility to change the visibility of
writeZipToStream
topublic
?