Search code examples
phpzipcompressionarchive

List of compression formats that are supported by PHP?


I am writing a web application to decompress archives using PHP.
Out of the general archive formats, which of them are supported by PHP officially?

Can you give me a short introduction of each?


Solution

  • As stated in PHP's own documentation, several extensions are available:

    • Bzip2 - Known for being free and patent-free. Bzip2 is considerably better than Deflate (.gz) in compression, but not as good as LZMA (generally .7z and .xz). It also promises to be faster in both compression and decompression than similar formats.
    • LZF - Aims for little memory usage during decompression. It's very fast, but not so good in compression. Still better than no compression at all.
    • Phar - "PHP Archive" is a format developed by the PHP team to quickly deploy PHP applications. Phar can group multiple php files into one, and is easy to work with.
    • RAR - Closed-source compression and archiving tool, which is very common and also really efficient at compression. Due to the licensing, creating RAR archives is not directly supported, however it is not impossible.
    • ZIP - Probably the most known compression format. For more information, check out the Wikipedia page.
    • GZ - GZip uses the Deflate format by default, and it is very easy to use in PHP. Commonly used to save bandwidth while serving text-based files.

    Some of these methods can only compress a single file. To decompress the content of multiple files, you'll probably need to untar them first.

    Just remember to have the corresponding extensions enabled on your web server.

    There are several more formats that can be used in conjunction with php, but either they aren't supported by an official extension, or you have to execute shell commands to extract them. (Fair Warning: Some web hosting providers disable this functionality.)