What compression method to use in PHP?

I have a large amount of data to move using two PHP scripts: one on the client side using the command line PHP script and the other behind Apache. I send data to the server and use the php: // input stream to save it to the web server. In order not to reach the limits of memory, the data is divided into pieces of 500 kB for each POST request. It all works great.

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

Which pair of features would you recommend and why?

UPDATE: I just read the zlib FAQ:

The gzip ( gzencode ) format was designed to store directory information for a single file, such as the name and date of the last change. The zlib ( gzcompress ) format, on the other hand, was developed for applications in memory and communication channels and has a much more compact header and trailer and uses a faster integrity check than gzip.

+50
php gzip compression
Mar 07 '09 at 15:17
source share
3 answers

All of this can be used. There are subtle differences between the three:

  • gzencode () uses the gzip file format, the same as the gzip command line tool. This file format contains a header containing additional metadata, compressed DEFLATE data, and a footer containing the CRC32 checksum and length check.
  • gzcompress () uses the ZLIB format. It has a shorter header, which serves only to determine the compression format, the compressed DEFLATE data and the footer containing the ADLER32 checksum.
  • gzdeflate () uses its own DEFLATE algorithm on its own, which is the basis for both formats.

All three use the same algorithm under the hood. gzencode() adds the ability to include the original file name and other environmental data (this is not used for line compression). gzencode() and gzcompress() both add a checksum, so you can check the integrity of the archive, which can be useful for unreliable transfer and storage methods. If everything is stored locally and you do not need additional metadata, then gzdeflate() will be enough. For portability, I would recommend gzencode() (GZIP format), which is probably better supported than gzcompress() (ZLIB format) among other tools.

+60
Mar 07 '09 at 15:24
source share

I am not a PHP expert and cannot answer the question, but it seems that there is a lot of guessing and fuzzy information is offered.

DEFLATE is the name of the compression algorithm used by ZLIB, GZIP and others. Theoretically, GZIP supports alternative compression algorithms, but in practice they are not.

There is no such thing as a "gzip algorithm". GZIP uses the DEFLATE algorithm and places the crop data around the compressed data. With gzip you can add things like file name, file time, CRC, even comment. However, this metadata is optional, and many gzippers simply omit it.

ZLIB is similar, with the exception of another, more limited set of metadata and a specific 2-byte header.

This is all in the IETF RFC channels 1950 , 1951 and 1952 .

To say that "the gzip algorithm is compressed better than DEFLATE" is simply absurd. There is no gzip algorithm. And the algorithm used in the GZIP format is DEFLATE .

+39
Mar 07 '09 at 16:13
source share

All methods are essentially the same, the difference between them is mainly in the headers. I personally would use gzencode, this will produce output that is equal to calling the command line in the gzip utility.

+4
Mar 07 '09 at 15:27
source share



All Articles