blob: 7a97914c0ab581f7b881f5f14f12c3541c29453c [file] [log] [blame]
Andrey Andreev0ea39292011-12-25 18:48:46 +02001<?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
Andrey Andreev0ea39292011-12-25 18:48:46 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0ea39292011-12-25 18:48:46 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreev0ea39292011-12-25 18:48:46 +020047 public $zipdata = '';
48 public $directory = '';
49 public $entries = 0;
50 public $file_num = 0;
51 public $offset = 0;
52 public $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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020075 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
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 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200100 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500101 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100102 // filemtime() may return false, but raises an error for non-existing files
Thomas Traubdba657e2011-12-06 06:30:37 +0100103 $date = (file_exists($dir)) ? filemtime($dir): getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200104
105 $time = array(
106 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
107 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
108 );
Barry Mienydd671972010-10-04 16:33:58 +0200109
Greg Aker5ed19b42010-03-19 12:13:14 -0500110 return $time;
111 }
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // --------------------------------------------------------------------
114
115 /**
116 * Add Directory
117 *
Andrey Andreevb9535c82011-12-26 16:21:17 +0200118 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @param string the directory name
120 * @return void
121 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200122 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 $dir = str_replace("\\", "/", $dir);
125
126 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500127 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
128 .pack('v', $file_mtime)
129 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 .pack('V', 0) // crc32
131 .pack('V', 0) // compressed filesize
132 .pack('V', 0) // uncompressed filesize
133 .pack('v', strlen($dir)) // length of pathname
134 .pack('v', 0) // extra field length
135 .$dir
136 // below is "data descriptor" segment
137 .pack('V', 0) // crc32
138 .pack('V', 0) // compressed filesize
139 .pack('V', 0); // uncompressed filesize
140
141 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500142 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
143 .pack('v', $file_mtime)
144 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 .pack('V',0) // crc32
146 .pack('V',0) // compressed filesize
147 .pack('V',0) // uncompressed filesize
148 .pack('v', strlen($dir)) // length of pathname
149 .pack('v', 0) // extra field length
150 .pack('v', 0) // file comment length
151 .pack('v', 0) // disk number start
152 .pack('v', 0) // internal file attributes
153 .pack('V', 16) // external file attributes - 'directory' bit set
154 .pack('V', $this->offset) // relative offset of local header
155 .$dir;
156
157 $this->offset = strlen($this->zipdata);
158 $this->entries++;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
162
163 /**
164 * Add Data to Zip
165 *
166 * Lets you add files to the archive. If the path is included
Derek Jones37f4b9c2011-07-01 17:56:50 -0500167 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * sure you use add_dir() first to create the folder.
169 *
170 * @access public
171 * @param mixed
172 * @param string
173 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200174 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200175 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if (is_array($filepath))
178 {
179 foreach ($filepath as $path => $data)
180 {
Barry Mienydd671972010-10-04 16:33:58 +0200181 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500182 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184 }
185 else
186 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500187 $file_data = $this->_get_mod_time($filepath);
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 *
Andrey Andreevb9535c82011-12-26 16:21:17 +0200197 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @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 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200202 protected 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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200254 public function read_file($path, $preserve_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200289 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100290 {
Andrey Andreevb9535c82011-12-26 16:21:17 +0200291 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
292
Derek Jones2735b3e2010-05-11 08:58:21 -0500293 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100295 return FALSE;
296 }
297
298 // Set the original directory root for child dir's to use as relative
299 if ($root_path === NULL)
300 {
301 $root_path = dirname($path).'/';
302 }
303
304 while (FALSE !== ($file = readdir($fp)))
305 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200306 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100308 continue;
309 }
310
311 if (@is_dir($path.$file))
312 {
313 $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
314 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100315 else
316 {
317 if (FALSE !== ($data = file_get_contents($path.$file)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100319 $name = str_replace("\\", "/", $path);
320
321 if ($preserve_filepath === FALSE)
322 {
323 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100325
326 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500330
Phil Sturgeon26872de2010-05-11 11:41:59 +0100331 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Get the Zip file
338 *
339 * @access public
340 * @return binary string
Barry Mienydd671972010-10-04 16:33:58 +0200341 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200342 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 // Is there any data to return?
345 if ($this->entries == 0)
346 {
347 return FALSE;
348 }
349
Andrey Andreev0ea39292011-12-25 18:48:46 +0200350 return $this->zipdata
351 . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
352 . pack('v', $this->entries) // total # of entries "on this disk"
353 . pack('v', $this->entries) // total # of entries overall
354 . pack('V', strlen($this->directory)) // size of central dir
355 . pack('V', strlen($this->zipdata)) // offset to start of central dir
356 . "\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200370 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200395 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200423 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $this->zipdata = '';
426 $this->directory = '';
427 $this->entries = 0;
428 $this->file_num = 0;
429 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200430 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433}
434
435/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200436/* Location: ./system/libraries/Zip.php */