blob: 5ff7d07d6ec9bf5a76afcfc5a9a5317eaae43f2f [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
28Once loaded, the Zip library object will be available using: $this->zip
29
30Usage Example
31=============
32
33This example demonstrates how to compress a file, save it to a folder on
34your server, and download it to your desktop.
35
36::
37
Derek Jones526362d2011-10-05 15:23:43 -050038 $name = 'mydata1.txt';
39 $data = 'A Data String!';
40
41 $this->zip->add_data($name, $data);
42
43 // Write the zip file to a folder on your server. Name it "my_backup.zip"
44 $this->zip->archive('/path/to/directory/my_backup.zip');
45
46 // Download the file to your desktop. Name it "my_backup.zip"
47 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030049***************
50Class Reference
51***************
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030053.. class:: CI_Zip
Derek Jones8ede1a22011-10-05 13:34:52 -050054
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030055 .. method:: add_data($filepath[, $data = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreev28c2c972014-02-08 04:27:48 +020057 :param mixed $filepath: A single file path or an array of file => data pairs
58 :param array $data: File contents (ignored if $filepath is an array)
59 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -050060
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030061 Adds data to the Zip archive. Can work both in single and multiple files mode.
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030063 When adding a single file, the first parameter must contain the name you would like given to the file and the second must contain the file contents::
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030065 $name = 'mydata1.txt';
66 $data = 'A Data String!';
67 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -050068
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030069 $name = 'mydata2.txt';
70 $data = 'Another Data String!';
71 $this->zip->add_data($name, $data);
72
73 When adding multiple files, the first parameter must contain *file => contents* pairs and the second parameter is ignored::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030075 $data = array(
76 'mydata1.txt' => 'A Data String!',
77 'mydata2.txt' => 'Another Data String!'
78 );
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030080 $this->zip->add_data($data);
Derek Jones526362d2011-10-05 15:23:43 -050081
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030082 If you would like your compressed data organized into sub-directories, simply include the path as part of the filename(s)::
Derek Jones526362d2011-10-05 15:23:43 -050083
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030084 $name = 'personal/my_bio.txt';
85 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030087 $this->zip->add_data($name, $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030089 The above example will place my_bio.txt inside a folder called personal.
Derek Jones526362d2011-10-05 15:23:43 -050090
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030091 .. method:: add_dir($directory)
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev28c2c972014-02-08 04:27:48 +020093 :param mixed $directory: Directory name string or an array of multiple directories
94 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050095
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030096 Permits you to add a directory. Usually this method is unnecessary since you can place your data into directories when using
97 ``$this->zip->add_data()``, but if you would like to create an empty directory you can do so::
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Andrey Andreev2c08e4e2013-09-23 15:20:21 +030099 $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreev45082b62014-01-07 15:28:17 +0200101 .. method:: read_file($path[, $archive_filepath = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Andrey Andreev28c2c972014-02-08 04:27:48 +0200103 :param string $path: Path to file
104 :param mixed $archive_filepath: New file name/path (string) or (boolean) whether to maintain the original filepath
105 :returns: TRUE on success, FALSE on failure
106 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300108 Permits you to compress a file that already exists somewhere on your server.
109 Supply a file path and the zip class will read it and add it to the archive::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300111 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500112
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300113 $this->zip->read_file($path);
Derek Jones526362d2011-10-05 15:23:43 -0500114
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300115 // Download the file to your desktop. Name it "my_backup.zip"
116 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300118 If you would like the Zip archive to maintain the directory structure of
119 the file in it, pass TRUE (boolean) in the second parameter. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300121 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500122
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300123 $this->zip->read_file($path, TRUE);
Derek Jones526362d2011-10-05 15:23:43 -0500124
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300125 // Download the file to your desktop. Name it "my_backup.zip"
126 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300128 In the above example, photo.jpg will be placed into the *path/to/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev45082b62014-01-07 15:28:17 +0200130 You can also specify a new name (path included) for the added file on the fly::
131
132 $path = '/path/to/photo.jpg';
133 $new_path = '/new/path/some_photo.jpg';
134
135 $this->zip->read_file($path, $new_path);
136
137 // Download ZIP archive containing /new/path/some_photo.jpg
138 $this->zip->download('my_archive.zip');
139
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300140 .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev28c2c972014-02-08 04:27:48 +0200142 :param string $path: Path to directory
143 :param bool $preserve_filepath: Whether to maintain the original path
144 :param string $root_path: Part of the path to exclude from the archive directory
145 :returns: TRUE on success, FALSE on failure
146 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300148 Permits you to compress a directory (and its contents) that already exists somewhere on your server.
149 Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
150 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 -0500151
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300152 $path = '/path/to/your/directory/';
Derek Jones526362d2011-10-05 15:23:43 -0500153
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300154 $this->zip->read_dir($path);
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300156 // Download the file to your desktop. Name it "my_backup.zip"
157 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300159 By default the Zip archive will place all directories listed in the first parameter inside the zip.
160 If you want the tree preceding the target directory to be ignored you can pass FALSE (boolean) in the second parameter. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500161
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300162 $path = '/path/to/your/directory/';
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300164 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300166 This will create a ZIP with a directory named "directory" inside, then all sub-directories stored correctly inside that, but will not include the
167 */path/to/your* part of the path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300169 .. method:: archive($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev28c2c972014-02-08 04:27:48 +0200171 :param string $filepath: Path to target zip archive
172 :returns: TRUE on success, FALSE on failure
173 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300175 Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name.
176 Make sure the directory is writable (660 or 666 is usually OK). Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300178 $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300180 .. method:: download($filename = 'backup.zip')
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev28c2c972014-02-08 04:27:48 +0200182 :param string $filename: Archive file name
183 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300185 Causes the Zip file to be downloaded from your server. You must pass the name you would like the zip file called. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300187 $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300189 .. note:: Do not display any data in the controller in which you call
190 this method since it sends various server headers that cause the
191 download to happen and the file to be treated as binary.
Derek Jones526362d2011-10-05 15:23:43 -0500192
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300193 .. method:: get_zip()
Derek Jones526362d2011-10-05 15:23:43 -0500194
Andrey Andreev28c2c972014-02-08 04:27:48 +0200195 :returns: Zip file content
196 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300198 Returns the Zip-compressed file data. Generally you will not need this method unless you want to do something unique with the data. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300200 $name = 'my_bio.txt';
201 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300203 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -0500204
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300205 $zip_file = $this->zip->get_zip();
Derek Jones526362d2011-10-05 15:23:43 -0500206
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300207 .. method:: clear_data()
Derek Jones526362d2011-10-05 15:23:43 -0500208
Andrey Andreev28c2c972014-02-08 04:27:48 +0200209 :rtype: void
Derek Jones526362d2011-10-05 15:23:43 -0500210
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300211 The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each method you use above.
212 If, however, you need to create multiple Zip archives, each with different data, you can clear the cache between calls. Example::
Derek Jones526362d2011-10-05 15:23:43 -0500213
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300214 $name = 'my_bio.txt';
215 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300217 $this->zip->add_data($name, $data);
218 $zip_file = $this->zip->get_zip();
219
220 $this->zip->clear_data();
221
222 $name = 'photo.jpg';
223 $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
224
225 $this->zip->download('myphotos.zip');