views:

614

answers:

4

I have a large amount of data to move using two PHP scripts: one on the client side using a command line PHP script and other behind Apache. I POST the data to the server side and use php://input stream to save it on the web-server end. To prevent from reaching any memory limits, data is separated into 500kB chunks for each POST request. All this works fine.

Now, to save the bandwidth and speed it up, I want to compress the data before sending and decompress when received on the other end. I found 3 pairs of functions that can do the job, but I cannot decide which one to use:

  • gzencode / gzdecode
  • gzdeflate / gzinflate
  • gzcompress / gzdecompress

Which pair of functions would you recommend and why?

UPDATE: I just read zlib FAQ:

The gzip format was designed to retain the directory information about a single file, such as the name and last modification date. The zlib format on the other hand was designed for in-memory and communication channel applications, and has a much more compact header and trailer and uses a faster integrity check than gzip.

+5  A: 

All of these are usable. I would use gzencode() because it is self-contained; its output is the same as if you had used the gzip command line tool.

There is not really much difference between the three.

  • gzencode() uses the fully self-contained gzip format, same as the gzip command line tool
  • gzcompress() uses the raw ZLIB format. It is similar to gzencode but without header data, etc. I think it was intended for streaming.
  • gzdeflate() uses the raw DEFLATE algorithm on its own, which is the basis for both the other formats.
thomasrutter
Almost correct. I investigated a little bit, and it seems gzencode is not wihtout any header data - it just has different header data.
Milan Babuškov
@Milan I guess you meant "gzcompress is not without any header data - it just has different header data".
thomasrutter
+1  A: 

Which to use depends on whether CPU time is important or not.

AIUI, gzdeflate is faster, but doesn't compress as well as gzencode.

Alnitak
+2  A: 

All methods are essentially the same, the difference between them is mostly in the headers. personally I'd use gzencode, this will produce output which is equal to a commandline invocation to the gzip utility.

Jan Jungnickel
+3  A: 

I am no PHP expert and cannot answer the question posed, but it seems like there is a lot of guessing going on here, and fuzzy information being proffered.

DEFLATE is the name of the compression algorithm that is used by ZLIB, GZIP and others. In theory, GZIP supports alternative compression algorithms, but in practice, there are none.

There is no such thing as "the GZIP algorithm". GZIP uses the DEFLATE algorithm, and puts framing data around the compressed data. With GZIP you can add things like the filename, the time of the file, a CRC, even a comment. This metadata is optional, though, and many gzippers just omit it.

ZLIB is similar, except with a different, more limited set of metadata, and a specific 2-byte header.

This is all in IETF RFCs 1950, 1951, and 1952.

To say that "the gzip algorithm compresses better than DEFLATE" is just nonsense. There is no gzip algorithm. And the algorithm used in the GZIP format is DEFLATE.

Cheeso