1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Zip Compression Class
21 * This class is based on a library I found at Zend:
22 * http://www.zend.com/codex.php?id=696&single=1
24 * The original library is a little rough around the edges so I
25 * refactored it and added several additional methods -- Rick Ellis
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category Encryption
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/libraries/zip.html
45 public function __construct()
47 log_message('debug', "Zip Compression Class Initialized");
52 // --------------------------------------------------------------------
57 * Lets you add a virtual directory into which you can place files.
60 * @param mixed the directory name. Can be string or array
63 function add_dir($directory)
65 foreach ((array)$directory as $dir)
67 if ( ! preg_match("|.+/$|", $dir))
72 $dir_time = $this->_get_mod_time($dir);
74 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
78 // --------------------------------------------------------------------
81 * Get file/directory modification time
83 * If this is a newly created file/dir, we will set the time to 'now'
85 * @param string path to file
86 * @return array filemtime/filemdate
88 function _get_mod_time($dir)
90 // filemtime() will return false, but it does raise an error.
91 $date = (@filemtime($dir)) ? filemtime($dir) : getdate($this->now);
93 $time['file_mtime'] = ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2;
94 $time['file_mdate'] = (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'];
99 // --------------------------------------------------------------------
105 * @param string the directory name
108 function _add_dir($dir, $file_mtime, $file_mdate)
110 $dir = str_replace("\\", "/", $dir);
113 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
114 .pack('v', $file_mtime)
115 .pack('v', $file_mdate)
116 .pack('V', 0) // crc32
117 .pack('V', 0) // compressed filesize
118 .pack('V', 0) // uncompressed filesize
119 .pack('v', strlen($dir)) // length of pathname
120 .pack('v', 0) // extra field length
122 // below is "data descriptor" segment
123 .pack('V', 0) // crc32
124 .pack('V', 0) // compressed filesize
125 .pack('V', 0); // uncompressed filesize
128 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
129 .pack('v', $file_mtime)
130 .pack('v', $file_mdate)
131 .pack('V',0) // crc32
132 .pack('V',0) // compressed filesize
133 .pack('V',0) // uncompressed filesize
134 .pack('v', strlen($dir)) // length of pathname
135 .pack('v', 0) // extra field length
136 .pack('v', 0) // file comment length
137 .pack('v', 0) // disk number start
138 .pack('v', 0) // internal file attributes
139 .pack('V', 16) // external file attributes - 'directory' bit set
140 .pack('V', $this->offset) // relative offset of local header
143 $this->offset = strlen($this->zipdata);
147 // --------------------------------------------------------------------
152 * Lets you add files to the archive. If the path is included
153 * in the filename it will be placed within a directory. Make
154 * sure you use add_dir() first to create the folder.
161 function add_data($filepath, $data = NULL)
163 if (is_array($filepath))
165 foreach ($filepath as $path => $data)
167 $file_data = $this->_get_mod_time($path);
169 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
174 $file_data = $this->_get_mod_time($filepath);
176 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
180 // --------------------------------------------------------------------
186 * @param string the file name/path
187 * @param string the data to be encoded
190 function _add_data($filepath, $data, $file_mtime, $file_mdate)
192 $filepath = str_replace("\\", "/", $filepath);
194 $uncompressed_size = strlen($data);
195 $crc32 = crc32($data);
197 $gzdata = gzcompress($data);
198 $gzdata = substr($gzdata, 2, -4);
199 $compressed_size = strlen($gzdata);
202 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
203 .pack('v', $file_mtime)
204 .pack('v', $file_mdate)
206 .pack('V', $compressed_size)
207 .pack('V', $uncompressed_size)
208 .pack('v', strlen($filepath)) // length of filename
209 .pack('v', 0) // extra field length
211 .$gzdata; // "file data" segment
214 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
215 .pack('v', $file_mtime)
216 .pack('v', $file_mdate)
218 .pack('V', $compressed_size)
219 .pack('V', $uncompressed_size)
220 .pack('v', strlen($filepath)) // length of filename
221 .pack('v', 0) // extra field length
222 .pack('v', 0) // file comment length
223 .pack('v', 0) // disk number start
224 .pack('v', 0) // internal file attributes
225 .pack('V', 32) // external file attributes - 'archive' bit set
226 .pack('V', $this->offset) // relative offset of local header
229 $this->offset = strlen($this->zipdata);
234 // --------------------------------------------------------------------
237 * Read the contents of a file and add it to the zip
242 function read_file($path, $preserve_filepath = FALSE)
244 if ( ! file_exists($path))
249 if (FALSE !== ($data = file_get_contents($path)))
251 $name = str_replace("\\", "/", $path);
253 if ($preserve_filepath === FALSE)
255 $name = preg_replace("|.*/(.+)|", "\\1", $name);
258 $this->add_data($name, $data);
264 // ------------------------------------------------------------------------
267 * Read a directory and add it to the zip.
269 * This function recursively reads a folder and everything it contains (including
270 * sub-folders) and creates a zip based on it. Whatever directory structure
271 * is in the original file path will be recreated in the zip file.
274 * @param string path to source
277 function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
279 if ( ! $fp = @opendir($path))
284 // Set the original directory root for child dir's to use as relative
285 if ($root_path === NULL)
287 $root_path = dirname($path).'/';
290 while (FALSE !== ($file = readdir($fp)))
292 if (substr($file, 0, 1) == '.')
297 if (@is_dir($path.$file))
299 $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
303 if (FALSE !== ($data = file_get_contents($path.$file)))
305 $name = str_replace("\\", "/", $path);
307 if ($preserve_filepath === FALSE)
309 $name = str_replace($root_path, '', $name);
312 $this->add_data($name.$file, $data);
320 // --------------------------------------------------------------------
326 * @return binary string
330 // Is there any data to return?
331 if ($this->entries == 0)
336 $zip_data = $this->zipdata;
337 $zip_data .= $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00";
338 $zip_data .= pack('v', $this->entries); // total # of entries "on this disk"
339 $zip_data .= pack('v', $this->entries); // total # of entries overall
340 $zip_data .= pack('V', strlen($this->directory)); // size of central dir
341 $zip_data .= pack('V', strlen($this->zipdata)); // offset to start of central dir
342 $zip_data .= "\x00\x00"; // .zip file comment length
347 // --------------------------------------------------------------------
350 * Write File to the specified directory
352 * Lets you write a file
355 * @param string the file name
358 function archive($filepath)
360 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
366 fwrite($fp, $this->get_zip());
373 // --------------------------------------------------------------------
379 * @param string the file name
380 * @param string the data to be encoded
383 function download($filename = 'backup.zip')
385 if ( ! preg_match("|.+?\.zip$|", $filename))
390 $CI =& get_instance();
391 $CI->load->helper('download');
393 $get_zip = $this->get_zip();
395 $zip_content =& $get_zip;
397 force_download($filename, $zip_content);
400 // --------------------------------------------------------------------
405 * Lets you clear current zip data. Useful if you need to create
406 * multiple zips with different data.
411 function clear_data()
414 $this->directory = '';
422 /* End of file Zip.php */
423 /* Location: ./system/libraries/Zip.php */