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 |
| 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 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 | |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 42 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 43 | * @link http://codeigniter.com/user_guide/libraries/zip.html |
| 44 | */ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 45 | class CI_Zip { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 47 | public $zipdata = ''; |
| 48 | public $directory = ''; |
| 49 | public $entries = 0; |
| 50 | public $file_num = 0; |
| 51 | public $offset = 0; |
| 52 | public $now; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 54 | /** |
| 55 | * Constructor |
| 56 | */ |
| 57 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | { |
| 59 | log_message('debug', "Zip Compression Class Initialized"); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 61 | $this->now = time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // -------------------------------------------------------------------- |
| 65 | |
| 66 | /** |
| 67 | * Add Directory |
| 68 | * |
| 69 | * Lets you add a virtual directory into which you can place files. |
| 70 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | * @param mixed the directory name. Can be string or array |
| 72 | * @return void |
| 73 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 74 | public function add_dir($directory) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
| 76 | foreach ((array)$directory as $dir) |
| 77 | { |
| 78 | if ( ! preg_match("|.+/$|", $dir)) |
| 79 | { |
| 80 | $dir .= '/'; |
| 81 | } |
| 82 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 83 | $dir_time = $this->_get_mod_time($dir); |
| 84 | |
| 85 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | * |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 94 | * If this is a newly created file/dir, we will set the time to 'now' |
| 95 | * |
| 96 | * @param string path to file |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 98 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 99 | protected function _get_mod_time($dir) |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 100 | { |
Thomas Traub | 98f85b1 | 2011-12-05 14:58:29 +0100 | [diff] [blame] | 101 | // filemtime() may return false, but raises an error for non-existing files |
Thomas Traub | dba657e | 2011-12-06 06:30:37 +0100 | [diff] [blame] | 102 | $date = (file_exists($dir)) ? filemtime($dir): getdate($this->now); |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 103 | |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 109 | return $time; |
| 110 | } |
| 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
| 115 | * Add Directory |
| 116 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | * @param string the directory name |
| 118 | * @return void |
| 119 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 120 | protected function _add_dir($dir, $file_mtime, $file_mdate) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | $dir = str_replace("\\", "/", $dir); |
| 123 | |
| 124 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 125 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" |
| 126 | .pack('v', $file_mtime) |
| 127 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | .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 Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 140 | "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" |
| 141 | .pack('v', $file_mtime) |
| 142 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | .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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | // -------------------------------------------------------------------- |
| 160 | |
| 161 | /** |
| 162 | * Add Data to Zip |
| 163 | * |
| 164 | * Lets you add files to the archive. If the path is included |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 165 | * in the filename it will be placed within a directory. Make |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | * sure you use add_dir() first to create the folder. |
| 167 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | * @param mixed |
| 169 | * @param string |
| 170 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 172 | public function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | if (is_array($filepath)) |
| 175 | { |
| 176 | foreach ($filepath as $path => $data) |
| 177 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 179 | $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] | 180 | } |
| 181 | } |
| 182 | else |
| 183 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 184 | $file_data = $this->_get_mod_time($filepath); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 185 | $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] | 186 | } |
| 187 | } |
| 188 | |
| 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Add Data to Zip |
| 193 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * @param string the file name/path |
| 195 | * @param string the data to be encoded |
| 196 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 198 | protected function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
| 200 | $filepath = str_replace("\\", "/", $filepath); |
| 201 | |
| 202 | $uncompressed_size = strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 203 | $crc32 = crc32($data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | |
| 205 | $gzdata = gzcompress($data); |
| 206 | $gzdata = substr($gzdata, 2, -4); |
| 207 | $compressed_size = strlen($gzdata); |
| 208 | |
| 209 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 210 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 211 | .pack('v', $file_mtime) |
| 212 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | .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 Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 222 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 223 | .pack('v', $file_mtime) |
| 224 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | .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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Read the contents of a file and add it to the zip |
| 246 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 249 | public function read_file($path, $preserve_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | /** |
| 274 | * Read a directory and add it to the zip. |
| 275 | * |
| 276 | * This function recursively reads a folder and everything it contains (including |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 277 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | * is in the original file path will be recreated in the zip file. |
| 279 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | * @param string path to source |
| 281 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 282 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 283 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 284 | { |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 285 | $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
| 286 | |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 287 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 289 | 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 Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 300 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 302 | continue; |
| 303 | } |
| 304 | |
| 305 | if (@is_dir($path.$file)) |
| 306 | { |
| 307 | $this->read_dir($path.$file."/", $preserve_filepath, $root_path); |
| 308 | } |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | if (FALSE !== ($data = file_get_contents($path.$file))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 313 | $name = str_replace("\\", "/", $path); |
| 314 | |
| 315 | if ($preserve_filepath === FALSE) |
| 316 | { |
| 317 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | } |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 319 | |
| 320 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 324 | |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 325 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // -------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * Get the Zip file |
| 332 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | * @return binary string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 335 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | { |
| 337 | // Is there any data to return? |
| 338 | if ($this->entries == 0) |
| 339 | { |
| 340 | return FALSE; |
| 341 | } |
| 342 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 343 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
| 355 | * Write File to the specified directory |
| 356 | * |
| 357 | * Lets you write a file |
| 358 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | * @param string the file name |
| 360 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 362 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
| 364 | if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE))) |
| 365 | { |
| 366 | return FALSE; |
| 367 | } |
| 368 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | flock($fp, LOCK_EX); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | fwrite($fp, $this->get_zip()); |
| 371 | flock($fp, LOCK_UN); |
| 372 | fclose($fp); |
| 373 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // -------------------------------------------------------------------- |
| 378 | |
| 379 | /** |
| 380 | * Download |
| 381 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | * @param string the file name |
| 383 | * @param string the data to be encoded |
| 384 | * @return bool |
| 385 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 386 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
| 388 | if ( ! preg_match("|.+?\.zip$|", $filename)) |
| 389 | { |
| 390 | $filename .= '.zip'; |
| 391 | } |
| 392 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | $CI =& get_instance(); |
| 394 | $CI->load->helper('download'); |
| 395 | |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 396 | $get_zip = $this->get_zip(); |
| 397 | |
| 398 | $zip_content =& $get_zip; |
| 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | force_download($filename, $zip_content); |
| 401 | } |
| 402 | |
| 403 | // -------------------------------------------------------------------- |
| 404 | |
| 405 | /** |
| 406 | * Initialize Data |
| 407 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 408 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | * multiple zips with different data. |
| 410 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 413 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | { |
| 415 | $this->zipdata = ''; |
| 416 | $this->directory = ''; |
| 417 | $this->entries = 0; |
| 418 | $this->file_num = 0; |
| 419 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 420 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 422 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* End of file Zip.php */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 426 | /* Location: ./system/libraries/Zip.php */ |