Mecha CMS

Add extra functionality to the core engine.

Package

Table of Contents
  1. Pack 
  2. Extract 
  3. Read 
  4. Add 
  5. Remove 

File packer and package extractor.

Download Version 2.1.1 Download Development Version

Something looks broken? We accept bounties to fix them faster.

This extension provides various API to work with ZIP files. ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. ZIP files generally use the file extensions .zip and the MIME media type application/zip.

Pack 

Create package of a folder with its entries:

$folder = new Folder('.\path\to\folder');
$folder->exist && $folder->pack('.\path\to\package.zip');

Create package of a file:

$file = new File('.\path\to\file.txt');
$file->exist && $file->pack('.\path\to\package.zip');

Create package of files:

$package = new Package('.\path\to\package.zip');

$package->paste('.\path\to\file-1.txt', 'file-1.txt');
$package->paste('.\path\to\file-2.txt', 'file-2.txt');
$package->paste('.\path\to\file-3.txt', 'foo\bar\file-3.txt');

Extract 

Extract package to the current folder:

$package->extract();

Extract all files and folders from the package to a folder:

$package->extract('.\path\to\folder');

Extract some files from the package to a folder:

$package->extract('.\path\to\folder', [
    'file-1.txt',
    'file-2.txt',
    'foo\bar\file-3.txt'
]);

Extract a file from the package to a folder:

$package->extract('.\path\to\folder', 'file-1.txt');

Read 

List files and folders from the package:

// Prepend root path to the results?
$package->root = true;

$A = $package->get(); // Get list of files and folders of the root level
$B = $package->get(null, true); // Get list of files and folders recursively
$C = $package->get(null, 2); // Get list of files and folders with maximum nesting level of 2
$D = $package->get(1, true); // Get list of files only, recursively
$E = $package->get(0, true); // Get list of folders only, recursively
$F = $package->get('gif,jpeg,jpg,png', true); // Get list of image files only, recursively

Stream list of files and folders of the package:

foreach ($package as $path => $content) { … }

Count total files stored in the package:

echo count($package);

Get content of a file stored in the package:

$content = $package->content('file-1.txt');

Check the existence of a file stored in the package:

if ($package->has('file-1.txt')) { … }

Add 

Add new file to the package:

$package->set('file-1.txt', 'Content of `file-1.txt` goes here.');
$package->set('file-2.txt', 'Content of `file-2.txt` goes here.');
$package->set('foo\bar\file-3.txt', 'Content of `file-3.txt` goes here.');

Add new folder to the package:

$package->set('foo', []);
$package->set('bar\baz\qux', []);

Add copy of files to the package:

$package->paste('.\path\to\file-1.txt', 'file-1.txt');
$package->paste('.\path\to\file-2.txt', 'file-2.txt');
$package->paste('.\path\to\file-3.txt', 'foo\bar\file-3.txt');

Remove 

Remove the package:

$package->let();

Remove files from the package:

$package->let([
    'file-1.txt',
    'file-2.txt',
    'foo\bar\file-3.txt'
]);

Remove a file from the package:

$package->let('file-1.txt');