In this tutorial, We will learn how to create a zip file, download it in Codeigniter 3. Follow some steps create a zip file in Codeigniter using the zip library.


Step 1:

Create Route application/config/routes.php

<?php
$route['create-zip'] = "ZipController";


Step 2:

Create Controller application/controllers/ZipController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
  
class ZipController extends CI_Controller {
  
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function __construct() {
       parent::__construct();
       $this->load->library('zip');
    }
   
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index()
    {
        $fileName = 'file1.txt';
        $fileData = 'This file created';
  
        $this->zip->add_data($fileName, $fileData);
  
        $fileName2 = 'file2.txt';
        $fileData2 = 'This file created';
  
        $this->zip->add_data($fileName2, $fileData2);
  
        $this->zip->download('myzip.zip');
    }
      
}


Finally, Run your application.

http://localhost/your_ci_project/create-zip

Thanks, I hope it will work for you...........