blob: 3e98ac568912465a331ce7c85cb88ee9eee679b1 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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 Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @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 Andreevfe9309d2015-01-09 17:48:58 +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 Andreevfe9309d2015-01-09 17:48:58 +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 Andreevfe9309d2015-01-09 17:48:58 +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 Andreevfe9309d2015-01-09 17:48:58 +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 Andreevfe9309d2015-01-09 17:48:58 +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;
Andrey Andreev309d7012014-12-04 11:47:26 +020098
99 /**
100 * The level of compression
101 *
102 * Ranges from 0 to 9, with 9 being the highest level.
103 *
104 * @var int
105 */
106 public $compression_level = 2;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
Timothy Warren86611db2012-04-27 10:06:25 -0400108 /**
109 * Initialize zip compression class
110 *
Andrey Andreev56454792012-05-17 14:32:19 +0300111 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400112 */
Greg Akera9263282010-11-10 15:26:43 -0600113 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500115 $this->now = time();
Andrey Andreev90726b82015-01-20 12:39:22 +0200116 log_message('info', 'Zip Compression Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Add Directory
123 *
124 * Lets you add a virtual directory into which you can place files.
125 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300126 * @param mixed $directory the directory name. Can be string or array
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @return void
128 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200129 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev88f41df2013-09-23 15:24:43 +0300131 foreach ((array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200133 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 $dir .= '/';
136 }
137
Greg Aker5ed19b42010-03-19 12:13:14 -0500138 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -0500139 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141 }
142
Barry Mienydd671972010-10-04 16:33:58 +0200143 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -0500144
145 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +0200146 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +0200147 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200148 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -0500149 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300150 * @param string $dir path to file
Andrey Andreev6c308b72012-02-02 22:03:53 +0200151 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -0500152 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200153 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500154 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100155 // filemtime() may return false, but raises an error for non-existing files
Ahmad Anbar00421bf2014-03-14 16:49:47 +0200156 $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200157
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200158 return array(
Andrey Andreev382b5132014-02-26 18:41:59 +0200159 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
160 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
161 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500162 }
163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // --------------------------------------------------------------------
165
166 /**
167 * Add Directory
168 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300169 * @param string $dir the directory name
170 * @param int $file_mtime
171 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @return void
173 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200174 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200176 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177
178 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500179 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
180 .pack('v', $file_mtime)
181 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 .pack('V', 0) // crc32
183 .pack('V', 0) // compressed filesize
184 .pack('V', 0) // uncompressed filesize
185 .pack('v', strlen($dir)) // length of pathname
186 .pack('v', 0) // extra field length
187 .$dir
188 // below is "data descriptor" segment
189 .pack('V', 0) // crc32
190 .pack('V', 0) // compressed filesize
191 .pack('V', 0); // uncompressed filesize
192
193 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500194 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
195 .pack('v', $file_mtime)
196 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 .pack('V',0) // crc32
198 .pack('V',0) // compressed filesize
199 .pack('V',0) // uncompressed filesize
200 .pack('v', strlen($dir)) // length of pathname
201 .pack('v', 0) // extra field length
202 .pack('v', 0) // file comment length
203 .pack('v', 0) // disk number start
204 .pack('v', 0) // internal file attributes
205 .pack('V', 16) // external file attributes - 'directory' bit set
206 .pack('V', $this->offset) // relative offset of local header
207 .$dir;
208
209 $this->offset = strlen($this->zipdata);
210 $this->entries++;
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // --------------------------------------------------------------------
214
215 /**
216 * Add Data to Zip
217 *
218 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200219 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * sure you use add_dir() first to create the folder.
221 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300222 * @param mixed $filepath A single filepath or an array of file => data pairs
223 * @param string $data Single file contents
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200225 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200226 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if (is_array($filepath))
229 {
230 foreach ($filepath as $path => $data)
231 {
Barry Mienydd671972010-10-04 16:33:58 +0200232 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500233 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235 }
236 else
237 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500238 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500239 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Add Data to Zip
247 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300248 * @param string $filepath the file name/path
249 * @param string $data the data to be encoded
250 * @param int $file_mtime
251 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200253 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200254 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200256 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
258 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500259 $crc32 = crc32($data);
Andrey Andreev309d7012014-12-04 11:47:26 +0200260 $gzdata = substr(gzcompress($data, $this->compression_level), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $compressed_size = strlen($gzdata);
262
263 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500264 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
265 .pack('v', $file_mtime)
266 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 .pack('V', $crc32)
268 .pack('V', $compressed_size)
269 .pack('V', $uncompressed_size)
270 .pack('v', strlen($filepath)) // length of filename
271 .pack('v', 0) // extra field length
272 .$filepath
273 .$gzdata; // "file data" segment
274
275 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500276 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
277 .pack('v', $file_mtime)
278 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 .pack('V', $crc32)
280 .pack('V', $compressed_size)
281 .pack('V', $uncompressed_size)
282 .pack('v', strlen($filepath)) // length of filename
283 .pack('v', 0) // extra field length
284 .pack('v', 0) // file comment length
285 .pack('v', 0) // disk number start
286 .pack('v', 0) // internal file attributes
287 .pack('V', 32) // external file attributes - 'archive' bit set
288 .pack('V', $this->offset) // relative offset of local header
289 .$filepath;
290
291 $this->offset = strlen($this->zipdata);
292 $this->entries++;
293 $this->file_num++;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // --------------------------------------------------------------------
297
298 /**
299 * Read the contents of a file and add it to the zip
300 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300301 * @param string $path
Andrey Andreeva20ec972014-01-07 15:23:10 +0200302 * @param bool $archive_filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Andrey Andreeva20ec972014-01-07 15:23:10 +0200305 public function read_file($path, $archive_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200307 if (file_exists($path) && FALSE !== ($data = file_get_contents($path)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200309 if (is_string($archive_filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200311 $name = str_replace('\\', '/', $archive_filepath);
312 }
313 else
314 {
315 $name = str_replace('\\', '/', $path);
316
Andrey Andreevaa9a4f72014-01-28 12:05:51 +0200317 if ($archive_filepath === FALSE)
Andrey Andreeva20ec972014-01-07 15:23:10 +0200318 {
319 $name = preg_replace('|.*/(.+)|', '\\1', $name);
320 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322
323 $this->add_data($name, $data);
324 return TRUE;
325 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 return FALSE;
328 }
329
330 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 /**
333 * Read a directory and add it to the zip.
334 *
335 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200336 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * is in the original file path will be recreated in the zip file.
338 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300339 * @param string $path path to source directory
340 * @param bool $preserve_filepath
341 * @param string $root_path
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100343 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200344 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100345 {
tschechnikerc8175be2012-04-04 14:47:30 +0300346 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
Derek Jones2735b3e2010-05-11 08:58:21 -0500347 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100349 return FALSE;
350 }
351
352 // Set the original directory root for child dir's to use as relative
353 if ($root_path === NULL)
354 {
Kyle Gaddb7a8fbb2015-04-03 17:37:44 -0600355 $root_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, dirname($path)).DIRECTORY_SEPARATOR;
Phil Sturgeon26872de2010-05-11 11:41:59 +0100356 }
357
358 while (FALSE !== ($file = readdir($fp)))
359 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200360 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100362 continue;
363 }
364
Andrey Andreev382b5132014-02-26 18:41:59 +0200365 if (is_dir($path.$file))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100366 {
tschechnikerc8175be2012-04-04 14:47:30 +0300367 $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100368 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200369 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100370 {
Tobias Tschech9664cc92012-04-04 15:22:33 +0300371 $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path);
Andrey Andreev16d80662012-01-20 13:26:49 +0200372 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200374 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200376
Andrey Andreev16d80662012-01-20 13:26:49 +0200377 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500380
Andrey Andreev6c308b72012-02-02 22:03:53 +0200381 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100382 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Get the Zip file
389 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200390 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200392 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200395 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 return FALSE;
398 }
399
Andrey Andreev0ea39292011-12-25 18:48:46 +0200400 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200401 .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
402 .pack('v', $this->entries) // total # of entries "on this disk"
403 .pack('v', $this->entries) // total # of entries overall
404 .pack('V', strlen($this->directory)) // size of central dir
405 .pack('V', strlen($this->zipdata)) // offset to start of central dir
406 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
410
411 /**
412 * Write File to the specified directory
413 *
414 * Lets you write a file
415 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300416 * @param string $filepath the file name
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200418 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200419 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200421 if ( ! ($fp = @fopen($filepath, 'w+b')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 return FALSE;
424 }
425
Barry Mienydd671972010-10-04 16:33:58 +0200426 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200427
Andrey Andreev1f5090a2014-06-03 15:40:30 +0300428 for ($result = $written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200429 {
430 if (($result = fwrite($fp, substr($data, $written))) === FALSE)
431 {
432 break;
433 }
434 }
435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 flock($fp, LOCK_UN);
437 fclose($fp);
438
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200439 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Download
446 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300447 * @param string $filename the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200448 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200450 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200452 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 $filename .= '.zip';
455 }
456
Andrey Andreev119d8a72014-01-08 15:27:53 +0200457 get_instance()->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000458 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000459 $zip_content =& $get_zip;
460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 force_download($filename, $zip_content);
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Initialize Data
468 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200469 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * multiple zips with different data.
471 *
Andrew Podner4296a652012-12-17 07:51:15 -0500472 * @return CI_Zip
Barry Mienydd671972010-10-04 16:33:58 +0200473 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200474 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreevfe9309d2015-01-09 17:48:58 +0200476 $this->zipdata = '';
477 $this->directory = '';
478 $this->entries = 0;
479 $this->file_num = 0;
480 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200481 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484}