Codeigniter Zip Encoding Class Library
Codeigniter Zip Encoding Class Library – This library is used to craete a zip file either text or binary data type. Archives can be downloaded to your desktop or saved to a directory. We can load zip encoding class library like this $this->load->library(‘zip’);. Here in this tutorial, we are going to explain how to use zip encoding class library.
Codeigniter zip encoding class library | Example.
Let us understand how zip encoding class library works in codeigniter with examples.
Load zip encoding class library
First load zip encoding class library to use its functions, this library can be loaded simply as below-
How to load zip encoding class library:
$this->load->library('zip'); |
Class reference:-
There are following references available in zip encoding class library. Now we will explain.
1. Compression level.
This reference is used to set range from 0 to 9.
compression_level = 2
$this->zip->compression_level = 0;
2. Add data.
This reference is used to add data to the zip archive. it can work with single and multiple file mode.
add_data($filepath[$data = NULL])
- Parameters :
- $filepath (mixed) : A single file path or an array of file => data pairs.
- $data (array) : File contents (ignored if $filepath is an array).
- Returns type : void
EXAMPLE.
Here is simple demo of add data class references.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class zip_controller extends CI_Controller { public function addData() { $this->load->library('zip'); $filename = 'hello.txt'; $filedata = 'Welcome page'; $this->zip->add_data($filename, $filedata); } } ?> |
3. Add directory.
This reference gives permit you to add directory.
add_dir($directory)
- Parameters :
- $directory (mixed) : Directory name string or an array of multiple directories.
- Returns type : void
EXAMPLE.
Here is simple demo of add directory class references.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class zip_controller extends CI_Controller { public function addDirectory() { $this->load->library('zip'); $this->zip->add_dir('myfolder'); $this->zip->download('my_directory.zip'); } } ?> |
4. Read File.
This reference gives permit you to compressa file.
read_file($path[$archive_filepath = FALSE])
- Parameters :
- $path (string) : Path to file.
- $archive_filepath (mixed) : New file name/path (string) or (boolean) whether to maintain the original filepath.
- Returns type : TRUE on success, FALSE on failure
- Returns type : Bool
EXAMPLE.
Here is simple demo of read file.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class zip_controller extends CI_Controller { public function read() { $this->load->library('zip'); $path = 'uploads/Jellyfish.jpg'; $newpath = './imagesManupulation/read.jpg'; $this->zip->read_file($path, $newpath); $this->zip->download('my_backup.zip'); } } ?> |
5. Read Directory.
This reference gives permit you to compress a directory.
read_dir($path[$preserve_filepath = TRUE[$root_path = NULL]])
- Parameters :
- $path (string) : Path to file.
- $preserve_filepath (bool) : Whether to maintain the original path.
- $root_path (string) : Part of the path to exclude from the archive directory.
- Returns type : TRUE on success, FALSE on failure
- Returns type : Bool
EXAMPLE.
Here is simple demo of read directory.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class zip_controller extends CI_Controller { public function readDirectory() { $this->load->library('zip'); $path = './uploads'; $this->zip->read_dir($path); $this->zip->archive('D:/xampp/htdocs/CodeIgniter/file/zip.zip'); } } } ?> |
6. Archive.
This reference is used to write teh zip encoded file to a directory.
archive($filepath)
- Parameters :
- $path (string) : Path to file.
- Returns type : TRUE on success, FALSE on failure
- Returns type : Bool
EXAMPLE.
$this->zip->archive('path to folder/zip.zip');
7. Download.
This reference is used to zip file to be downloaded from your server.
download($filename = 'solidcoupon.zip')
- Parameters :
- $filename (string) : Archive file name.
- Returns type : void
EXAMPLE.
$this->zip->download('solidcoupon.zip');
8. Get zip.
This reference Returns the zip-compressed file data.
get_zip()
- Parameters :
- $path (string) : Path to file.
- $preserve_filepath (bool) : Whether to maintain the original path.
- $root_path (string) : Part of the path to exclude from the archive directory.
- Returns : Zip file content
- Returns type : string
EXAMPLE.
Here is simple demo of get zip file.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class zip_controller extends CI_Controller { public function get() { $this->load->library('zip'); $name = 'mytext.txt'; $data = 'Welcome to SolidCoupon.'; $this->zip->add_data($name, $data); $zip_file = $this->zip->get_zip(); $this->zip->download($zip_file); } } } ?> |
8. Clear data.
This reference is used to clear caches between calls the program.
clear_data()
- Parameters :
- Returns type : Void
EXAMPLE.
Here is simple demo of get zip file.
$this->zip->clear_data(); |
Advertisements