tags:

views:

432

answers:

3

I'm writing a PHP script that generates gzipped files. The approach I've been using is to build up a string in PHP and gzcompress() the string before writing it out to a file at the end of the script.

Now I'm testing my script with larger files and running into memory allocation errors. It seems that the result string is becoming too large to hold in memory at one time.

To solve this I've tried to use gzopen() and gzwrite() to avoid allocating a large string in PHP. However, the gzipped file generated with gzwrite() is very different from when I use gzcompress(). I've experimented with different zip levels but it doesn't help. I've also tried using gzdeflate() and end up with the same results as gzwrite(), but still not similar to gzcompress(). It's not just the first two bytes (zlib header) that are different, it's the entire file.

What does gzcompress() do differently from these other gzip functions in PHP? Is there a way I can emulate the results of gzcompress() while incrementally producing the result?

A: 

I ran into a similar problem once - basically there wasn't enough ram allocated to php to do the business.

I ended up saving the string as a text file, then using exec() to gzip the file using the filesystem. Its not an ideal solution but it worked for my situation.

ae
I may end up doing this too but it's so unfortunate. I develop on Windows but my distribution platform will be Linux, which adds levels of annoyances.
Kai
A: 

try increasing the memory_limit parameter in your php.ini file

ghostdog74
A: 

I am not 100% certain, but my guess is that gzcompress uses GZIP format, and gzopen/gzwrite use ZLIB. Honestly, I can't tell you what the difference between the two is, but I do know that GZIP uses ZLIB for the actual compression.

It is possible that none of that will matter though. Try creating a gzip file with gzopen/gzwrite and then decompress it using the command-line gzip program. If it works, then using gzopen/gzwrite will work for you.

shadowhand
You're right about the different formats, but I think you have it backwards. From what I can tell, gzopen/gzwrite uses GZIP and gzcompress/decompress uses ZLIB. Also I think that that GZIP is a container format for ZLIB compressed data. Sadly, I have a file stored with raw ZLIB compressed data, so there's not really an efficient solution for me (in PHP).
Kai