Compress and decompress

n: smd_crunchers | v: 0.11 | f: /

Documentation for the Textpattern plugin smd_crunchers by Stef Dawson follows this short message from our sponsor ;-)

Plugin list buttonPlugin download buttonCompressed plugin download button


Requires PHP5+

Library of compression / decompression algorithms mostly written by other people.

After installation, it enables anyone to employ bi-directional Tar, Zip, Gzip and Bzip2 mechanisms, some of which would ordinarily rely on particular components being compiled into PHP.

While certain core libraries are still required to be compiled into PHP (e.g. zlib), this plugin mitigates the need for any further extensions like PECL / PEAR to offer zip capabilities. In limited server setups or places where users don’t have access to configuration files, this is rather handy.


Function smd_crunch_capabilities( type )
Find out which compression / decompression facilities are available on the current server setup. The type argument can be either:
compress: to determine which compression facilities are available
decompress: to determine which decompression facilities are available
Default: compress
Returns: an array containing zero or more of:
Function smd_mkdir_recursive( path, mode )
Create a directory tree representing path, and set the optional mode permissions (Default: 0755)
Class smd_crunch_archive
Base class for all main archiving / unarchiving facilities, excluding unzip.
Class smd_crunch_tar_file
Specific class for handling (de)compression of .tar files
Class smd_crunch_gzip_file
Specific class for handling (de)compression of.gz and .tgz files
Class smd_crunch_bzip_file
Specific class for handling (de)compression of.bz2 and .tbz2 files
Class smd_crunch_zip_file
Specific class for handling compression files
Class smd_crunch_dUnzip2
Specific class for handling decompression files
Class smd_crunch_dZip
Alternative methods for compressing .zip files

Example 1: compress all files in a folder as zip

But exclude any .svn directories in the process. Then immediately serve the file as a downloadable item

$arc = new smd_crunch_zip_file('');
     'basedir'   => '/path/to/files',
     'overwrite' => 1,
     'inmemory'  => 1

Example 2: decompress gzip archive

This is the same generic procedure for all supported compressed types.

$fullpath = '/path/to/file.gz';
$arc = new smd_crunch_gzip_file(basename($fullpath));
     'basedir'   => '/path/to/export/folder',
     'overwrite' => 1

Example 3: decompress zip archive

$fullpath = '/path/to/';
$zip = new smd_crunch_dUnzip2($fullpath);
$filelist = $zip->getList();
foreach($filelist as $fn => $zippedFile) {
   // Do file size/type validation here?
   echo "$fn ($zippedFile['uncompressed_size'] bytes)<br>";

Author / credits

Stef Dawson.

Libraries from PHP Classes by Devin Doucette and Alexandre Tedeschi. Many thanks to those authors for awesome coding skillz.


  • 29 Jan 2012 | v0.10 | Initial release
  • 29 Jan 2012 | v0.11 | Added smd_mkdir_recursive()

Source code

If you’d rather dive in and out of functions, you’ll need to step into the view source page.

Legacy software

If, for some inexplicable reason, you need a legacy version of a plugin, it can probably be found on the plugin archive page.

Experimental software

If you’re feeling brave, or fancy skateboarding into volcanos, you can test out some of my beta code. It can be found on the plugin beta page.