Mecha CMS

Mecha CMS blog and documentation.

Class Package

Updated: Sunday, 07 August 2016

File packer and package extractor.

Info: This class requires Zip extension to be installed. Most web hosting providers already have this extension installed, so you don’t need to do anything.

Added since version 1.0.3.

Packing

Basic Packing

Take a folder, then create a package for that folder, then save it in the same directory where the folder is located:

Package::take('path/to/directory')->pack();

The code above will generate a ZIP file named as directory.zip

Take a file, then create a package for that file, then save it in the same directory where the file is located:

Package::take('path/to/file-1.txt')->pack();

The code above will generate a ZIP file named as file-1.zip

Specify the Name of the ZIP File

Take a folder, then create a package for that folder, then save it in the same directory where the folder is located with a specific name:

Package::take('path/to/directory')->pack('package.zip');

The code above will generate a ZIP file named as package.zip

Pack and Move

Pack it, then move it to another location:

Package::take('path/to/directory')->pack('path/to/another-directory');

The code above will generate a ZIP file named as directory.zip, saved in path\to\another-directory.

Package::take('path/to/directory')->pack('path/to/another-directory/my-package.zip');

The code above will generate a ZIP file named as my-package.zip, saved in path\to\another-directory.#### Add a Root Folder to the Package

By default, this class will create a package from a folder without including the folder itself. You can ensure that the folder can be included by determining the second parameter as true (or as a folder name if you want to name it manually):

We will create a package for these files:

foo/bar/
 ├── file-1.txt
 ├── file-2.txt
 ├── directory-1/
 │   ├── file-3.txt
 │   └── file-4.txt
 └── file-5.txt
Package::take('foo/bar')->pack('package-1.zip');
Package::take('foo/bar')->pack('package-2.zip', true);
Package::take('foo/bar')->pack('package-3.zip', 'baz');
Package::take('foo/bar')->pack('package-4.zip', 'baz/aaa');

Results for package-1.zip:

package-1.zip
 ├── file-1.txt
 ├── file-2.txt
 ├── directory-1/
 │   ├── file-3.txt
 │   └── file-4.txt
 └── file-5.txt

Results for package-2.zip:

package-2.zip
 └── bar/
     ├── file-1.txt
     ├── file-2.txt
     ├── directory-1/
     │   ├── file-3.txt
     │   └── file-4.txt
     └── file-5.txt

Results for package-3.zip:

package-3.zip
 └── baz/
     ├── file-1.txt
     ├── file-2.txt
     ├── directory-1/
     │   ├── file-3.txt
     │   └── file-4.txt
     └── file-5.txt

Results for package-4.zip:

package-4.zip
 └── baz/
     └── aaa/
         ├── file-1.txt
         ├── file-2.txt
         ├── directory-1/
         │   ├── file-3.txt
         │   └── file-4.txt
         └── file-5.txt

Creating a Package from an Array of Files

Create an array of files: File location in the directory as the array key, file path in the ZIP file as the array value.

// A: Location in the directory
// B: Destination in the ZIP file

Package::take(array(
    A => B,
    'file-1.txt' => 'foo/file-1.txt',
    'dir/file-2.txt' => 'bar/file-2.txt',
    'dir/dir/file-3.txt' => 'bar/file-3.txt',
    'dir/file-4.txt' => 'file-4.txt'
))->pack();

Package::take(array(
    A => B,
    'file-1.txt' => 'foo/file-1.txt',
    'dir/file-2.txt' => 'bar/file-2.txt',
    'dir/dir/file-3.txt' => 'bar/file-3.txt',
    'dir/file-4.txt' => 'file-4.txt'
))->pack('package.zip');

Results:

file-1.zip
 ├── foo/
 │   └── file-1.txt
 ├── bar/
 │   ├── file-2.txt
 │   └── file-3.txt
 └── file-4.txt

package.zip
 ├── foo/
 │   └── file-1.txt
 ├── bar/
 │   ├── file-2.txt
 │   └── file-3.txt
 └── file-4.txt

Extracting

Basic Extracting

Take a ZIP file, extract it, then put the results into the same directory where the ZIP file is located:

Package::take('package.zip')->extract();

Take a ZIP file, extract it, then put the results into the same directory where the ZIP file is located, wrapped in a folder named as my-data:

Package::take('package.zip')->extract('my-data');

Extract and Move

Extract it, then move it to another location:

Package::take('package.zip')->extractTo('path/to/another-directory');

Extract it, then move it to another location and wrap the results in a folder named as my-data:

Package::take('package.zip')->extractTo('path/to/another-directory', 'my-data');

Adding and Removing

Add a File to the Package

Add a file to the package without extraction process:

// A: Location in the directory
// B: Destination in the ZIP file

Package::take('file.zip')->addFile(A, B);

Add Multiple Files to the Package

Add multiple files to the package without extraction process:

// A: Location in the directory
// B: Destination in the ZIP file

Package::take('file.zip')->addFiles(array(
    A => B,
    A => B,
    A => B
));

Remove a File from the Package

Remove a file from the package without extraction process:

Package::take('file.zip')->deleteFile('file-1.txt');

Remove Multiple Files from the Package

Remove multiple files from the package without extraction process:

Package::take('file.zip')->deleteFiles(array(
    'file-1.txt',
    'file-2.txt',
    'file-3.txt'
));

Note: You cannot create an empty package. There will always be at least one file in the package even if you have tried to remove all.

Remove a Folder from the Package

Added since version 1.2.0.

Remove a folder with its contents from the package without extraction process:

Package::take('file.zip')->deleteFolder('foo');

Remove Multiple Folders from the Package

Remove multiple folders with its contents from the package without extraction process:

Package::take('file.zip')->deleteFolders(array(
    'foo',
    'bar',
    'baz'
));

Renaming

Rename a File in the Package

Added since version 1.1.0.

Rename a file in the package without extraction process:

Package::take('file.zip')->renameFile('old-name.txt', 'new-name.txt');

Rename Multiple Files in the Package

Added since version 1.1.0.

Rename multiple files in the package without extraction process:

Package::take('file.zip')->renameFiles(array(
    'file-1.txt' => 'file-1.php',
    'file-2.txt' => 'file-2.php'
));

Get Contents of a File

Get contents of a file in the package without extraction process:

echo Package::take('file.zip')->read('file-1.txt');

Inspecting

Get info of a ZIP file.

$test_1 = Package::take('file.zip')->inspect();
$test_2 = Package::take('file.zip')->inspect('size');
$test_3 = Package::take('file.zip')->inspect('files.0.name');

Results for $test_1:

$test_1
 │
 ├── …
 ├── …
 ├── status: 0
 ├── total: 2
 └── files:
     ├── 0:
     │   ├── name: file-1.txt
     │   ├── index: 0
     │   ├── crc: 292843554
     │   ├── size: 1002
     │   ├── mtime: 1400903642
     │   ├── comp_size: 528
     │   └── comp_method: 8
     ├── 1: …
     ├── 2: …
     └── 3: …
Donation and Email Subscription