tags:

views:

38

answers:

3

I have a variable which contains the html content, $html and a variable with the pdf content $pdf.

I can create zip files by using $zip->addFile($file,$file); where $file is a file on the disk However, I want to create zip files with content from the variables, without having to write them to disk first. How do I do it?

I am already storing $html and $pdf as-is in the db. Zipping them would help me save some space in my db.

My next question will most probably be how do I unzip contents without unzipping them to disk first, but let's see.

+1  A: 

You could use the MySQL COMPRESS and UNCOMPRESS functions when you store to, and retrieve from, the database.

Mike
+2  A: 

From $zip->addFile($file,$file); I guess you're using the ZipArchive PHP extension. You can create files in the Zip archive by using the method addFromString()

$zip->addFromString('file.html', $html_data);
$zip->addFromString('file.pdf', $pdf_data);

Also see: http://nl3.php.net/manual/en/function.ziparchive-addfromstring.php

Robert Ros
He. Pierre should cleanup his bug/request reports. I saw [this one](http://pecl.php.net/bugs/bug.php?id=16838) laying there and thought it hadn't been implemented yet.
Artefacto
That bugreport describes a different problem. He wants to create the zip archive in memory. As far as I know this is not possible with the ZipArchive extension. (not even with the php://memory en php://temp streams) Also see this SO question: http://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-di
Robert Ros
A: 

Since I am using php, I found this to be the best solution.

$html="<h1>Contains HTML</h1>";

$ziphtml=gzcompress($html,9);

$statement=mysql_query("INSERT into TABLE (field) VALUES ('$ziphtml')",$db);

and then uncompress when you read the field using gzuncompress

http://php.net/manual/en/function.gzcompress.php

abel