blob: 0f255755e7cf2d58aadc9855fac4611b2d473414 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Zip Encoding Class
3##################
4
5CodeIgniter's Zip Encoding Class classes permit you to create Zip
6archives. Archives can be downloaded to your desktop or saved to a
7directory.
8
Andrey Andreev2c08e4e2013-09-23 15:20:21 +03009.. contents::
Andrey Andreevcc042092014-01-03 17:08:27 +020010 :local:
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030011
12.. raw:: html
13
Andrey Andreevcc042092014-01-03 17:08:27 +020014 <div class="custom-index container"></div>
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030015
16****************************
17Using the Zip Encoding Class
18****************************
19
Derek Jones8ede1a22011-10-05 13:34:52 -050020Initializing the Class
21======================
22
23Like most other classes in CodeIgniter, the Zip class is initialized in
24your controller using the $this->load->library function::
25
26 $this->load->library('zip');
27
Andrey Andreev309d7012014-12-04 11:47:26 +020028Once loaded, the Zip library object will be available using:
29
30 $this->zip
Derek Jones8ede1a22011-10-05 13:34:52 -050031
32Usage Example
33=============
34
35This example demonstrates how to compress a file, save it to a folder on
36your server, and download it to your desktop.
37
38::
39
Derek Jones526362d2011-10-05 15:23:43 -050040 $name = 'mydata1.txt';
41 $data = 'A Data String!';
42
43 $this->zip->add_data($name, $data);
44
45 // Write the zip file to a folder on your server. Name it "my_backup.zip"
46 $this->zip->archive('/path/to/directory/my_backup.zip');
47
48 // Download the file to your desktop. Name it "my_backup.zip"
49 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -050050
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030051***************
52Class Reference
53***************
Derek Jones8ede1a22011-10-05 13:34:52 -050054
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030055.. class:: CI_Zip
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreev309d7012014-12-04 11:47:26 +020057 .. attribute:: $compression_level = 2
58
59 The compression level to use.
60
61 It can range from 0 to 9, with 9 being the highest and 0 effectively disabling compression::
62
63 $this->zip->compression_level = 0;
64
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030065 .. method:: add_data($filepath[, $data = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Andrey Andreev28c2c972014-02-08 04:27:48 +020067 :param mixed $filepath: A single file path or an array of file => data pairs
68 :param array $data: File contents (ignored if $filepath is an array)
69 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -050070
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030071 Adds data to the Zip archive. Can work both in single and multiple files mode.
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev309d7012014-12-04 11:47:26 +020073 When adding a single file, the first parameter must contain the name you would
74 like given to the file and the second must contain the file contents::
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030076 $name = 'mydata1.txt';
77 $data = 'A Data String!';
78 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -050079
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030080 $name = 'mydata2.txt';
81 $data = 'Another Data String!';
82 $this->zip->add_data($name, $data);
83
Andrey Andreev309d7012014-12-04 11:47:26 +020084 When adding multiple files, the first parameter must contain *file => contents* pairs
85 and the second parameter is ignored::
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030087 $data = array(
88 'mydata1.txt' => 'A Data String!',
89 'mydata2.txt' => 'Another Data String!'
90 );
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030092 $this->zip->add_data($data);
Derek Jones526362d2011-10-05 15:23:43 -050093
Andrey Andreev309d7012014-12-04 11:47:26 +020094 If you would like your compressed data organized into sub-directories, simply include
95 the path as part of the filename(s)::
Derek Jones526362d2011-10-05 15:23:43 -050096
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030097 $name = 'personal/my_bio.txt';
98 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300100 $this->zip->add_data($name, $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300102 The above example will place my_bio.txt inside a folder called personal.
Derek Jones526362d2011-10-05 15:23:43 -0500103
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300104 .. method:: add_dir($directory)
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev28c2c972014-02-08 04:27:48 +0200106 :param mixed $directory: Directory name string or an array of multiple directories
107 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Andrey Andreev309d7012014-12-04 11:47:26 +0200109 Permits you to add a directory. Usually this method is unnecessary since you can place
110 your data into directories when using ``$this->zip->add_data()``, but if you would like
111 to create an empty directory you can do so::
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300113 $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreev45082b62014-01-07 15:28:17 +0200115 .. method:: read_file($path[, $archive_filepath = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreev28c2c972014-02-08 04:27:48 +0200117 :param string $path: Path to file
118 :param mixed $archive_filepath: New file name/path (string) or (boolean) whether to maintain the original filepath
119 :returns: TRUE on success, FALSE on failure
120 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300122 Permits you to compress a file that already exists somewhere on your server.
123 Supply a file path and the zip class will read it and add it to the archive::
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300125 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500126
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300127 $this->zip->read_file($path);
Derek Jones526362d2011-10-05 15:23:43 -0500128
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300129 // Download the file to your desktop. Name it "my_backup.zip"
130 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300132 If you would like the Zip archive to maintain the directory structure of
133 the file in it, pass TRUE (boolean) in the second parameter. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300135 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500136
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300137 $this->zip->read_file($path, TRUE);
Derek Jones526362d2011-10-05 15:23:43 -0500138
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300139 // Download the file to your desktop. Name it "my_backup.zip"
140 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300142 In the above example, photo.jpg will be placed into the *path/to/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreev45082b62014-01-07 15:28:17 +0200144 You can also specify a new name (path included) for the added file on the fly::
145
146 $path = '/path/to/photo.jpg';
147 $new_path = '/new/path/some_photo.jpg';
148
149 $this->zip->read_file($path, $new_path);
150
151 // Download ZIP archive containing /new/path/some_photo.jpg
152 $this->zip->download('my_archive.zip');
153
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300154 .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Andrey Andreev28c2c972014-02-08 04:27:48 +0200156 :param string $path: Path to directory
157 :param bool $preserve_filepath: Whether to maintain the original path
158 :param string $root_path: Part of the path to exclude from the archive directory
159 :returns: TRUE on success, FALSE on failure
160 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300162 Permits you to compress a directory (and its contents) that already exists somewhere on your server.
163 Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
164 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 -0500165
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300166 $path = '/path/to/your/directory/';
Derek Jones526362d2011-10-05 15:23:43 -0500167
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300168 $this->zip->read_dir($path);
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300170 // Download the file to your desktop. Name it "my_backup.zip"
171 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev309d7012014-12-04 11:47:26 +0200173 By default the Zip archive will place all directories listed in the first parameter
174 inside the zip. If you want the tree preceding the target directory to be ignored,
175 you can pass FALSE (boolean) in the second parameter. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500176
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300177 $path = '/path/to/your/directory/';
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300179 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreev309d7012014-12-04 11:47:26 +0200181 This will create a ZIP with a directory named "directory" inside, then all sub-directories
182 stored correctly inside that, but will not include the */path/to/your* part of the path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300184 .. method:: archive($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreev28c2c972014-02-08 04:27:48 +0200186 :param string $filepath: Path to target zip archive
187 :returns: TRUE on success, FALSE on failure
188 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreev309d7012014-12-04 11:47:26 +0200190 Writes the Zip-encoded file to a directory on your server. Submit a valid server path
191 ending in the file name. Make sure the directory is writable (755 is usually OK).
192 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300194 $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300196 .. method:: download($filename = 'backup.zip')
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreev28c2c972014-02-08 04:27:48 +0200198 :param string $filename: Archive file name
199 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
Andrey Andreev309d7012014-12-04 11:47:26 +0200201 Causes the Zip file to be downloaded from your server.
202 You must pass the name you would like the zip file called. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300204 $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300206 .. note:: Do not display any data in the controller in which you call
207 this method since it sends various server headers that cause the
208 download to happen and the file to be treated as binary.
Derek Jones526362d2011-10-05 15:23:43 -0500209
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300210 .. method:: get_zip()
Derek Jones526362d2011-10-05 15:23:43 -0500211
Andrey Andreev28c2c972014-02-08 04:27:48 +0200212 :returns: Zip file content
213 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Andrey Andreev309d7012014-12-04 11:47:26 +0200215 Returns the Zip-compressed file data. Generally you will not need this method unless you
216 want to do something unique with the data. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300218 $name = 'my_bio.txt';
219 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300221 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -0500222
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300223 $zip_file = $this->zip->get_zip();
Derek Jones526362d2011-10-05 15:23:43 -0500224
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300225 .. method:: clear_data()
Derek Jones526362d2011-10-05 15:23:43 -0500226
Andrey Andreev28c2c972014-02-08 04:27:48 +0200227 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -0500228
Andrey Andreev309d7012014-12-04 11:47:26 +0200229 The Zip class caches your zip data so that it doesn't need to recompile the Zip archive
230 for each method you use above. If, however, you need to create multiple Zip archives,
231 each with different data, you can clear the cache between calls. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500232
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300233 $name = 'my_bio.txt';
234 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300236 $this->zip->add_data($name, $data);
237 $zip_file = $this->zip->get_zip();
238
239 $this->zip->clear_data();
240
241 $name = 'photo.jpg';
242 $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
243
244 $this->zip->download('myphotos.zip');