blob: 434229471bdcc631be660f7c120b8e2fb2174560 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev0ea39292011-12-25 18:48:46 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev0ea39292011-12-25 18:48:46 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Zip Compression Class
42 *
43 * This class is based on a library I found at Zend:
44 * http://www.zend.com/codex.php?id=696&single=1
45 *
46 * The original library is a little rough around the edges so I
47 * refactored it and added several additional methods -- Rick Ellis
48 *
49 * @package CodeIgniter
50 * @subpackage Libraries
51 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050052 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @link http://codeigniter.com/user_guide/libraries/zip.html
54 */
vkeranov95b2f4f2012-06-16 20:37:11 +030055class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000056
Timothy Warren86611db2012-04-27 10:06:25 -040057 /**
58 * Zip data in string form
59 *
60 * @var string
61 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020062 public $zipdata = '';
Andrey Andreev56454792012-05-17 14:32:19 +030063
Timothy Warren86611db2012-04-27 10:06:25 -040064 /**
65 * Zip data for a directory in string form
66 *
67 * @var string
68 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020069 public $directory = '';
Andrey Andreev56454792012-05-17 14:32:19 +030070
Timothy Warren86611db2012-04-27 10:06:25 -040071 /**
72 * Number of files/folder in zip file
73 *
74 * @var int
75 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020076 public $entries = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030077
Timothy Warren86611db2012-04-27 10:06:25 -040078 /**
79 * Number of files in zip
80 *
81 * @var int
82 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020083 public $file_num = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030084
Timothy Warren86611db2012-04-27 10:06:25 -040085 /**
86 * relative offset of local header
87 *
88 * @var int
89 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020090 public $offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030091
Timothy Warren86611db2012-04-27 10:06:25 -040092 /**
93 * Reference to time at init
94 *
95 * @var int
96 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020097 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000098
Timothy Warren86611db2012-04-27 10:06:25 -040099 /**
100 * Initialize zip compression class
101 *
Andrey Andreev56454792012-05-17 14:32:19 +0300102 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400103 */
Greg Akera9263282010-11-10 15:26:43 -0600104 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500106 $this->now = time();
Andrey Andreevbb2e5182012-03-26 13:58:12 +0300107 log_message('debug', 'Zip Compression Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
110 // --------------------------------------------------------------------
111
112 /**
113 * Add Directory
114 *
115 * Lets you add a virtual directory into which you can place files.
116 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300117 * @param mixed $directory the directory name. Can be string or array
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * @return void
119 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200120 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreev88f41df2013-09-23 15:24:43 +0300122 foreach ((array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200124 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 $dir .= '/';
127 }
128
Greg Aker5ed19b42010-03-19 12:13:14 -0500129 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -0500130 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132 }
133
Barry Mienydd671972010-10-04 16:33:58 +0200134 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -0500135
136 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +0200137 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +0200138 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200139 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -0500140 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300141 * @param string $dir path to file
Andrey Andreev6c308b72012-02-02 22:03:53 +0200142 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -0500143 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200144 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500145 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100146 // filemtime() may return false, but raises an error for non-existing files
Ahmad Anbar00421bf2014-03-14 16:49:47 +0200147 $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200148
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200149 return array(
Andrey Andreev382b5132014-02-26 18:41:59 +0200150 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
151 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
152 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500153 }
154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // --------------------------------------------------------------------
156
157 /**
158 * Add Directory
159 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300160 * @param string $dir the directory name
161 * @param int $file_mtime
162 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * @return void
164 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200165 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200167 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
169 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500170 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
171 .pack('v', $file_mtime)
172 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 .pack('V', 0) // crc32
174 .pack('V', 0) // compressed filesize
175 .pack('V', 0) // uncompressed filesize
176 .pack('v', strlen($dir)) // length of pathname
177 .pack('v', 0) // extra field length
178 .$dir
179 // below is "data descriptor" segment
180 .pack('V', 0) // crc32
181 .pack('V', 0) // compressed filesize
182 .pack('V', 0); // uncompressed filesize
183
184 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500185 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
186 .pack('v', $file_mtime)
187 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 .pack('V',0) // crc32
189 .pack('V',0) // compressed filesize
190 .pack('V',0) // uncompressed filesize
191 .pack('v', strlen($dir)) // length of pathname
192 .pack('v', 0) // extra field length
193 .pack('v', 0) // file comment length
194 .pack('v', 0) // disk number start
195 .pack('v', 0) // internal file attributes
196 .pack('V', 16) // external file attributes - 'directory' bit set
197 .pack('V', $this->offset) // relative offset of local header
198 .$dir;
199
200 $this->offset = strlen($this->zipdata);
201 $this->entries++;
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // --------------------------------------------------------------------
205
206 /**
207 * Add Data to Zip
208 *
209 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200210 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 * sure you use add_dir() first to create the folder.
212 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300213 * @param mixed $filepath A single filepath or an array of file => data pairs
214 * @param string $data Single file contents
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200216 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200217 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 if (is_array($filepath))
220 {
221 foreach ($filepath as $path => $data)
222 {
Barry Mienydd671972010-10-04 16:33:58 +0200223 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500224 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226 }
227 else
228 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500229 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500230 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Add Data to Zip
238 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300239 * @param string $filepath the file name/path
240 * @param string $data the data to be encoded
241 * @param int $file_mtime
242 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200244 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200245 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200247 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500250 $crc32 = crc32($data);
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200251 $gzdata = substr(gzcompress($data), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 $compressed_size = strlen($gzdata);
253
254 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500255 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
256 .pack('v', $file_mtime)
257 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 .pack('V', $crc32)
259 .pack('V', $compressed_size)
260 .pack('V', $uncompressed_size)
261 .pack('v', strlen($filepath)) // length of filename
262 .pack('v', 0) // extra field length
263 .$filepath
264 .$gzdata; // "file data" segment
265
266 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500267 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
268 .pack('v', $file_mtime)
269 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 .pack('V', $crc32)
271 .pack('V', $compressed_size)
272 .pack('V', $uncompressed_size)
273 .pack('v', strlen($filepath)) // length of filename
274 .pack('v', 0) // extra field length
275 .pack('v', 0) // file comment length
276 .pack('v', 0) // disk number start
277 .pack('v', 0) // internal file attributes
278 .pack('V', 32) // external file attributes - 'archive' bit set
279 .pack('V', $this->offset) // relative offset of local header
280 .$filepath;
281
282 $this->offset = strlen($this->zipdata);
283 $this->entries++;
284 $this->file_num++;
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // --------------------------------------------------------------------
288
289 /**
290 * Read the contents of a file and add it to the zip
291 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300292 * @param string $path
Andrey Andreeva20ec972014-01-07 15:23:10 +0200293 * @param bool $archive_filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200295 */
Andrey Andreeva20ec972014-01-07 15:23:10 +0200296 public function read_file($path, $archive_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200298 if (file_exists($path) && FALSE !== ($data = file_get_contents($path)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200300 if (is_string($archive_filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200302 $name = str_replace('\\', '/', $archive_filepath);
303 }
304 else
305 {
306 $name = str_replace('\\', '/', $path);
307
Andrey Andreevaa9a4f72014-01-28 12:05:51 +0200308 if ($archive_filepath === FALSE)
Andrey Andreeva20ec972014-01-07 15:23:10 +0200309 {
310 $name = preg_replace('|.*/(.+)|', '\\1', $name);
311 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313
314 $this->add_data($name, $data);
315 return TRUE;
316 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 return FALSE;
319 }
320
321 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
324 * Read a directory and add it to the zip.
325 *
326 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200327 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * is in the original file path will be recreated in the zip file.
329 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300330 * @param string $path path to source directory
331 * @param bool $preserve_filepath
332 * @param string $root_path
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100334 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200335 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100336 {
tschechnikerc8175be2012-04-04 14:47:30 +0300337 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
Derek Jones2735b3e2010-05-11 08:58:21 -0500338 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100340 return FALSE;
341 }
342
343 // Set the original directory root for child dir's to use as relative
344 if ($root_path === NULL)
345 {
tschechnikerc8175be2012-04-04 14:47:30 +0300346 $root_path = dirname($path).DIRECTORY_SEPARATOR;
Phil Sturgeon26872de2010-05-11 11:41:59 +0100347 }
348
349 while (FALSE !== ($file = readdir($fp)))
350 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200351 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100353 continue;
354 }
355
Andrey Andreev382b5132014-02-26 18:41:59 +0200356 if (is_dir($path.$file))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100357 {
tschechnikerc8175be2012-04-04 14:47:30 +0300358 $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100359 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200360 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100361 {
Tobias Tschech9664cc92012-04-04 15:22:33 +0300362 $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path);
Andrey Andreev16d80662012-01-20 13:26:49 +0200363 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200365 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200367
Andrey Andreev16d80662012-01-20 13:26:49 +0200368 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500371
Andrey Andreev6c308b72012-02-02 22:03:53 +0200372 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100373 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Get the Zip file
380 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200381 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200382 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200383 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200386 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 return FALSE;
389 }
390
Andrey Andreev0ea39292011-12-25 18:48:46 +0200391 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200392 .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
393 .pack('v', $this->entries) // total # of entries "on this disk"
394 .pack('v', $this->entries) // total # of entries overall
395 .pack('V', strlen($this->directory)) // size of central dir
396 .pack('V', strlen($this->zipdata)) // offset to start of central dir
397 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
401
402 /**
403 * Write File to the specified directory
404 *
405 * Lets you write a file
406 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300407 * @param string $filepath the file name
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200409 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200410 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200412 if ( ! ($fp = @fopen($filepath, 'w+b')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return FALSE;
415 }
416
Barry Mienydd671972010-10-04 16:33:58 +0200417 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200418
Andrey Andreev1f5090a2014-06-03 15:40:30 +0300419 for ($result = $written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200420 {
421 if (($result = fwrite($fp, substr($data, $written))) === FALSE)
422 {
423 break;
424 }
425 }
426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 flock($fp, LOCK_UN);
428 fclose($fp);
429
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200430 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Download
437 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300438 * @param string $filename the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200439 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200441 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200443 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 $filename .= '.zip';
446 }
447
Andrey Andreev119d8a72014-01-08 15:27:53 +0200448 get_instance()->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000449 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000450 $zip_content =& $get_zip;
451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 force_download($filename, $zip_content);
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Initialize Data
459 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200460 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * multiple zips with different data.
462 *
Andrew Podner4296a652012-12-17 07:51:15 -0500463 * @return CI_Zip
Barry Mienydd671972010-10-04 16:33:58 +0200464 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200465 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
467 $this->zipdata = '';
468 $this->directory = '';
469 $this->entries = 0;
470 $this->file_num = 0;
471 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200472 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475}
476
477/* End of file Zip.php */
Andrey Andreev0336dc22012-03-20 15:34:28 +0200478/* Location: ./system/libraries/Zip.php */