Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Zip Compression Class |
| 30 | * |
| 31 | * This class is based on a library I found at Zend: |
| 32 | * http://www.zend.com/codex.php?id=696&single=1 |
| 33 | * |
| 34 | * The original library is a little rough around the edges so I |
| 35 | * refactored it and added several additional methods -- Rick Ellis |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Libraries |
| 39 | * @category Encryption |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/libraries/zip.html |
| 42 | */ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 43 | class CI_Zip { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 45 | public $zipdata = ''; |
| 46 | public $directory = ''; |
| 47 | public $entries = 0; |
| 48 | public $file_num = 0; |
| 49 | public $offset = 0; |
| 50 | public $now; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 52 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 54 | log_message('debug', 'Zip Compression Class Initialized'); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 55 | $this->now = time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // -------------------------------------------------------------------- |
| 59 | |
| 60 | /** |
| 61 | * Add Directory |
| 62 | * |
| 63 | * Lets you add a virtual directory into which you can place files. |
| 64 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | * @param mixed the directory name. Can be string or array |
| 66 | * @return void |
| 67 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 68 | public function add_dir($directory) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | { |
| 70 | foreach ((array)$directory as $dir) |
| 71 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 72 | if ( ! preg_match('|.+/$|', $dir)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
| 74 | $dir .= '/'; |
| 75 | } |
| 76 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 77 | $dir_time = $this->_get_mod_time($dir); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 78 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | * |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 87 | * If this is a newly created file/dir, we will set the time to 'now' |
| 88 | * |
| 89 | * @param string path to file |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 91 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 92 | protected function _get_mod_time($dir) |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 93 | { |
Thomas Traub | 98f85b1 | 2011-12-05 14:58:29 +0100 | [diff] [blame] | 94 | // filemtime() may return false, but raises an error for non-existing files |
Thomas Traub | dba657e | 2011-12-06 06:30:37 +0100 | [diff] [blame] | 95 | $date = (file_exists($dir)) ? filemtime($dir): getdate($this->now); |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 96 | |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 97 | return array( |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 98 | 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2, |
| 99 | 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'] |
| 100 | ); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | // -------------------------------------------------------------------- |
| 104 | |
| 105 | /** |
| 106 | * Add Directory |
| 107 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | * @param string the directory name |
| 109 | * @return void |
| 110 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 111 | protected function _add_dir($dir, $file_mtime, $file_mdate) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 113 | $dir = str_replace('\\', '/', $dir); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | |
| 115 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 116 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" |
| 117 | .pack('v', $file_mtime) |
| 118 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | .pack('V', 0) // crc32 |
| 120 | .pack('V', 0) // compressed filesize |
| 121 | .pack('V', 0) // uncompressed filesize |
| 122 | .pack('v', strlen($dir)) // length of pathname |
| 123 | .pack('v', 0) // extra field length |
| 124 | .$dir |
| 125 | // below is "data descriptor" segment |
| 126 | .pack('V', 0) // crc32 |
| 127 | .pack('V', 0) // compressed filesize |
| 128 | .pack('V', 0); // uncompressed filesize |
| 129 | |
| 130 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 131 | "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" |
| 132 | .pack('v', $file_mtime) |
| 133 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | .pack('V',0) // crc32 |
| 135 | .pack('V',0) // compressed filesize |
| 136 | .pack('V',0) // uncompressed filesize |
| 137 | .pack('v', strlen($dir)) // length of pathname |
| 138 | .pack('v', 0) // extra field length |
| 139 | .pack('v', 0) // file comment length |
| 140 | .pack('v', 0) // disk number start |
| 141 | .pack('v', 0) // internal file attributes |
| 142 | .pack('V', 16) // external file attributes - 'directory' bit set |
| 143 | .pack('V', $this->offset) // relative offset of local header |
| 144 | .$dir; |
| 145 | |
| 146 | $this->offset = strlen($this->zipdata); |
| 147 | $this->entries++; |
| 148 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
| 153 | * Add Data to Zip |
| 154 | * |
| 155 | * Lets you add files to the archive. If the path is included |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 156 | * in the filename it will be placed within a directory. Make |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | * sure you use add_dir() first to create the folder. |
| 158 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | * @param mixed |
| 160 | * @param string |
| 161 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 163 | public function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | if (is_array($filepath)) |
| 166 | { |
| 167 | foreach ($filepath as $path => $data) |
| 168 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 170 | $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 175 | $file_data = $this->_get_mod_time($filepath); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 176 | $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Add Data to Zip |
| 184 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | * @param string the file name/path |
| 186 | * @param string the data to be encoded |
| 187 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 189 | protected function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 191 | $filepath = str_replace('\\', '/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | |
| 193 | $uncompressed_size = strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 194 | $crc32 = crc32($data); |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 195 | $gzdata = substr(gzcompress($data), 2, -4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | $compressed_size = strlen($gzdata); |
| 197 | |
| 198 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 199 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 200 | .pack('v', $file_mtime) |
| 201 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | .pack('V', $crc32) |
| 203 | .pack('V', $compressed_size) |
| 204 | .pack('V', $uncompressed_size) |
| 205 | .pack('v', strlen($filepath)) // length of filename |
| 206 | .pack('v', 0) // extra field length |
| 207 | .$filepath |
| 208 | .$gzdata; // "file data" segment |
| 209 | |
| 210 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 211 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 212 | .pack('v', $file_mtime) |
| 213 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | .pack('V', $crc32) |
| 215 | .pack('V', $compressed_size) |
| 216 | .pack('V', $uncompressed_size) |
| 217 | .pack('v', strlen($filepath)) // length of filename |
| 218 | .pack('v', 0) // extra field length |
| 219 | .pack('v', 0) // file comment length |
| 220 | .pack('v', 0) // disk number start |
| 221 | .pack('v', 0) // internal file attributes |
| 222 | .pack('V', 32) // external file attributes - 'archive' bit set |
| 223 | .pack('V', $this->offset) // relative offset of local header |
| 224 | .$filepath; |
| 225 | |
| 226 | $this->offset = strlen($this->zipdata); |
| 227 | $this->entries++; |
| 228 | $this->file_num++; |
| 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Read the contents of a file and add it to the zip |
| 235 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 238 | public function read_file($path, $preserve_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
| 240 | if ( ! file_exists($path)) |
| 241 | { |
| 242 | return FALSE; |
| 243 | } |
| 244 | |
| 245 | if (FALSE !== ($data = file_get_contents($path))) |
| 246 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 247 | $name = str_replace('\\', '/', $path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | if ($preserve_filepath === FALSE) |
| 249 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 250 | $name = preg_replace('|.*/(.+)|', '\\1', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | $this->add_data($name, $data); |
| 254 | return TRUE; |
| 255 | } |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | return FALSE; |
| 258 | } |
| 259 | |
| 260 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | /** |
| 263 | * Read a directory and add it to the zip. |
| 264 | * |
| 265 | * This function recursively reads a folder and everything it contains (including |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 266 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | * is in the original file path will be recreated in the zip file. |
| 268 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | * @param string path to source |
| 270 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 271 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 272 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 273 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 274 | $path = rtrim($path, '/\\').'/'; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 275 | |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 276 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 278 | return FALSE; |
| 279 | } |
| 280 | |
| 281 | // Set the original directory root for child dir's to use as relative |
| 282 | if ($root_path === NULL) |
| 283 | { |
| 284 | $root_path = dirname($path).'/'; |
| 285 | } |
| 286 | |
| 287 | while (FALSE !== ($file = readdir($fp))) |
| 288 | { |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 289 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 291 | continue; |
| 292 | } |
| 293 | |
| 294 | if (@is_dir($path.$file)) |
| 295 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 296 | $this->read_dir($path.$file.'/', $preserve_filepath, $root_path); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 297 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 298 | elseif (FALSE !== ($data = file_get_contents($path.$file))) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 299 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 300 | $name = str_replace('\\', '/', $path); |
| 301 | if ($preserve_filepath === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 303 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 305 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 308 | |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 309 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
| 315 | * Get the Zip file |
| 316 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 317 | * @return string (binary encoded) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 319 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | // Is there any data to return? |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 322 | if ($this->entries === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | { |
| 324 | return FALSE; |
| 325 | } |
| 326 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 327 | return $this->zipdata |
| 328 | . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 329 | . pack('v', $this->entries) // total # of entries "on this disk" |
| 330 | . pack('v', $this->entries) // total # of entries overall |
| 331 | . pack('V', strlen($this->directory)) // size of central dir |
| 332 | . pack('V', strlen($this->zipdata)) // offset to start of central dir |
| 333 | . "\x00\x00"; // .zip file comment length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | // -------------------------------------------------------------------- |
| 337 | |
| 338 | /** |
| 339 | * Write File to the specified directory |
| 340 | * |
| 341 | * Lets you write a file |
| 342 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | * @param string the file name |
| 344 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 346 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
| 348 | if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE))) |
| 349 | { |
| 350 | return FALSE; |
| 351 | } |
| 352 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | flock($fp, LOCK_EX); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | fwrite($fp, $this->get_zip()); |
| 355 | flock($fp, LOCK_UN); |
| 356 | fclose($fp); |
| 357 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * Download |
| 365 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | * @param string the file name |
| 367 | * @param string the data to be encoded |
| 368 | * @return bool |
| 369 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 370 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 372 | if ( ! preg_match('|.+?\.zip$|', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
| 374 | $filename .= '.zip'; |
| 375 | } |
| 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | $CI =& get_instance(); |
| 378 | $CI->load->helper('download'); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 379 | $get_zip = $this->get_zip(); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 380 | $zip_content =& $get_zip; |
| 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | force_download($filename, $zip_content); |
| 383 | } |
| 384 | |
| 385 | // -------------------------------------------------------------------- |
| 386 | |
| 387 | /** |
| 388 | * Initialize Data |
| 389 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 390 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | * multiple zips with different data. |
| 392 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame^] | 393 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 395 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
| 397 | $this->zipdata = ''; |
| 398 | $this->directory = ''; |
| 399 | $this->entries = 0; |
| 400 | $this->file_num = 0; |
| 401 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 402 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* End of file Zip.php */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 408 | /* Location: ./system/libraries/Zip.php */ |