blob: 3c66c6b3a4acc3290fc49b68ec0e96ebaefcc62e [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 Andreev0ea39292011-12-25 18:48:46 +0200100 private 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 *
118 * @access private
119 * @param string the directory name
120 * @return void
121 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200122 private 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 *
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200202 private 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 {
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 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200304 if ($file[0] === '.')
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 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200340 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 // Is there any data to return?
343 if ($this->entries == 0)
344 {
345 return FALSE;
346 }
347
Andrey Andreev0ea39292011-12-25 18:48:46 +0200348 return $this->zipdata
349 . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
350 . pack('v', $this->entries) // total # of entries "on this disk"
351 . pack('v', $this->entries) // total # of entries overall
352 . pack('V', strlen($this->directory)) // size of central dir
353 . pack('V', strlen($this->zipdata)) // offset to start of central dir
354 . "\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
358
359 /**
360 * Write File to the specified directory
361 *
362 * Lets you write a file
363 *
364 * @access public
365 * @param string the file name
366 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200367 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200368 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
371 {
372 return FALSE;
373 }
374
Barry Mienydd671972010-10-04 16:33:58 +0200375 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 fwrite($fp, $this->get_zip());
377 flock($fp, LOCK_UN);
378 fclose($fp);
379
Barry Mienydd671972010-10-04 16:33:58 +0200380 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Download
387 *
388 * @access public
389 * @param string the file name
390 * @param string the data to be encoded
391 * @return bool
392 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200393 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 if ( ! preg_match("|.+?\.zip$|", $filename))
396 {
397 $filename .= '.zip';
398 }
399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $CI =& get_instance();
401 $CI->load->helper('download');
402
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000403 $get_zip = $this->get_zip();
404
405 $zip_content =& $get_zip;
406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 force_download($filename, $zip_content);
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Initialize Data
414 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500415 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * multiple zips with different data.
417 *
418 * @access public
419 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200420 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200421 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->zipdata = '';
424 $this->directory = '';
425 $this->entries = 0;
426 $this->file_num = 0;
427 $this->offset = 0;
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430}
431
432/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200433/* Location: ./system/libraries/Zip.php */