blob: 535aa82d9f13b00948e12dd1635e20ae6badfc10 [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 Andreev2c08e4e2013-09-23 15:20:21 +030057 :param mixed $filepath: a single file path or an array of file => data pairs
58 :param array $data: single file contents
59 :returns: 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 Andreev2c08e4e2013-09-23 15:20:21 +030093 :param mixed $directory: string directory name or an array of multiple directories
94 :returns: 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 Andreev45082b62014-01-07 15:28:17 +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
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300105 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300107 Permits you to compress a file that already exists somewhere on your server.
108 Supply a file path and the zip class will read it and add it to the archive::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300110 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500111
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300112 $this->zip->read_file($path);
Derek Jones526362d2011-10-05 15:23:43 -0500113
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300114 // Download the file to your desktop. Name it "my_backup.zip"
115 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300117 If you would like the Zip archive to maintain the directory structure of
118 the file in it, pass TRUE (boolean) in the second parameter. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300120 $path = '/path/to/photo.jpg';
Derek Jones526362d2011-10-05 15:23:43 -0500121
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300122 $this->zip->read_file($path, TRUE);
Derek Jones526362d2011-10-05 15:23:43 -0500123
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300124 // Download the file to your desktop. Name it "my_backup.zip"
125 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300127 In the above example, photo.jpg will be placed into the *path/to/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreev45082b62014-01-07 15:28:17 +0200129 You can also specify a new name (path included) for the added file on the fly::
130
131 $path = '/path/to/photo.jpg';
132 $new_path = '/new/path/some_photo.jpg';
133
134 $this->zip->read_file($path, $new_path);
135
136 // Download ZIP archive containing /new/path/some_photo.jpg
137 $this->zip->download('my_archive.zip');
138
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300139 .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300141 :param string $path: path to directory
142 :param bool $preserve_filepath: whether to maintain the original path
143 :param string $root_path: part of the path to exclude from the archive directory
144 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300146 Permits you to compress a directory (and its contents) that already exists somewhere on your server.
147 Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
148 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 -0500149
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300150 $path = '/path/to/your/directory/';
Derek Jones526362d2011-10-05 15:23:43 -0500151
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300152 $this->zip->read_dir($path);
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300154 // Download the file to your desktop. Name it "my_backup.zip"
155 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300157 By default the Zip archive will place all directories listed in the first parameter inside the zip.
158 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 -0500159
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300160 $path = '/path/to/your/directory/';
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300162 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300164 This will create a ZIP with a directory named "directory" inside, then all sub-directories stored correctly inside that, but will not include the
165 */path/to/your* part of the path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300167 .. method:: archive($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300169 :param string $filepath: path to target zip archive
170 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300172 Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name.
173 Make sure the directory is writable (660 or 666 is usually OK). Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300175 $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300177 .. method:: download($filename = 'backup.zip')
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300179 :param string $filename: the archive file name
180 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300182 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 -0500183
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300184 $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300186 .. note:: Do not display any data in the controller in which you call
187 this method since it sends various server headers that cause the
188 download to happen and the file to be treated as binary.
Derek Jones526362d2011-10-05 15:23:43 -0500189
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300190 .. method:: get_zip()
Derek Jones526362d2011-10-05 15:23:43 -0500191
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300192 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300194 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 -0500195
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300196 $name = 'my_bio.txt';
197 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300199 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -0500200
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300201 $zip_file = $this->zip->get_zip();
Derek Jones526362d2011-10-05 15:23:43 -0500202
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300203 .. method:: clear_data()
Derek Jones526362d2011-10-05 15:23:43 -0500204
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300205 :returns: void
Derek Jones526362d2011-10-05 15:23:43 -0500206
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300207 The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each method you use above.
208 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 -0500209
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300210 $name = 'my_bio.txt';
211 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300213 $this->zip->add_data($name, $data);
214 $zip_file = $this->zip->get_zip();
215
216 $this->zip->clear_data();
217
218 $name = 'photo.jpg';
219 $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
220
221 $this->zip->download('myphotos.zip');