blob: 5208c9149ae5f21fe229a664b1b60b56f6b08507 [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;
garrettairf678e632014-11-18 17:09:31 -060098
99 /**
100 * The level of compression. 0 to 9, 9 being the highest level of
101 * compression.
102 * @var int
103 */
104 public $compression_level = 6;
105
106 /**
107 * Which encoding to use. One of the ZLIB_ENCODING_* constants.
108 * @var int
109 */
110 public $compression_encoding = ZLIB_ENCODING_DEFLATE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111
Timothy Warren86611db2012-04-27 10:06:25 -0400112 /**
113 * Initialize zip compression class
114 *
Andrey Andreev56454792012-05-17 14:32:19 +0300115 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400116 */
Greg Akera9263282010-11-10 15:26:43 -0600117 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500119 $this->now = time();
Andrey Andreevbb2e5182012-03-26 13:58:12 +0300120 log_message('debug', 'Zip Compression Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Add Directory
127 *
128 * Lets you add a virtual directory into which you can place files.
129 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300130 * @param mixed $directory the directory name. Can be string or array
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * @return void
132 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200133 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev88f41df2013-09-23 15:24:43 +0300135 foreach ((array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200137 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 $dir .= '/';
140 }
141
Greg Aker5ed19b42010-03-19 12:13:14 -0500142 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -0500143 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145 }
146
Barry Mienydd671972010-10-04 16:33:58 +0200147 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -0500148
149 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +0200150 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +0200151 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200152 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -0500153 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300154 * @param string $dir path to file
Andrey Andreev6c308b72012-02-02 22:03:53 +0200155 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -0500156 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200157 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500158 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100159 // filemtime() may return false, but raises an error for non-existing files
Ahmad Anbar00421bf2014-03-14 16:49:47 +0200160 $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200161
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200162 return array(
Andrey Andreev382b5132014-02-26 18:41:59 +0200163 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
164 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
165 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500166 }
167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // --------------------------------------------------------------------
169
170 /**
171 * Add Directory
172 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300173 * @param string $dir the directory name
174 * @param int $file_mtime
175 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @return void
177 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200178 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200179 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200180 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181
182 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500183 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
184 .pack('v', $file_mtime)
185 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 .pack('V', 0) // crc32
187 .pack('V', 0) // compressed filesize
188 .pack('V', 0) // uncompressed filesize
189 .pack('v', strlen($dir)) // length of pathname
190 .pack('v', 0) // extra field length
191 .$dir
192 // below is "data descriptor" segment
193 .pack('V', 0) // crc32
194 .pack('V', 0) // compressed filesize
195 .pack('V', 0); // uncompressed filesize
196
197 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500198 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
199 .pack('v', $file_mtime)
200 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 .pack('V',0) // crc32
202 .pack('V',0) // compressed filesize
203 .pack('V',0) // uncompressed filesize
204 .pack('v', strlen($dir)) // length of pathname
205 .pack('v', 0) // extra field length
206 .pack('v', 0) // file comment length
207 .pack('v', 0) // disk number start
208 .pack('v', 0) // internal file attributes
209 .pack('V', 16) // external file attributes - 'directory' bit set
210 .pack('V', $this->offset) // relative offset of local header
211 .$dir;
212
213 $this->offset = strlen($this->zipdata);
214 $this->entries++;
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
220 * Add Data to Zip
221 *
222 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200223 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * sure you use add_dir() first to create the folder.
225 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300226 * @param mixed $filepath A single filepath or an array of file => data pairs
227 * @param string $data Single file contents
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200229 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200230 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200231 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 if (is_array($filepath))
233 {
234 foreach ($filepath as $path => $data)
235 {
Barry Mienydd671972010-10-04 16:33:58 +0200236 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500237 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239 }
240 else
241 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500242 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500243 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Add Data to Zip
251 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300252 * @param string $filepath the file name/path
253 * @param string $data the data to be encoded
254 * @param int $file_mtime
255 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200257 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200258 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200260 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500263 $crc32 = crc32($data);
garrettairf678e632014-11-18 17:09:31 -0600264 $gzdata = substr(gzcompress($data, $this->compression_level, $this->compression_encoding), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $compressed_size = strlen($gzdata);
266
267 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500268 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
269 .pack('v', $file_mtime)
270 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 .pack('V', $crc32)
272 .pack('V', $compressed_size)
273 .pack('V', $uncompressed_size)
274 .pack('v', strlen($filepath)) // length of filename
275 .pack('v', 0) // extra field length
276 .$filepath
277 .$gzdata; // "file data" segment
278
279 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500280 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
281 .pack('v', $file_mtime)
282 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 .pack('V', $crc32)
284 .pack('V', $compressed_size)
285 .pack('V', $uncompressed_size)
286 .pack('v', strlen($filepath)) // length of filename
287 .pack('v', 0) // extra field length
288 .pack('v', 0) // file comment length
289 .pack('v', 0) // disk number start
290 .pack('v', 0) // internal file attributes
291 .pack('V', 32) // external file attributes - 'archive' bit set
292 .pack('V', $this->offset) // relative offset of local header
293 .$filepath;
294
295 $this->offset = strlen($this->zipdata);
296 $this->entries++;
297 $this->file_num++;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Read the contents of a file and add it to the zip
304 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300305 * @param string $path
Andrey Andreeva20ec972014-01-07 15:23:10 +0200306 * @param bool $archive_filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200308 */
Andrey Andreeva20ec972014-01-07 15:23:10 +0200309 public function read_file($path, $archive_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200311 if (file_exists($path) && FALSE !== ($data = file_get_contents($path)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200313 if (is_string($archive_filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200315 $name = str_replace('\\', '/', $archive_filepath);
316 }
317 else
318 {
319 $name = str_replace('\\', '/', $path);
320
Andrey Andreevaa9a4f72014-01-28 12:05:51 +0200321 if ($archive_filepath === FALSE)
Andrey Andreeva20ec972014-01-07 15:23:10 +0200322 {
323 $name = preg_replace('|.*/(.+)|', '\\1', $name);
324 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326
327 $this->add_data($name, $data);
328 return TRUE;
329 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 return FALSE;
332 }
333
334 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * Read a directory and add it to the zip.
338 *
339 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200340 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * is in the original file path will be recreated in the zip file.
342 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300343 * @param string $path path to source directory
344 * @param bool $preserve_filepath
345 * @param string $root_path
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100347 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200348 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100349 {
tschechnikerc8175be2012-04-04 14:47:30 +0300350 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
Derek Jones2735b3e2010-05-11 08:58:21 -0500351 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100353 return FALSE;
354 }
355
356 // Set the original directory root for child dir's to use as relative
357 if ($root_path === NULL)
358 {
tschechnikerc8175be2012-04-04 14:47:30 +0300359 $root_path = dirname($path).DIRECTORY_SEPARATOR;
Phil Sturgeon26872de2010-05-11 11:41:59 +0100360 }
361
362 while (FALSE !== ($file = readdir($fp)))
363 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200364 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100366 continue;
367 }
368
Andrey Andreev382b5132014-02-26 18:41:59 +0200369 if (is_dir($path.$file))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100370 {
tschechnikerc8175be2012-04-04 14:47:30 +0300371 $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100372 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200373 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100374 {
Tobias Tschech9664cc92012-04-04 15:22:33 +0300375 $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path);
Andrey Andreev16d80662012-01-20 13:26:49 +0200376 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200378 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200380
Andrey Andreev16d80662012-01-20 13:26:49 +0200381 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500384
Andrey Andreev6c308b72012-02-02 22:03:53 +0200385 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100386 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * Get the Zip file
393 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200394 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200395 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200396 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200399 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 return FALSE;
402 }
403
Andrey Andreev0ea39292011-12-25 18:48:46 +0200404 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200405 .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
406 .pack('v', $this->entries) // total # of entries "on this disk"
407 .pack('v', $this->entries) // total # of entries overall
408 .pack('V', strlen($this->directory)) // size of central dir
409 .pack('V', strlen($this->zipdata)) // offset to start of central dir
410 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * Write File to the specified directory
417 *
418 * Lets you write a file
419 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300420 * @param string $filepath the file name
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200422 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200423 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200425 if ( ! ($fp = @fopen($filepath, 'w+b')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 return FALSE;
428 }
429
Barry Mienydd671972010-10-04 16:33:58 +0200430 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200431
Andrey Andreev1f5090a2014-06-03 15:40:30 +0300432 for ($result = $written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200433 {
434 if (($result = fwrite($fp, substr($data, $written))) === FALSE)
435 {
436 break;
437 }
438 }
439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 flock($fp, LOCK_UN);
441 fclose($fp);
442
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200443 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * Download
450 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300451 * @param string $filename the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200452 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200454 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200456 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 $filename .= '.zip';
459 }
460
Andrey Andreev119d8a72014-01-08 15:27:53 +0200461 get_instance()->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000462 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000463 $zip_content =& $get_zip;
464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 force_download($filename, $zip_content);
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Initialize Data
472 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200473 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * multiple zips with different data.
475 *
Andrew Podner4296a652012-12-17 07:51:15 -0500476 * @return CI_Zip
Barry Mienydd671972010-10-04 16:33:58 +0200477 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200478 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 $this->zipdata = '';
481 $this->directory = '';
482 $this->entries = 0;
483 $this->file_num = 0;
484 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200485 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488}
489
490/* End of file Zip.php */
Andrey Andreev0336dc22012-03-20 15:34:28 +0200491/* Location: ./system/libraries/Zip.php */