blob: e0955e3d64c1fe7cce91b22e507c010dd2a937ec [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::
10 :local:
11
12.. raw:: html
13
14 <div class="custom-index container"></div>
15
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 Andreev2c08e4e2013-09-23 15:20:21 +0300101 .. method:: read_file($path[, $preserve_filepath = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300103 :param string $path: path to file
104 :param bool $preserve_filepath: whether to maintain the original filepath
105 :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 Andreev2c08e4e2013-09-23 15:20:21 +0300129 .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300131 :param string $path: path to directory
132 :param bool $preserve_filepath: whether to maintain the original path
133 :param string $root_path: part of the path to exclude from the archive directory
134 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300136 Permits you to compress a directory (and its contents) that already exists somewhere on your server.
137 Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
138 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 -0500139
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300140 $path = '/path/to/your/directory/';
Derek Jones526362d2011-10-05 15:23:43 -0500141
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300142 $this->zip->read_dir($path);
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300144 // Download the file to your desktop. Name it "my_backup.zip"
145 $this->zip->download('my_backup.zip');
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300147 By default the Zip archive will place all directories listed in the first parameter inside the zip.
148 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 -0500149
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300150 $path = '/path/to/your/directory/';
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300152 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300154 This will create a ZIP with a directory named "directory" inside, then all sub-directories stored correctly inside that, but will not include the
155 */path/to/your* part of the path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300157 .. method:: archive($filepath)
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300159 :param string $filepath: path to target zip archive
160 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300162 Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name.
163 Make sure the directory is writable (660 or 666 is usually OK). Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300165 $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300167 .. method:: download($filename = 'backup.zip')
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300169 :param string $filename: the archive file name
170 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300172 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 -0500173
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300174 $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300176 .. note:: Do not display any data in the controller in which you call
177 this method since it sends various server headers that cause the
178 download to happen and the file to be treated as binary.
Derek Jones526362d2011-10-05 15:23:43 -0500179
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300180 .. method:: get_zip()
Derek Jones526362d2011-10-05 15:23:43 -0500181
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300182 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300184 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 -0500185
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300186 $name = 'my_bio.txt';
187 $data = 'I was born in an elevator...';
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300189 $this->zip->add_data($name, $data);
Derek Jones526362d2011-10-05 15:23:43 -0500190
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300191 $zip_file = $this->zip->get_zip();
Derek Jones526362d2011-10-05 15:23:43 -0500192
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300193 .. method:: clear_data()
Derek Jones526362d2011-10-05 15:23:43 -0500194
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300195 :returns: void
Derek Jones526362d2011-10-05 15:23:43 -0500196
Andrey Andreev2c08e4e2013-09-23 15:20:21 +0300197 The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each method you use above.
198 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 -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);
204 $zip_file = $this->zip->get_zip();
205
206 $this->zip->clear_data();
207
208 $name = 'photo.jpg';
209 $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
210
211 $this->zip->download('myphotos.zip');