blob: 9704d5b1d81749674af0dae5252d65e95c24ad23 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Zip Encoding Class
3##################
4
Andrey Andreev9a0e6602015-01-19 14:54:08 +02005CodeIgniter's Zip Encoding Class permits you to create Zip archives.
6Archives can be downloaded to your desktop or saved to a directory.
Derek Jones8ede1a22011-10-05 13:34:52 -05007
Andrey Andreev2c08e4e2013-09-23 15:20:21 +03008.. contents::
Andrey Andreevcc042092014-01-03 17:08:27 +02009 :local:
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030010
11.. raw:: html
12
Andrey Andreevcc042092014-01-03 17:08:27 +020013 <div class="custom-index container"></div>
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030014
15****************************
16Using the Zip Encoding Class
17****************************
18
Derek Jones8ede1a22011-10-05 13:34:52 -050019Initializing the Class
20======================
21
22Like most other classes in CodeIgniter, the Zip class is initialized in
23your controller using the $this->load->library function::
24
25 $this->load->library('zip');
26
Andrey Andreev83dfab92016-03-17 18:09:50 +020027Once loaded, the Zip library object will be available using::
Andrey Andreev309d7012014-12-04 11:47:26 +020028
29 $this->zip
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31Usage Example
32=============
33
34This example demonstrates how to compress a file, save it to a folder on
35your server, and download it to your desktop.
36
37::
38
Derek Jones526362d2011-10-05 15:23:43 -050039 $name = 'mydata1.txt';
40 $data = 'A Data String!';
41
42 $this->zip->add_data($name, $data);
43
44 // Write the zip file to a folder on your server. Name it "my_backup.zip"
David Wosnitzad31a4e62014-12-12 16:35:35 +010045 $this->zip->archive('/path/to/directory/my_backup.zip');
Derek Jones526362d2011-10-05 15:23:43 -050046
47 // Download the file to your desktop. Name it "my_backup.zip"
48 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030050***************
51Class Reference
52***************
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020054.. php:class:: CI_Zip
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Andrey Andreev309d7012014-12-04 11:47:26 +020056 .. attribute:: $compression_level = 2
57
58 The compression level to use.
59
60 It can range from 0 to 9, with 9 being the highest and 0 effectively disabling compression::
61
62 $this->zip->compression_level = 0;
63
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020064 .. php:method:: add_data($filepath[, $data = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Andrey Andreev28c2c972014-02-08 04:27:48 +020066 :param mixed $filepath: A single file path or an array of file => data pairs
67 :param array $data: File contents (ignored if $filepath is an array)
68 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -050069
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030070 Adds data to the Zip archive. Can work both in single and multiple files mode.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
Andrey Andreev309d7012014-12-04 11:47:26 +020072 When adding a single file, the first parameter must contain the name you would
73 like given to the file and the second must contain the file contents::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030075 $name = 'mydata1.txt';
76 $data = 'A Data String!';
77 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -050078
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030079 $name = 'mydata2.txt';
80 $data = 'Another Data String!';
81 $this->zip->add_data($name, $data);
David Wosnitzad31a4e62014-12-12 16:35:35 +010082
Andrey Andreev309d7012014-12-04 11:47:26 +020083 When adding multiple files, the first parameter must contain *file => contents* pairs
84 and the second parameter is ignored::
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030086 $data = array(
87 'mydata1.txt' => 'A Data String!',
88 'mydata2.txt' => 'Another Data String!'
89 );
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030091 $this->zip->add_data($data);
Derek Jones526362d2011-10-05 15:23:43 -050092
Andrey Andreev309d7012014-12-04 11:47:26 +020093 If you would like your compressed data organized into sub-directories, simply include
94 the path as part of the filename(s)::
Derek Jones526362d2011-10-05 15:23:43 -050095
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030096 $name = 'personal/my_bio.txt';
97 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030099 $this->zip->add_data($name, $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300101 The above example will place my_bio.txt inside a folder called personal.
Derek Jones526362d2011-10-05 15:23:43 -0500102
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200103 .. php:method:: add_dir($directory)
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreev28c2c972014-02-08 04:27:48 +0200105 :param mixed $directory: Directory name string or an array of multiple directories
106 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev309d7012014-12-04 11:47:26 +0200108 Permits you to add a directory. Usually this method is unnecessary since you can place
109 your data into directories when using ``$this->zip->add_data()``, but if you would like
110 to create an empty directory you can do so::
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300112 $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200114 .. php:method:: read_file($path[, $archive_filepath = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev28c2c972014-02-08 04:27:48 +0200116 :param string $path: Path to file
117 :param mixed $archive_filepath: New file name/path (string) or (boolean) whether to maintain the original filepath
118 :returns: TRUE on success, FALSE on failure
119 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300121 Permits you to compress a file that already exists somewhere on your server.
122 Supply a file path and the zip class will read it and add it to the archive::
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300124 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500125
David Wosnitzad31a4e62014-12-12 16:35:35 +0100126 $this->zip->read_file($path);
Derek Jones526362d2011-10-05 15:23:43 -0500127
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300128 // Download the file to your desktop. Name it "my_backup.zip"
129 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300131 If you would like the Zip archive to maintain the directory structure of
132 the file in it, pass TRUE (boolean) in the second parameter. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300134 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500135
David Wosnitzad31a4e62014-12-12 16:35:35 +0100136 $this->zip->read_file($path, TRUE);
Derek Jones526362d2011-10-05 15:23:43 -0500137
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300138 // Download the file to your desktop. Name it "my_backup.zip"
139 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300141 In the above example, photo.jpg will be placed into the *path/to/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev45082b62014-01-07 15:28:17 +0200143 You can also specify a new name (path included) for the added file on the fly::
144
145 $path = '/path/to/photo.jpg';
146 $new_path = '/new/path/some_photo.jpg';
147
148 $this->zip->read_file($path, $new_path);
149
150 // Download ZIP archive containing /new/path/some_photo.jpg
151 $this->zip->download('my_archive.zip');
152
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200153 .. php:method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev28c2c972014-02-08 04:27:48 +0200155 :param string $path: Path to directory
156 :param bool $preserve_filepath: Whether to maintain the original path
157 :param string $root_path: Part of the path to exclude from the archive directory
158 :returns: TRUE on success, FALSE on failure
159 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300161 Permits you to compress a directory (and its contents) that already exists somewhere on your server.
162 Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
163 All files contained within the supplied path will be encoded, as will any sub-directories contained within it. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500164
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300165 $path = '/path/to/your/directory/';
Derek Jones526362d2011-10-05 15:23:43 -0500166
David Wosnitzad31a4e62014-12-12 16:35:35 +0100167 $this->zip->read_dir($path);
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300169 // Download the file to your desktop. Name it "my_backup.zip"
170 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev309d7012014-12-04 11:47:26 +0200172 By default the Zip archive will place all directories listed in the first parameter
173 inside the zip. If you want the tree preceding the target directory to be ignored,
David Wosnitzad31a4e62014-12-12 16:35:35 +0100174 you can pass FALSE (boolean) in the second parameter. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500175
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300176 $path = '/path/to/your/directory/';
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300178 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev309d7012014-12-04 11:47:26 +0200180 This will create a ZIP with a directory named "directory" inside, then all sub-directories
181 stored correctly inside that, but will not include the */path/to/your* part of the path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200183 .. php:method:: archive($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreev28c2c972014-02-08 04:27:48 +0200185 :param string $filepath: Path to target zip archive
186 :returns: TRUE on success, FALSE on failure
187 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev309d7012014-12-04 11:47:26 +0200189 Writes the Zip-encoded file to a directory on your server. Submit a valid server path
190 ending in the file name. Make sure the directory is writable (755 is usually OK).
191 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500192
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300193 $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200195 .. php:method:: download($filename = 'backup.zip')
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
Andrey Andreev28c2c972014-02-08 04:27:48 +0200197 :param string $filename: Archive file name
198 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
Andrey Andreev309d7012014-12-04 11:47:26 +0200200 Causes the Zip file to be downloaded from your server.
201 You must pass the name you would like the zip file called. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300203 $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300205 .. note:: Do not display any data in the controller in which you call
206 this method since it sends various server headers that cause the
207 download to happen and the file to be treated as binary.
Derek Jones526362d2011-10-05 15:23:43 -0500208
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200209 .. php:method:: get_zip()
Derek Jones526362d2011-10-05 15:23:43 -0500210
Andrey Andreev28c2c972014-02-08 04:27:48 +0200211 :returns: Zip file content
212 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Andrey Andreev309d7012014-12-04 11:47:26 +0200214 Returns the Zip-compressed file data. Generally you will not need this method unless you
215 want to do something unique with the data. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300217 $name = 'my_bio.txt';
218 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300220 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -0500221
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300222 $zip_file = $this->zip->get_zip();
Derek Jones526362d2011-10-05 15:23:43 -0500223
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200224 .. php:method:: clear_data()
Derek Jones526362d2011-10-05 15:23:43 -0500225
Andrey Andreev28c2c972014-02-08 04:27:48 +0200226 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -0500227
Andrey Andreev309d7012014-12-04 11:47:26 +0200228 The Zip class caches your zip data so that it doesn't need to recompile the Zip archive
229 for each method you use above. If, however, you need to create multiple Zip archives,
230 each with different data, you can clear the cache between calls. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500231
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300232 $name = 'my_bio.txt';
233 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300235 $this->zip->add_data($name, $data);
236 $zip_file = $this->zip->get_zip();
237
David Wosnitzad31a4e62014-12-12 16:35:35 +0100238 $this->zip->clear_data();
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300239
240 $name = 'photo.jpg';
241 $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
242
David Wosnitzad31a4e62014-12-12 16:35:35 +0100243 $this->zip->download('myphotos.zip');