Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php zlib: How to dynamically create an in-memory zip files from string variables?

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 =)

like image 307
el Dude Avatar asked Sep 07 '25 09:09

el Dude


1 Answers

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

like image 115
el Dude Avatar answered Sep 10 '25 17:09

el Dude