×

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

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

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

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.

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.

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 String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

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

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes 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 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 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 Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

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

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.