blob: 9101eecb40631f4f562482cc68e8e28c1abb849a [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Zip Compression Class
32 *
33 * This class is based on a library I found at Zend:
34 * http://www.zend.com/codex.php?id=696&single=1
35 *
36 * The original library is a little rough around the edges so I
37 * refactored it and added several additional methods -- Rick Ellis
38 *
39 * @package CodeIgniter
40 * @subpackage Libraries
41 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050042 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @link http://codeigniter.com/user_guide/libraries/zip.html
44 */
Derek Jones37f4b9c2011-07-01 17:56:50 -050045class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Barry Mienydd671972010-10-04 16:33:58 +020047 var $zipdata = '';
48 var $directory = '';
49 var $entries = 0;
50 var $file_num = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +000051 var $offset = 0;
Greg Aker5ed19b42010-03-19 12:13:14 -050052 var $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Greg Akera9263282010-11-10 15:26:43 -060054 /**
55 * Constructor
56 */
57 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
59 log_message('debug', "Zip Compression Class Initialized");
Barry Mienydd671972010-10-04 16:33:58 +020060
Greg Aker5ed19b42010-03-19 12:13:14 -050061 $this->now = time();
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Add Directory
68 *
69 * Lets you add a virtual directory into which you can place files.
70 *
71 * @access public
72 * @param mixed the directory name. Can be string or array
73 * @return void
74 */
75 function add_dir($directory)
76 {
77 foreach ((array)$directory as $dir)
78 {
79 if ( ! preg_match("|.+/$|", $dir))
80 {
81 $dir .= '/';
82 }
83
Greg Aker5ed19b42010-03-19 12:13:14 -050084 $dir_time = $this->_get_mod_time($dir);
85
86 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
88 }
89
Barry Mienydd671972010-10-04 16:33:58 +020090 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -050091
92 /**
93 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +020094 *
Greg Aker5ed19b42010-03-19 12:13:14 -050095 * If this is a newly created file/dir, we will set the time to 'now'
96 *
97 * @param string path to file
Barry Mienydd671972010-10-04 16:33:58 +020098 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -050099 */
100 function _get_mod_time($dir)
101 {
102 // filemtime() will return false, but it does raise an error.
Barry Mienydd671972010-10-04 16:33:58 +0200103 $date = (@filemtime($dir)) ? filemtime($dir) : getdate($this->now);
Greg Aker5ed19b42010-03-19 12:13:14 -0500104
105 $time['file_mtime'] = ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2;
106 $time['file_mdate'] = (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'];
Barry Mienydd671972010-10-04 16:33:58 +0200107
Greg Aker5ed19b42010-03-19 12:13:14 -0500108 return $time;
109 }
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
112
113 /**
114 * Add Directory
115 *
116 * @access private
117 * @param string the directory name
118 * @return void
119 */
Greg Aker5ed19b42010-03-19 12:13:14 -0500120 function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200121 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 $dir = str_replace("\\", "/", $dir);
123
124 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500125 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
126 .pack('v', $file_mtime)
127 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 .pack('V', 0) // crc32
129 .pack('V', 0) // compressed filesize
130 .pack('V', 0) // uncompressed filesize
131 .pack('v', strlen($dir)) // length of pathname
132 .pack('v', 0) // extra field length
133 .$dir
134 // below is "data descriptor" segment
135 .pack('V', 0) // crc32
136 .pack('V', 0) // compressed filesize
137 .pack('V', 0); // uncompressed filesize
138
139 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500140 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
141 .pack('v', $file_mtime)
142 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 .pack('V',0) // crc32
144 .pack('V',0) // compressed filesize
145 .pack('V',0) // uncompressed filesize
146 .pack('v', strlen($dir)) // length of pathname
147 .pack('v', 0) // extra field length
148 .pack('v', 0) // file comment length
149 .pack('v', 0) // disk number start
150 .pack('v', 0) // internal file attributes
151 .pack('V', 16) // external file attributes - 'directory' bit set
152 .pack('V', $this->offset) // relative offset of local header
153 .$dir;
154
155 $this->offset = strlen($this->zipdata);
156 $this->entries++;
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // --------------------------------------------------------------------
160
161 /**
162 * Add Data to Zip
163 *
164 * Lets you add files to the archive. If the path is included
Derek Jones37f4b9c2011-07-01 17:56:50 -0500165 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * sure you use add_dir() first to create the folder.
167 *
168 * @access public
169 * @param mixed
170 * @param string
171 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200172 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200174 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if (is_array($filepath))
176 {
177 foreach ($filepath as $path => $data)
178 {
Barry Mienydd671972010-10-04 16:33:58 +0200179 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500180
181 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183 }
184 else
185 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500186 $file_data = $this->_get_mod_time($filepath);
Barry Mienydd671972010-10-04 16:33:58 +0200187
Greg Aker5ed19b42010-03-19 12:13:14 -0500188 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Add Data to Zip
196 *
197 * @access private
198 * @param string the file name/path
199 * @param string the data to be encoded
200 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200201 */
Greg Aker5ed19b42010-03-19 12:13:14 -0500202 function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 $filepath = str_replace("\\", "/", $filepath);
205
206 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500207 $crc32 = crc32($data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000208
209 $gzdata = gzcompress($data);
210 $gzdata = substr($gzdata, 2, -4);
211 $compressed_size = strlen($gzdata);
212
213 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500214 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
215 .pack('v', $file_mtime)
216 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 .pack('V', $crc32)
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 .$filepath
223 .$gzdata; // "file data" segment
224
225 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500226 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
227 .pack('v', $file_mtime)
228 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 .pack('V', $crc32)
230 .pack('V', $compressed_size)
231 .pack('V', $uncompressed_size)
232 .pack('v', strlen($filepath)) // length of filename
233 .pack('v', 0) // extra field length
234 .pack('v', 0) // file comment length
235 .pack('v', 0) // disk number start
236 .pack('v', 0) // internal file attributes
237 .pack('V', 32) // external file attributes - 'archive' bit set
238 .pack('V', $this->offset) // relative offset of local header
239 .$filepath;
240
241 $this->offset = strlen($this->zipdata);
242 $this->entries++;
243 $this->file_num++;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * Read the contents of a file and add it to the zip
250 *
251 * @access public
252 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200253 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 function read_file($path, $preserve_filepath = FALSE)
255 {
256 if ( ! file_exists($path))
257 {
258 return FALSE;
259 }
260
261 if (FALSE !== ($data = file_get_contents($path)))
262 {
263 $name = str_replace("\\", "/", $path);
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 if ($preserve_filepath === FALSE)
266 {
267 $name = preg_replace("|.*/(.+)|", "\\1", $name);
268 }
269
270 $this->add_data($name, $data);
271 return TRUE;
272 }
273 return FALSE;
274 }
275
276 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 /**
279 * Read a directory and add it to the zip.
280 *
281 * This function recursively reads a folder and everything it contains (including
Derek Jones37f4b9c2011-07-01 17:56:50 -0500282 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * is in the original file path will be recreated in the zip file.
284 *
285 * @access public
286 * @param string path to source
287 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100288 */
289 function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
290 {
Derek Jones2735b3e2010-05-11 08:58:21 -0500291 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100293 return FALSE;
294 }
295
296 // Set the original directory root for child dir's to use as relative
297 if ($root_path === NULL)
298 {
299 $root_path = dirname($path).'/';
300 }
301
302 while (FALSE !== ($file = readdir($fp)))
303 {
Pascal Kriete14287f32011-02-14 13:39:34 -0500304 if (substr($file, 0, 1) == '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100306 continue;
307 }
308
309 if (@is_dir($path.$file))
310 {
311 $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
312 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100313 else
314 {
315 if (FALSE !== ($data = file_get_contents($path.$file)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100317 $name = str_replace("\\", "/", $path);
318
319 if ($preserve_filepath === FALSE)
320 {
321 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100323
324 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500328
Phil Sturgeon26872de2010-05-11 11:41:59 +0100329 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Get the Zip file
336 *
337 * @access public
338 * @return binary string
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 function get_zip()
341 {
342 // Is there any data to return?
343 if ($this->entries == 0)
344 {
345 return FALSE;
346 }
347
348 $zip_data = $this->zipdata;
349 $zip_data .= $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00";
350 $zip_data .= pack('v', $this->entries); // total # of entries "on this disk"
351 $zip_data .= pack('v', $this->entries); // total # of entries overall
352 $zip_data .= pack('V', strlen($this->directory)); // size of central dir
353 $zip_data .= pack('V', strlen($this->zipdata)); // offset to start of central dir
354 $zip_data .= "\x00\x00"; // .zip file comment length
355
356 return $zip_data;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
360
361 /**
362 * Write File to the specified directory
363 *
364 * Lets you write a file
365 *
366 * @access public
367 * @param string the file name
368 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200369 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 function archive($filepath)
371 {
372 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
373 {
374 return FALSE;
375 }
376
Barry Mienydd671972010-10-04 16:33:58 +0200377 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 fwrite($fp, $this->get_zip());
379 flock($fp, LOCK_UN);
380 fclose($fp);
381
Barry Mienydd671972010-10-04 16:33:58 +0200382 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Download
389 *
390 * @access public
391 * @param string the file name
392 * @param string the data to be encoded
393 * @return bool
394 */
395 function download($filename = 'backup.zip')
396 {
397 if ( ! preg_match("|.+?\.zip$|", $filename))
398 {
399 $filename .= '.zip';
400 }
401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 $CI =& get_instance();
403 $CI->load->helper('download');
404
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000405 $get_zip = $this->get_zip();
406
407 $zip_content =& $get_zip;
408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 force_download($filename, $zip_content);
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Initialize Data
416 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500417 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * multiple zips with different data.
419 *
420 * @access public
421 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200422 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 function clear_data()
424 {
425 $this->zipdata = '';
426 $this->directory = '';
427 $this->entries = 0;
428 $this->file_num = 0;
429 $this->offset = 0;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432}
433
434/* End of file Zip.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000435/* Location: ./system/libraries/Zip.php */