blob: d60b9946295c550410197ec0228ea3d182bb40b1 [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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000071 * @param mixed the directory name. Can be string or array
72 * @return void
73 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020074 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 foreach ((array)$directory as $dir)
77 {
78 if ( ! preg_match("|.+/$|", $dir))
79 {
80 $dir .= '/';
81 }
82
Greg Aker5ed19b42010-03-19 12:13:14 -050083 $dir_time = $this->_get_mod_time($dir);
84
85 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
87 }
88
Barry Mienydd671972010-10-04 16:33:58 +020089 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -050090
91 /**
92 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +020093 *
Greg Aker5ed19b42010-03-19 12:13:14 -050094 * If this is a newly created file/dir, we will set the time to 'now'
95 *
96 * @param string path to file
Barry Mienydd671972010-10-04 16:33:58 +020097 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -050098 */
Andrey Andreevb9535c82011-12-26 16:21:17 +020099 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500100 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100101 // filemtime() may return false, but raises an error for non-existing files
Thomas Traubdba657e2011-12-06 06:30:37 +0100102 $date = (file_exists($dir)) ? filemtime($dir): getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200103
104 $time = array(
105 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
106 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
107 );
Barry Mienydd671972010-10-04 16:33:58 +0200108
Greg Aker5ed19b42010-03-19 12:13:14 -0500109 return $time;
110 }
111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // --------------------------------------------------------------------
113
114 /**
115 * Add Directory
116 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * @param string the directory name
118 * @return void
119 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200120 protected 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @param mixed
169 * @param string
170 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200171 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200172 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200173 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if (is_array($filepath))
175 {
176 foreach ($filepath as $path => $data)
177 {
Barry Mienydd671972010-10-04 16:33:58 +0200178 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500179 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181 }
182 else
183 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500184 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500185 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Add Data to Zip
193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the file name/path
195 * @param string the data to be encoded
196 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200197 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200198 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 $filepath = str_replace("\\", "/", $filepath);
201
202 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500203 $crc32 = crc32($data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205 $gzdata = gzcompress($data);
206 $gzdata = substr($gzdata, 2, -4);
207 $compressed_size = strlen($gzdata);
208
209 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500210 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
211 .pack('v', $file_mtime)
212 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 .pack('V', $crc32)
214 .pack('V', $compressed_size)
215 .pack('V', $uncompressed_size)
216 .pack('v', strlen($filepath)) // length of filename
217 .pack('v', 0) // extra field length
218 .$filepath
219 .$gzdata; // "file data" segment
220
221 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500222 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
223 .pack('v', $file_mtime)
224 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 .pack('V', $crc32)
226 .pack('V', $compressed_size)
227 .pack('V', $uncompressed_size)
228 .pack('v', strlen($filepath)) // length of filename
229 .pack('v', 0) // extra field length
230 .pack('v', 0) // file comment length
231 .pack('v', 0) // disk number start
232 .pack('v', 0) // internal file attributes
233 .pack('V', 32) // external file attributes - 'archive' bit set
234 .pack('V', $this->offset) // relative offset of local header
235 .$filepath;
236
237 $this->offset = strlen($this->zipdata);
238 $this->entries++;
239 $this->file_num++;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Read the contents of a file and add it to the zip
246 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200249 public function read_file($path, $preserve_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 if ( ! file_exists($path))
252 {
253 return FALSE;
254 }
255
256 if (FALSE !== ($data = file_get_contents($path)))
257 {
258 $name = str_replace("\\", "/", $path);
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if ($preserve_filepath === FALSE)
261 {
262 $name = preg_replace("|.*/(.+)|", "\\1", $name);
263 }
264
265 $this->add_data($name, $data);
266 return TRUE;
267 }
268 return FALSE;
269 }
270
271 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 /**
274 * Read a directory and add it to the zip.
275 *
276 * This function recursively reads a folder and everything it contains (including
Derek Jones37f4b9c2011-07-01 17:56:50 -0500277 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * is in the original file path will be recreated in the zip file.
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string path to source
281 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100282 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200283 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100284 {
Andrey Andreevb9535c82011-12-26 16:21:17 +0200285 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
286
Derek Jones2735b3e2010-05-11 08:58:21 -0500287 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100289 return FALSE;
290 }
291
292 // Set the original directory root for child dir's to use as relative
293 if ($root_path === NULL)
294 {
295 $root_path = dirname($path).'/';
296 }
297
298 while (FALSE !== ($file = readdir($fp)))
299 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200300 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100302 continue;
303 }
304
305 if (@is_dir($path.$file))
306 {
307 $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
308 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100309 else
310 {
311 if (FALSE !== ($data = file_get_contents($path.$file)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100313 $name = str_replace("\\", "/", $path);
314
315 if ($preserve_filepath === FALSE)
316 {
317 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100319
320 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500324
Phil Sturgeon26872de2010-05-11 11:41:59 +0100325 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Get the Zip file
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @return binary string
Barry Mienydd671972010-10-04 16:33:58 +0200334 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200335 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 // Is there any data to return?
338 if ($this->entries == 0)
339 {
340 return FALSE;
341 }
342
Andrey Andreev0ea39292011-12-25 18:48:46 +0200343 return $this->zipdata
344 . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
345 . pack('v', $this->entries) // total # of entries "on this disk"
346 . pack('v', $this->entries) // total # of entries overall
347 . pack('V', strlen($this->directory)) // size of central dir
348 . pack('V', strlen($this->zipdata)) // offset to start of central dir
349 . "\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // --------------------------------------------------------------------
353
354 /**
355 * Write File to the specified directory
356 *
357 * Lets you write a file
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string the file name
360 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200361 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200362 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
365 {
366 return FALSE;
367 }
368
Barry Mienydd671972010-10-04 16:33:58 +0200369 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 fwrite($fp, $this->get_zip());
371 flock($fp, LOCK_UN);
372 fclose($fp);
373
Barry Mienydd671972010-10-04 16:33:58 +0200374 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Download
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @param string the file name
383 * @param string the data to be encoded
384 * @return bool
385 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200386 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 if ( ! preg_match("|.+?\.zip$|", $filename))
389 {
390 $filename .= '.zip';
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $CI =& get_instance();
394 $CI->load->helper('download');
395
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000396 $get_zip = $this->get_zip();
397
398 $zip_content =& $get_zip;
399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 force_download($filename, $zip_content);
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Initialize Data
407 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500408 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * multiple zips with different data.
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200412 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200413 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 $this->zipdata = '';
416 $this->directory = '';
417 $this->entries = 0;
418 $this->file_num = 0;
419 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200420 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423}
424
425/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200426/* Location: ./system/libraries/Zip.php */