×

CodeIgniter ZIP Encoding Class

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

$this->load->helper(‘text’);

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

$this->zip

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.
$this->zip->compression_level = 5;
  • add_data(): An add_data() function is used to add file contents to a defined single file name.

Syntax

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

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

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Zip_controller extends CI_Controller 
 {
     public function add()
     {   
       echo "<title> Tutorial and Example </title>";
         $this->load->library('zip'); 
         $filename = 'myfile.txt';
         $data = 'Welcome to the Tutorial and example';
         $this->zip->add_data($filename, $data);  /* define filename and the content which     you want to write. */
         if($this->zip->archive('Files/mydata.zip')) // it is an archive function.
         { 
             echo ' Zip file has been created';
         }
         else
        {
             echo 'Failed to create';
         }
     } 
 } 
 ?> 

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:

$file = array(
             ‘myfirst.txt’ => ‘Codeigniter is a PHP framework’,
             ‘mysecond.text’ => ‘Codeigniter is an easy to learn’
             );
 $this->zip->add_data($file);   // it will add the content to their respective file. 
  • add_dir(): An add_dir() function is used to create a directory that contains your data files.

Syntax

add_dir ($directory)

Example:

$this->zip->add_dir(‘folder_name’); // It creates ‘folder_name’ as directory name 
  • 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

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

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

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Zip_controller extends CI_Controller 
 {
 public function read()
     {  
         echo "<title> Tutorial and Example </title>"; 
         $this->load->library('zip');
         $this->load->helper('file');
         $new_path = 'images/Folder/file2.txt';
         $path = 'Files/mydata/myfile.txt';
         if($this->zip->read_file($path, $new_path)) 
         {
             echo "<h4> Your File Content is here... </h4>".read_file($path);
         }
         else
         {
            echo "Can't be Read"; 
         }
     }
 }
 ?> 

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

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

$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:

$source = ‘uploads/directory_name/’;

$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

archive ($filepath)

$filepath: It defines the target zip folder.

Example:

$this->zip->archive( ‘/path/to/archivefile.zip’); /* creates an archive file with a name archivefile.zip */
  • 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

download ($filename = ‘backup.zip’)

$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

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Zip_controller extends CI_Controller 
 {
 public function index()
     {
         $this->load->view('zip_view'); // load a view file 
     }
         public function downloadZip()
         {          
             $this->load->library('zip');
             $path = 'Files/mydata/myfile.txt';
             if($this->zip->read_file($path)) 
             {
              $data = $this->zip->download('compress.zip'); /* it download a zip file with a name compress.zip */
             }
             else
             { 
                echo "File cannot be download";
             }
         }
 }
 ?> 

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

<html>
 <head>
 <title> Tutorial and Example </title>
 </head>
 <body>
 <h2> Example of download a zip file </h2> 
 <a href ="<?php echo base_url('index.php/Zip_controller/downloadZip'); ?>" > Click here </a> to download a Zip file.
 </body>
 </html> 

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:

$filename = ‘my_file.txt’;
$data = ‘Welcome to the world’;
$this->zip->add_data ($filename, $data);
$zip_file = $this->zip->get_zip(); 

Related Topics

CodeIgniter ZIP Encoding Class

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...

5 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.