blob: 43abfba42202350bea9922dd97a77003a3138984 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0ea39292011-12-25 18:48:46 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0ea39292011-12-25 18:48:46 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreev1841e6b2012-01-24 15:30:01 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Zip Compression Class
31 *
32 * This class is based on a library I found at Zend:
33 * http://www.zend.com/codex.php?id=696&single=1
34 *
35 * The original library is a little rough around the edges so I
36 * refactored it and added several additional methods -- Rick Ellis
37 *
38 * @package CodeIgniter
39 * @subpackage Libraries
40 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050041 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @link http://codeigniter.com/user_guide/libraries/zip.html
43 */
vkeranov95b2f4f2012-06-16 20:37:11 +030044class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000045
Timothy Warren86611db2012-04-27 10:06:25 -040046 /**
47 * Zip data in string form
48 *
49 * @var string
50 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020051 public $zipdata = '';
Andrey Andreev56454792012-05-17 14:32:19 +030052
Timothy Warren86611db2012-04-27 10:06:25 -040053 /**
54 * Zip data for a directory in string form
55 *
56 * @var string
57 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020058 public $directory = '';
Andrey Andreev56454792012-05-17 14:32:19 +030059
Timothy Warren86611db2012-04-27 10:06:25 -040060 /**
61 * Number of files/folder in zip file
62 *
63 * @var int
64 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020065 public $entries = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren86611db2012-04-27 10:06:25 -040067 /**
68 * Number of files in zip
69 *
70 * @var int
71 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020072 public $file_num = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren86611db2012-04-27 10:06:25 -040074 /**
75 * relative offset of local header
76 *
77 * @var int
78 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020079 public $offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren86611db2012-04-27 10:06:25 -040081 /**
82 * Reference to time at init
83 *
84 * @var int
85 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020086 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Timothy Warren86611db2012-04-27 10:06:25 -040088 /**
89 * Initialize zip compression class
90 *
Andrey Andreev56454792012-05-17 14:32:19 +030091 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -040092 */
Greg Akera9263282010-11-10 15:26:43 -060093 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Greg Aker5ed19b42010-03-19 12:13:14 -050095 $this->now = time();
Andrey Andreevbb2e5182012-03-26 13:58:12 +030096 log_message('debug', 'Zip Compression Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Add Directory
103 *
104 * Lets you add a virtual directory into which you can place files.
105 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300106 * @param mixed $directory the directory name. Can be string or array
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return void
108 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200109 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreev88f41df2013-09-23 15:24:43 +0300111 foreach ((array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200113 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
115 $dir .= '/';
116 }
117
Greg Aker5ed19b42010-03-19 12:13:14 -0500118 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -0500119 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121 }
122
Barry Mienydd671972010-10-04 16:33:58 +0200123 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -0500124
125 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +0200126 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +0200127 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200128 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -0500129 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300130 * @param string $dir path to file
Andrey Andreev6c308b72012-02-02 22:03:53 +0200131 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -0500132 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200133 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500134 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100135 // filemtime() may return false, but raises an error for non-existing files
Andrey Andreev382b5132014-02-26 18:41:59 +0200136 $date = file_exists($dir) ? filemtime($dir) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200137
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200138 return array(
Andrey Andreev382b5132014-02-26 18:41:59 +0200139 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
140 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
141 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500142 }
143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
145
146 /**
147 * Add Directory
148 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300149 * @param string $dir the directory name
150 * @param int $file_mtime
151 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @return void
153 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200154 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200155 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200156 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157
158 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500159 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
160 .pack('v', $file_mtime)
161 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 .pack('V', 0) // crc32
163 .pack('V', 0) // compressed filesize
164 .pack('V', 0) // uncompressed filesize
165 .pack('v', strlen($dir)) // length of pathname
166 .pack('v', 0) // extra field length
167 .$dir
168 // below is "data descriptor" segment
169 .pack('V', 0) // crc32
170 .pack('V', 0) // compressed filesize
171 .pack('V', 0); // uncompressed filesize
172
173 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500174 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
175 .pack('v', $file_mtime)
176 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 .pack('V',0) // crc32
178 .pack('V',0) // compressed filesize
179 .pack('V',0) // uncompressed filesize
180 .pack('v', strlen($dir)) // length of pathname
181 .pack('v', 0) // extra field length
182 .pack('v', 0) // file comment length
183 .pack('v', 0) // disk number start
184 .pack('v', 0) // internal file attributes
185 .pack('V', 16) // external file attributes - 'directory' bit set
186 .pack('V', $this->offset) // relative offset of local header
187 .$dir;
188
189 $this->offset = strlen($this->zipdata);
190 $this->entries++;
191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // --------------------------------------------------------------------
194
195 /**
196 * Add Data to Zip
197 *
198 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200199 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * sure you use add_dir() first to create the folder.
201 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300202 * @param mixed $filepath A single filepath or an array of file => data pairs
203 * @param string $data Single file contents
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200206 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200207 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 if (is_array($filepath))
209 {
210 foreach ($filepath as $path => $data)
211 {
Barry Mienydd671972010-10-04 16:33:58 +0200212 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500213 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215 }
216 else
217 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500218 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500219 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Add Data to Zip
227 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300228 * @param string $filepath the file name/path
229 * @param string $data the data to be encoded
230 * @param int $file_mtime
231 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200234 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200236 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000237
238 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500239 $crc32 = crc32($data);
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200240 $gzdata = substr(gzcompress($data), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $compressed_size = strlen($gzdata);
242
243 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500244 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
245 .pack('v', $file_mtime)
246 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 .pack('V', $crc32)
248 .pack('V', $compressed_size)
249 .pack('V', $uncompressed_size)
250 .pack('v', strlen($filepath)) // length of filename
251 .pack('v', 0) // extra field length
252 .$filepath
253 .$gzdata; // "file data" segment
254
255 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500256 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
257 .pack('v', $file_mtime)
258 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 .pack('V', $crc32)
260 .pack('V', $compressed_size)
261 .pack('V', $uncompressed_size)
262 .pack('v', strlen($filepath)) // length of filename
263 .pack('v', 0) // extra field length
264 .pack('v', 0) // file comment length
265 .pack('v', 0) // disk number start
266 .pack('v', 0) // internal file attributes
267 .pack('V', 32) // external file attributes - 'archive' bit set
268 .pack('V', $this->offset) // relative offset of local header
269 .$filepath;
270
271 $this->offset = strlen($this->zipdata);
272 $this->entries++;
273 $this->file_num++;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // --------------------------------------------------------------------
277
278 /**
279 * Read the contents of a file and add it to the zip
280 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300281 * @param string $path
Andrey Andreeva20ec972014-01-07 15:23:10 +0200282 * @param bool $archive_filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200284 */
Andrey Andreeva20ec972014-01-07 15:23:10 +0200285 public function read_file($path, $archive_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200287 if (file_exists($path) && FALSE !== ($data = file_get_contents($path)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200289 if (is_string($archive_filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200291 $name = str_replace('\\', '/', $archive_filepath);
292 }
293 else
294 {
295 $name = str_replace('\\', '/', $path);
296
Andrey Andreevaa9a4f72014-01-28 12:05:51 +0200297 if ($archive_filepath === FALSE)
Andrey Andreeva20ec972014-01-07 15:23:10 +0200298 {
299 $name = preg_replace('|.*/(.+)|', '\\1', $name);
300 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
303 $this->add_data($name, $data);
304 return TRUE;
305 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 return FALSE;
308 }
309
310 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 /**
313 * Read a directory and add it to the zip.
314 *
315 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200316 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * is in the original file path will be recreated in the zip file.
318 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300319 * @param string $path path to source directory
320 * @param bool $preserve_filepath
321 * @param string $root_path
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100323 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200324 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100325 {
tschechnikerc8175be2012-04-04 14:47:30 +0300326 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
Derek Jones2735b3e2010-05-11 08:58:21 -0500327 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100329 return FALSE;
330 }
331
332 // Set the original directory root for child dir's to use as relative
333 if ($root_path === NULL)
334 {
tschechnikerc8175be2012-04-04 14:47:30 +0300335 $root_path = dirname($path).DIRECTORY_SEPARATOR;
Phil Sturgeon26872de2010-05-11 11:41:59 +0100336 }
337
338 while (FALSE !== ($file = readdir($fp)))
339 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200340 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100342 continue;
343 }
344
Andrey Andreev382b5132014-02-26 18:41:59 +0200345 if (is_dir($path.$file))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100346 {
tschechnikerc8175be2012-04-04 14:47:30 +0300347 $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100348 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200349 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100350 {
Tobias Tschech9664cc92012-04-04 15:22:33 +0300351 $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path);
Andrey Andreev16d80662012-01-20 13:26:49 +0200352 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200354 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200356
Andrey Andreev16d80662012-01-20 13:26:49 +0200357 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500360
Andrey Andreev6c308b72012-02-02 22:03:53 +0200361 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100362 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Get the Zip file
369 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200370 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200371 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200372 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200375 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 return FALSE;
378 }
379
Andrey Andreev0ea39292011-12-25 18:48:46 +0200380 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200381 .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
382 .pack('v', $this->entries) // total # of entries "on this disk"
383 .pack('v', $this->entries) // total # of entries overall
384 .pack('V', strlen($this->directory)) // size of central dir
385 .pack('V', strlen($this->zipdata)) // offset to start of central dir
386 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // --------------------------------------------------------------------
390
391 /**
392 * Write File to the specified directory
393 *
394 * Lets you write a file
395 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300396 * @param string $filepath the file name
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200398 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200399 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
402 {
403 return FALSE;
404 }
405
Barry Mienydd671972010-10-04 16:33:58 +0200406 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200407
408 for ($written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result)
409 {
410 if (($result = fwrite($fp, substr($data, $written))) === FALSE)
411 {
412 break;
413 }
414 }
415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 flock($fp, LOCK_UN);
417 fclose($fp);
418
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200419 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Download
426 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300427 * @param string $filename the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200428 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200430 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200432 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $filename .= '.zip';
435 }
436
Andrey Andreev119d8a72014-01-08 15:27:53 +0200437 get_instance()->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000438 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000439 $zip_content =& $get_zip;
440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 force_download($filename, $zip_content);
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Initialize Data
448 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200449 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * multiple zips with different data.
451 *
Andrew Podner4296a652012-12-17 07:51:15 -0500452 * @return CI_Zip
Barry Mienydd671972010-10-04 16:33:58 +0200453 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200454 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 $this->zipdata = '';
457 $this->directory = '';
458 $this->entries = 0;
459 $this->file_num = 0;
460 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200461 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464}
465
466/* End of file Zip.php */
Andrey Andreev0336dc22012-03-20 15:34:28 +0200467/* Location: ./system/libraries/Zip.php */