ZIP Encoding Class

The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or saved to a defined directory.

Loading a Zip Encoding Class

Like most other library classes in CodeIgniter application you must load a Zip class in the controller file such as:

Syntax

Once the class has been loaded, you can use the zip library object as shown:

Class reference

There are various class references available in the Zip class:

  1.  $compression_level: As the name suggests, $compression_level is used to compress it to what level to reduce their size. Its range starts from 0 to 9, where 0 disable the compression, and 9 means highly compression of the file.
  • add_data(): An add_data() function is used to add file contents to a defined single file name.

Syntax

It has two parameters:

$filepath: It defines the file path or file name that contain your file contents.

$data: In this parameter, you can define the file contents that you want to store in the defined filename.

Example: Create a Zip_controller.php controller file and save it to the following path application/controller/Zip_controller.php folder. After that, write the following program in the controller file.

Zip_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Zip_controller/add function; it shows the output,as shown below.

ZIP Encoding Class

If you verify that your zip file has been created in your application, follow this path:

C:\wamp64\www\CodeIgniter-3.1.11\Files, and it shows a zip file with a name ‘mydata’ in the defined folder.

Alternatively, if you want to add multiple files, you can use an array where the first parameter should contain file => content and the second parameter canbe ignored, as shown below:

  • add_dir(): An add_dir() function is used to create a directory that contains your data files.

Syntax

Example:

  • read_file(): As the name suggests, a read_file() function enables you to compress a file that already exists on a defined folder, so you have to pass a file path, the zip class will read a defined file and add it to archive.

Syntax

It has two parameters:

$path: It contains the file path, where the Zip file is stored.

$archive_filepath: It defines whether you want to maintain the original file path (using Boolean) or create a new file name/path (in string).

Example: Create a Zip_controller.php file to the following path application/controller/Zip_controller.php folder. After that, write the following program in the controller file.

Zip_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Zip_controller/add function; it shows the output,as shown below.

ZIP Encoding Class
  • read_dir(): This function is similar to read_dir () except that it is used to compress a directory that already exists on your application. This requires a directory path, which the zip class can read and recreate it as a zip archive.

Syntax

$path: It define the path to directory.

$preserve_filepath (bool): It defines whether you want to maintain the original path.

$root_path: It defines which part of the directory path to exclude from the archive directory.

Example:

$this->zip->read_dir($source);  // read a directory name

$this->zip->download(‘myfile.zip’); /* download a zip file to your desktop as myfile.zip. */

  • archive(): An arhive() function is used to create a Zip-encoded file from a target directory ending with the file name.

Syntax

$filepath: It defines the target zip folder.

Example:

  • download(): As the name suggests, a download () function is used to download the zip file from the server. For this, you have to provide a name in the function, which you call a zip file.

Syntax

$filename: You can pass an archive file name.

Example: Create a Zip_controller.php file to the following path application/controller/Zip_controller.php folder. After that, write the following program in the controller file.

Zip_controller.php

Create a zip_view.php file to the following path application/views/zip_view.php folder. After that, write the following program in the view file.

Zip_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Zip_controller; it shows the output,as shown below.

ZIP Encoding Class

Click on the click here link, it will download a Zip file from your server, as shown in below image.

ZIP Encoding Class
  • get_zip(): A get_zip() function is used to return the Zip compressed file data.

Example:

Pin It on Pinterest

Share This