×

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

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

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

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.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

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

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 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 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 File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

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.