-->

php zlib: How to dynamically create an in-memory z

2020-07-27 01:55发布

问题:

this is what I need

$a=array('folder'=>'anyfolder','filename'=>'anyfilename','filedata'=>'anyfiledata');

I need to create a variable $zip with compressed data from $a and output this $zip into browser window with

header('Content-type: application/octetstream');
echo $zip;

All libs that I found, they pack files and create zip files physically on disk. I need to do it dynamically, using memory. Is there any examples of how to do it using pure php's zlib and without studying the zip format specifications?


UPD:
could it be done with CreateZIP class?

UPUPD:
Yes. it could =)

回答1:

Yes, CreateZIP class does it.

require_once('CreateZipFile.php');
$zip = new CreateZipFile;
$zip->addFile('anyfiledata', 'anyfilename');
$zip->addFile('anyfiledata2', 'anyfolder/anyfilename.anyext');
$zip->addDirectory('anyemptydirwouldbecreatedinthiszipfile');
header('Content-disposition: attachment; filename='.'zipfile.zip'.'');
header('Content-type: application/octetstream');
echo $zip->getZippedfile();
die();

great thanx to Er. Rochak Chauhan