CI_Zip Class Reference

List of all members.


Public Member Functions

 CI_Zip ()
 add_dir ($directory)
 Add Directory.
 _add_dir ($dir)
 Add Directory.
 add_data ($filepath, $data=NULL)
 Add Data to Zip.
 _add_data ($filepath, $data)
 Add Data to Zip.
 read_file ($path, $preserve_filepath=FALSE)
 Read the contents of a file and add it to the zip.
 read_dir ($path)
 Read a directory and add it to the zip.
 get_zip ()
 Get the Zip file.
 archive ($filepath)
 Write File to the specified directory.
 download ($filename= 'backup.zip')
 Download.
 clear_data ()
 Initialize Data.

Public Attributes

 $zipdata = ''
 $directory = ''
 $entries = 0
 $file_num = 0
 $offset = 0

Detailed Description

Definition at line 33 of file Zip.php.


Member Function Documentation

CI_Zip::_add_data ( filepath,
data 
)

Add Data to Zip.

private

Parameters:
string the file name/path
string the data to be encoded
Returns:
void

Definition at line 153 of file Zip.php.

Referenced by add_data().

00154         {
00155                 $filepath = str_replace("\\", "/", $filepath);
00156 
00157                 $uncompressed_size = strlen($data);
00158                 $crc32  = crc32($data);
00159 
00160                 $gzdata = gzcompress($data);
00161                 $gzdata = substr($gzdata, 2, -4);
00162                 $compressed_size = strlen($gzdata);
00163 
00164                 $this->zipdata .=
00165                         "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00"
00166                         .pack('V', $crc32)
00167                         .pack('V', $compressed_size)
00168                         .pack('V', $uncompressed_size)
00169                         .pack('v', strlen($filepath)) // length of filename
00170                         .pack('v', 0) // extra field length
00171                         .$filepath
00172                         .$gzdata; // "file data" segment
00173 
00174                 $this->directory .=
00175                         "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00"
00176                         .pack('V', $crc32)
00177                         .pack('V', $compressed_size)
00178                         .pack('V', $uncompressed_size)
00179                         .pack('v', strlen($filepath)) // length of filename
00180                         .pack('v', 0) // extra field length
00181                         .pack('v', 0) // file comment length
00182                         .pack('v', 0) // disk number start
00183                         .pack('v', 0) // internal file attributes
00184                         .pack('V', 32) // external file attributes - 'archive' bit set
00185                         .pack('V', $this->offset) // relative offset of local header
00186                         .$filepath;
00187 
00188                 $this->offset = strlen($this->zipdata);
00189                 $this->entries++;
00190                 $this->file_num++;
00191         }

Here is the caller graph for this function:

CI_Zip::_add_dir ( dir  ) 

Add Directory.

private

Parameters:
string the directory name
Returns:
void

Definition at line 79 of file Zip.php.

Referenced by add_dir().

00080         {
00081                 $dir = str_replace("\\", "/", $dir);
00082 
00083                 $this->zipdata .=
00084                         "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
00085                         .pack('V', 0) // crc32
00086                         .pack('V', 0) // compressed filesize
00087                         .pack('V', 0) // uncompressed filesize
00088                         .pack('v', strlen($dir)) // length of pathname
00089                         .pack('v', 0) // extra field length
00090                         .$dir
00091                         // below is "data descriptor" segment
00092                         .pack('V', 0) // crc32
00093                         .pack('V', 0) // compressed filesize
00094                         .pack('V', 0); // uncompressed filesize
00095 
00096                 $this->directory .=
00097                         "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
00098                         .pack('V',0) // crc32
00099                         .pack('V',0) // compressed filesize
00100                         .pack('V',0) // uncompressed filesize
00101                         .pack('v', strlen($dir)) // length of pathname
00102                         .pack('v', 0) // extra field length
00103                         .pack('v', 0) // file comment length
00104                         .pack('v', 0) // disk number start
00105                         .pack('v', 0) // internal file attributes
00106                         .pack('V', 16) // external file attributes - 'directory' bit set
00107                         .pack('V', $this->offset) // relative offset of local header
00108                         .$dir;
00109 
00110                 $this->offset = strlen($this->zipdata);
00111                 $this->entries++;
00112         }

Here is the caller graph for this function:

CI_Zip::add_data ( filepath,
data = NULL 
)

Add Data to Zip.

Lets you add files to the archive. If the path is included in the filename it will be placed within a directory. Make sure you use add_dir() first to create the folder.

public

Parameters:
mixed 
string 
Returns:
void

Definition at line 128 of file Zip.php.

References _add_data().

Referenced by read_dir(), and read_file().

00129         {
00130                 if (is_array($filepath))
00131                 {
00132                         foreach ($filepath as $path => $data)
00133                         {
00134                                 $this->_add_data($path, $data);
00135                         }
00136                 }
00137                 else
00138                 {
00139                         $this->_add_data($filepath, $data);
00140                 }
00141         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Zip::add_dir ( directory  ) 

Add Directory.

Lets you add a virtual directory into which you can place files.

public

Parameters:
mixed the directory name. Can be string or array
Returns:
void

Definition at line 57 of file Zip.php.

References $directory, and _add_dir().

00058         {
00059                 foreach ((array)$directory as $dir)
00060                 {
00061                         if ( ! preg_match("|.+/$|", $dir))
00062                         {
00063                                 $dir .= '/';
00064                         }
00065 
00066                         $this->_add_dir($dir);
00067                 }
00068         }

Here is the call graph for this function:

CI_Zip::archive ( filepath  ) 

Write File to the specified directory.

Lets you write a file

public

Parameters:
string the file name
Returns:
bool

Definition at line 296 of file Zip.php.

References get_zip().

00297         {
00298                 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
00299                 {
00300                         return FALSE;
00301                 }
00302 
00303                 flock($fp, LOCK_EX);    
00304                 fwrite($fp, $this->get_zip());
00305                 flock($fp, LOCK_UN);
00306                 fclose($fp);
00307 
00308                 return TRUE;    
00309         }

Here is the call graph for this function:

CI_Zip::CI_Zip (  ) 

Definition at line 41 of file Zip.php.

References log_message().

00042         {
00043                 log_message('debug', "Zip Compression Class Initialized");
00044         }

Here is the call graph for this function:

CI_Zip::clear_data (  ) 

Initialize Data.

Lets you clear current zip data. Useful if you need to create multiple zips with different data.

public

Returns:
void

Definition at line 347 of file Zip.php.

00348         {
00349                 $this->zipdata          = '';
00350                 $this->directory        = '';
00351                 $this->entries          = 0;
00352                 $this->file_num         = 0;
00353                 $this->offset           = 0;
00354         }

CI_Zip::download ( filename = 'backup.zip'  ) 

Download.

public

Parameters:
string the file name
string the data to be encoded
Returns:
bool

Definition at line 321 of file Zip.php.

References $CI, get_instance(), and get_zip().

00322         {
00323                 if ( ! preg_match("|.+?\.zip$|", $filename))
00324                 {
00325                         $filename .= '.zip';
00326                 }
00327 
00328                 $zip_content =& $this->get_zip();
00329 
00330                 $CI =& get_instance();
00331                 $CI->load->helper('download');
00332 
00333                 force_download($filename, $zip_content);
00334         }

Here is the call graph for this function:

CI_Zip::get_zip (  ) 

Get the Zip file.

public

Returns:
binary string

Definition at line 266 of file Zip.php.

Referenced by archive(), and download().

00267         {
00268                 // Is there any data to return?
00269                 if ($this->entries == 0)
00270                 {
00271                         return FALSE;
00272                 }
00273 
00274                 $zip_data = $this->zipdata;
00275                 $zip_data .= $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00";
00276                 $zip_data .= pack('v', $this->entries); // total # of entries "on this disk"
00277                 $zip_data .= pack('v', $this->entries); // total # of entries overall
00278                 $zip_data .= pack('V', strlen($this->directory)); // size of central dir
00279                 $zip_data .= pack('V', strlen($this->zipdata)); // offset to start of central dir
00280                 $zip_data .= "\x00\x00"; // .zip file comment length
00281 
00282                 return $zip_data;
00283         }

Here is the caller graph for this function:

CI_Zip::read_dir ( path  ) 

Read a directory and add it to the zip.

This function recursively reads a folder and everything it contains (including sub-folders) and creates a zip based on it. Whatever directory structure is in the original file path will be recreated in the zip file.

public

Parameters:
string path to source
Returns:
bool

Definition at line 236 of file Zip.php.

References add_data().

00237         {       
00238                 if ($fp = @opendir($path))
00239                 {
00240                         while (FALSE !== ($file = readdir($fp)))
00241                         {
00242                                 if (@is_dir($path.$file) && substr($file, 0, 1) != '.')
00243                                 {                                       
00244                                         $this->read_dir($path.$file."/");
00245                                 }
00246                                 elseif (substr($file, 0, 1) != ".")
00247                                 {
00248                                         if (FALSE !== ($data = file_get_contents($path.$file)))
00249                                         {                                               
00250                                                 $this->add_data(str_replace("\\", "/", $path).$file, $data);
00251                                         }
00252                                 }
00253                         }
00254                         return TRUE;
00255                 }
00256         }

Here is the call graph for this function:

CI_Zip::read_file ( path,
preserve_filepath = FALSE 
)

Read the contents of a file and add it to the zip.

public

Returns:
bool

Definition at line 201 of file Zip.php.

References add_data().

00202         {
00203                 if ( ! file_exists($path))
00204                 {
00205                         return FALSE;
00206                 }
00207 
00208                 if (FALSE !== ($data = file_get_contents($path)))
00209                 {
00210                         $name = str_replace("\\", "/", $path);
00211                         
00212                         if ($preserve_filepath === FALSE)
00213                         {
00214                                 $name = preg_replace("|.*/(.+)|", "\\1", $name);
00215                         }
00216 
00217                         $this->add_data($name, $data);
00218                         return TRUE;
00219                 }
00220                 return FALSE;
00221         }

Here is the call graph for this function:


Member Data Documentation

CI_Zip::$directory = ''

Definition at line 36 of file Zip.php.

Referenced by add_dir().

CI_Zip::$entries = 0

Definition at line 37 of file Zip.php.

CI_Zip::$file_num = 0

Definition at line 38 of file Zip.php.

CI_Zip::$offset = 0

Definition at line 39 of file Zip.php.

CI_Zip::$zipdata = ''

Definition at line 35 of file Zip.php.


The documentation for this class was generated from the following file: