Zip Encoding Class — CodeIgniter 3.1.11 documentation

# Zip Encoding Class

CodeIgniter’s Zip Encoding Class permits you to create Zip archives. Archives can be downloaded to your desktop or saved to a directory.

- Using the Zip Encoding Class
  - Initializing the Class
  - Usage Example
- Class Reference

## Using the Zip Encoding Class

### Initializing the Class

Like most other classes in CodeIgniter, the Zip class is initialized in your controller using the $this->load->library function:

    $this->load->library('zip');

Once loaded, the Zip library object will be available using:

    $this->zip

### Usage Example

This example demonstrates how to compress a file, save it to a folder on your server, and download it to your desktop.

    $name = 'mydata1.txt';
    $data = 'A Data String!';

    $this->zip->add_data($name, $data);

    // Write the zip file to a folder on your server. Name it "my_backup.zip"
    $this->zip->archive('/path/to/directory/my_backup.zip');

    // Download the file to your desktop. Name it "my_backup.zip"
    $this->zip->download('my_backup.zip');

## Class Reference

class `CI_Zip`
  `$compression_level = 2`

    The compression level to use.

    It can range from 0 to 9, with 9 being the highest and 0 effectively disabling compression:

        $this->zip->compression_level = 0;

  `add_data`($filepath[, $data = NULL])

    Parameters: | $filepath (mixed) – A single file path or an array of file => data pairs $data (array) – File contents (ignored if $filepath is an array)
    Return type: | void

    Adds data to the Zip archive. Can work both in single and multiple files mode.

    When adding a single file, the first parameter must contain the name you would like given to the file and the second must contain the file contents:

        $name = 'mydata1.txt';
        $data = 'A Data String!';
        $this->zip->add_data($name, $data);

        $name = 'mydata2.txt';
        $data = 'Another Data String!';
        $this->zip->add_data($name, $data);

    When adding multiple files, the first parameter must contain file => contents pairs and the second parameter is ignored:

        $data = array(
                'mydata1.txt' => 'A Data String!',
                'mydata2.txt' => 'Another Data String!'
        );

        $this->zip->add_data($data);

    If you would like your compressed data organized into sub-directories, simply include the path as part of the filename(s):

        $name = 'personal/my_bio.txt';
        $data = 'I was born in an elevator...';

        $this->zip->add_data($name, $data);

    The above example will place my_bio.txt inside a folder called personal.

  `add_dir`($directory)

    Parameters: | $directory (mixed) – Directory name string or an array of multiple directories
    Return type: | void

    Permits you to add a directory. Usually this method is unnecessary since you can place your data into directories when using `$this->zip->add_data()`, but if you would like to create an empty directory you can do so:

        $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"

  `read_file`($path[, $archive_filepath = FALSE])

    Parameters: | $path (string) – Path to file $archive_filepath (mixed) – New file name/path (string) or (boolean) whether to maintain the original filepath
    Returns: | TRUE on success, FALSE on failure
    Return type: | bool

    Permits you to compress a file that already exists somewhere on your server. Supply a file path and the zip class will read it and add it to the archive:

        $path = '/path/to/photo.jpg';

        $this->zip->read_file($path);

        // Download the file to your desktop. Name it "my_backup.zip"
        $this->zip->download('my_backup.zip');

    If you would like the Zip archive to maintain the directory structure of the file in it, pass TRUE (boolean) in the second parameter. Example:

        $path = '/path/to/photo.jpg';

        $this->zip->read_file($path, TRUE);

        // Download the file to your desktop. Name it "my_backup.zip"
        $this->zip->download('my_backup.zip');

    In the above example, photo.jpg will be placed into the path/to/ directory.

    You can also specify a new name (path included) for the added file on the fly:

        $path = '/path/to/photo.jpg';
        $new_path = '/new/path/some_photo.jpg';

        $this->zip->read_file($path, $new_path);

        // Download ZIP archive containing /new/path/some_photo.jpg
        $this->zip->download('my_archive.zip');

  `read_dir`($path[, $preserve_filepath = TRUE[, $root_path = NULL]])

    Parameters: | $path (string) – Path to directory $preserve_filepath (bool) – Whether to maintain the original path $root_path (string) – Part of the path to exclude from the archive directory
    Returns: | TRUE on success, FALSE on failure
    Return type: | bool

    Permits you to compress a directory (and its contents) that already exists somewhere on your server. Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive. All files contained within the supplied path will be encoded, as will any sub-directories contained within it. Example:

        $path = '/path/to/your/directory/';

        $this->zip->read_dir($path);

        // Download the file to your desktop. Name it "my_backup.zip"
        $this->zip->download('my_backup.zip');

    By default the Zip archive will place all directories listed in the first parameter inside the zip. If you want the tree preceding the target directory to be ignored, you can pass FALSE (boolean) in the second parameter. Example:

        $path = '/path/to/your/directory/';

        $this->zip->read_dir($path, FALSE);

    This will create a ZIP with a directory named “directory” inside, then all sub-directories stored correctly inside that, but will not include the /path/to/your part of the path.

  `archive`($filepath)

    Parameters: | $filepath (string) – Path to target zip archive
    Returns: | TRUE on success, FALSE on failure
    Return type: | bool

    Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. Make sure the directory is writable (755 is usually OK). Example:

        $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip

  `download`($filename = 'backup.zip')

    Parameters: | $filename (string) – Archive file name
    Return type: | void

    Causes the Zip file to be downloaded from your server. You must pass the name you would like the zip file called. Example:

        $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"

    Note

    Do not display any data in the controller in which you call this method since it sends various server headers that cause the download to happen and the file to be treated as binary.

  `get_zip`()

    Returns: | Zip file content
    Return type: | string

    Returns the Zip-compressed file data. Generally you will not need this method unless you want to do something unique with the data. Example:

        $name = 'my_bio.txt';
        $data = 'I was born in an elevator...';

        $this->zip->add_data($name, $data);

        $zip_file = $this->zip->get_zip();

  `clear_data`()

    Return type: | void

    The Zip class caches your zip data so that it doesn’t need to recompile the Zip archive for each method you use above. If, however, you need to create multiple Zip archives, each with different data, you can clear the cache between calls. Example:

        $name = 'my_bio.txt';
        $data = 'I was born in an elevator...';

        $this->zip->add_data($name, $data);
        $zip_file = $this->zip->get_zip();

        $this->zip->clear_data();

        $name = 'photo.jpg';
        $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents

        $this->zip->download('myphotos.zip');