Documentation for the Textpattern plugin smd_crunchers by Stef Dawson follows this short message from our sponsor ;-)
If you like my code and it soothes an otherwise unscratchable itch, feel free to show your appreciation with something from my UK Amazon wish list (or US) or donate to the Stef Dawson community coding pot, either via paypal.me/stefdawson or by following the Donate button below to PayPal. Thanks!
smd_crunchers
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.
Usage
- 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 availabledecompress
: to determine which decompression facilities are available
- Default:
compress
- Returns: an array containing zero or more of:
tar
zip
gzip
bzip2
- Function
smd_mkdir_recursive( path, mode )
- Create a directory tree representing
path
, and set the optionalmode
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 of.zip files
- Class smd_crunch_dUnzip2
- Specific class for handling decompression of.zip 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 file.zip
.
$arc = new smd_crunch_zip_file('file.zip');
$arc->set_options(array(
'basedir' => '/path/to/files',
'overwrite' => 1,
'inmemory' => 1
));
$arc->add_files('/path/to/files/*.*');
$arc->exclude_files('/path/to/files/*.svn*');
$arc->create_archive();
$arc->download_file();
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));
$arc->set_options(array(
'basedir' => '/path/to/export/folder',
'overwrite' => 1
));
$arc->extract_files();
Example 3: decompress zip archive
$fullpath = '/path/to/file.zip';
$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>";
}
$zip->unzipAll('/path/to/export/folder');
$zip->close();
Author / credits
Libraries from PHP Classes by Devin Doucette and Alexandre Tedeschi. Many thanks to those authors for awesome coding skillz.
Changelog
- 29 Jan 2012 | v0.10 | Initial release
- 29 Jan 2012 | v0.11 | Added
smd_mkdir_recursive()
Source code
If you’d rather spend time with the bytes, 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 trying it without protection, you can test out some of my beta code. It can be found on the plugin beta page.