Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 47 | var $zipdata = ''; |
| 48 | var $directory = ''; |
| 49 | var $entries = 0; |
| 50 | var $file_num = 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | var $offset = 0; |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 52 | var $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 | * |
| 71 | * @access public |
| 72 | * @param mixed the directory name. Can be string or array |
| 73 | * @return void |
| 74 | */ |
| 75 | function add_dir($directory) |
| 76 | { |
| 77 | foreach ((array)$directory as $dir) |
| 78 | { |
| 79 | if ( ! preg_match("|.+/$|", $dir)) |
| 80 | { |
| 81 | $dir .= '/'; |
| 82 | } |
| 83 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 84 | $dir_time = $this->_get_mod_time($dir); |
| 85 | |
| 86 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | * |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 95 | * If this is a newly created file/dir, we will set the time to 'now' |
| 96 | * |
| 97 | * @param string path to file |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 99 | */ |
| 100 | function _get_mod_time($dir) |
| 101 | { |
| 102 | // filemtime() will return false, but it does raise an error. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | $date = (@filemtime($dir)) ? filemtime($dir) : getdate($this->now); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 104 | |
| 105 | $time['file_mtime'] = ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2; |
| 106 | $time['file_mdate'] = (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 108 | return $time; |
| 109 | } |
| 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
| 114 | * Add Directory |
| 115 | * |
| 116 | * @access private |
| 117 | * @param string the directory name |
| 118 | * @return void |
| 119 | */ |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 120 | 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 | * |
| 168 | * @access public |
| 169 | * @param mixed |
| 170 | * @param string |
| 171 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | if (is_array($filepath)) |
| 176 | { |
| 177 | foreach ($filepath as $path => $data) |
| 178 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 180 | |
| 181 | $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] | 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 186 | $file_data = $this->_get_mod_time($filepath); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 188 | $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] | 189 | } |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | */ |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 202 | function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
| 204 | $filepath = str_replace("\\", "/", $filepath); |
| 205 | |
| 206 | $uncompressed_size = strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 207 | $crc32 = crc32($data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | |
| 209 | $gzdata = gzcompress($data); |
| 210 | $gzdata = substr($gzdata, 2, -4); |
| 211 | $compressed_size = strlen($gzdata); |
| 212 | |
| 213 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 214 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 215 | .pack('v', $file_mtime) |
| 216 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | .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 Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 226 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 227 | .pack('v', $file_mtime) |
| 228 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | .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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | // -------------------------------------------------------------------- |
| 247 | |
| 248 | /** |
| 249 | * Read the contents of a file and add it to the zip |
| 250 | * |
| 251 | * @access public |
| 252 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | function read_file($path, $preserve_filepath = FALSE) |
| 255 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | /** |
| 279 | * Read a directory and add it to the zip. |
| 280 | * |
| 281 | * This function recursively reads a folder and everything it contains (including |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 282 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | * 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 Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 288 | */ |
| 289 | function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
| 290 | { |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 291 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 293 | 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 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 304 | if (substr($file, 0, 1) == '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 306 | continue; |
| 307 | } |
| 308 | |
| 309 | if (@is_dir($path.$file)) |
| 310 | { |
| 311 | $this->read_dir($path.$file."/", $preserve_filepath, $root_path); |
| 312 | } |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 313 | else |
| 314 | { |
| 315 | if (FALSE !== ($data = file_get_contents($path.$file))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 317 | $name = str_replace("\\", "/", $path); |
| 318 | |
| 319 | if ($preserve_filepath === FALSE) |
| 320 | { |
| 321 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | } |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 323 | |
| 324 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 328 | |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 329 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * Get the Zip file |
| 336 | * |
| 337 | * @access public |
| 338 | * @return binary string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | function get_zip() |
| 341 | { |
| 342 | // Is there any data to return? |
| 343 | if ($this->entries == 0) |
| 344 | { |
| 345 | return FALSE; |
| 346 | } |
| 347 | |
| 348 | $zip_data = $this->zipdata; |
| 349 | $zip_data .= $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"; |
| 350 | $zip_data .= pack('v', $this->entries); // total # of entries "on this disk" |
| 351 | $zip_data .= pack('v', $this->entries); // total # of entries overall |
| 352 | $zip_data .= pack('V', strlen($this->directory)); // size of central dir |
| 353 | $zip_data .= pack('V', strlen($this->zipdata)); // offset to start of central dir |
| 354 | $zip_data .= "\x00\x00"; // .zip file comment length |
| 355 | |
| 356 | return $zip_data; |
| 357 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | // -------------------------------------------------------------------- |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | function archive($filepath) |
| 371 | { |
| 372 | if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE))) |
| 373 | { |
| 374 | return FALSE; |
| 375 | } |
| 376 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 377 | flock($fp, LOCK_EX); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | fwrite($fp, $this->get_zip()); |
| 379 | flock($fp, LOCK_UN); |
| 380 | fclose($fp); |
| 381 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | } |
| 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 | */ |
| 395 | function download($filename = 'backup.zip') |
| 396 | { |
| 397 | if ( ! preg_match("|.+?\.zip$|", $filename)) |
| 398 | { |
| 399 | $filename .= '.zip'; |
| 400 | } |
| 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | $CI =& get_instance(); |
| 403 | $CI->load->helper('download'); |
| 404 | |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 405 | $get_zip = $this->get_zip(); |
| 406 | |
| 407 | $zip_content =& $get_zip; |
| 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | force_download($filename, $zip_content); |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Initialize Data |
| 416 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 417 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | * multiple zips with different data. |
| 419 | * |
| 420 | * @access public |
| 421 | * @return void |
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 | function clear_data() |
| 424 | { |
| 425 | $this->zipdata = ''; |
| 426 | $this->directory = ''; |
| 427 | $this->entries = 0; |
| 428 | $this->file_num = 0; |
| 429 | $this->offset = 0; |
| 430 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* End of file Zip.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 435 | /* Location: ./system/libraries/Zip.php */ |