Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################## |
| 2 | Zip Encoding Class |
| 3 | ################## |
| 4 | |
| 5 | CodeIgniter's Zip Encoding Class classes permit you to create Zip |
| 6 | archives. Archives can be downloaded to your desktop or saved to a |
| 7 | directory. |
| 8 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 9 | .. contents:: |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 10 | :local: |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 11 | |
| 12 | .. raw:: html |
| 13 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 14 | <div class="custom-index container"></div> |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 15 | |
| 16 | **************************** |
| 17 | Using the Zip Encoding Class |
| 18 | **************************** |
| 19 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | Initializing the Class |
| 21 | ====================== |
| 22 | |
| 23 | Like most other classes in CodeIgniter, the Zip class is initialized in |
| 24 | your controller using the $this->load->library function:: |
| 25 | |
| 26 | $this->load->library('zip'); |
| 27 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 28 | Once loaded, the Zip library object will be available using: |
| 29 | |
| 30 | $this->zip |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
| 32 | Usage Example |
| 33 | ============= |
| 34 | |
| 35 | This example demonstrates how to compress a file, save it to a folder on |
| 36 | your server, and download it to your desktop. |
| 37 | |
| 38 | :: |
| 39 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 40 | $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" |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 46 | $this->zip->archive('/path/to/directory/my_backup.zip'); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 47 | |
| 48 | // Download the file to your desktop. Name it "my_backup.zip" |
| 49 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 51 | *************** |
| 52 | Class Reference |
| 53 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 55 | .. class:: CI_Zip |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 57 | .. 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 Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 65 | .. method:: add_data($filepath[, $data = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 67 | :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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 70 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 71 | Adds data to the Zip archive. Can work both in single and multiple files mode. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 73 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 76 | $name = 'mydata1.txt'; |
| 77 | $data = 'A Data String!'; |
| 78 | $this->zip->add_data($name, $data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 80 | $name = 'mydata2.txt'; |
| 81 | $data = 'Another Data String!'; |
| 82 | $this->zip->add_data($name, $data); |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 83 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 84 | When adding multiple files, the first parameter must contain *file => contents* pairs |
| 85 | and the second parameter is ignored:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 87 | $data = array( |
| 88 | 'mydata1.txt' => 'A Data String!', |
| 89 | 'mydata2.txt' => 'Another Data String!' |
| 90 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 92 | $this->zip->add_data($data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 93 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 94 | If you would like your compressed data organized into sub-directories, simply include |
| 95 | the path as part of the filename(s):: |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 97 | $name = 'personal/my_bio.txt'; |
| 98 | $data = 'I was born in an elevator...'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 100 | $this->zip->add_data($name, $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 102 | The above example will place my_bio.txt inside a folder called personal. |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 104 | .. method:: add_dir($directory) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 106 | :param mixed $directory: Directory name string or an array of multiple directories |
| 107 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 109 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 113 | $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 45082b6 | 2014-01-07 15:28:17 +0200 | [diff] [blame] | 115 | .. method:: read_file($path[, $archive_filepath = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 117 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 122 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 125 | $path = '/path/to/photo.jpg'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 126 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 127 | $this->zip->read_file($path); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 128 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 129 | // Download the file to your desktop. Name it "my_backup.zip" |
| 130 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 132 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 135 | $path = '/path/to/photo.jpg'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 136 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 137 | $this->zip->read_file($path, TRUE); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 138 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 139 | // Download the file to your desktop. Name it "my_backup.zip" |
| 140 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 142 | In the above example, photo.jpg will be placed into the *path/to/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Andrey Andreev | 45082b6 | 2014-01-07 15:28:17 +0200 | [diff] [blame] | 144 | 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 Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 154 | .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 155 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 156 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 162 | 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 166 | $path = '/path/to/your/directory/'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 167 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 168 | $this->zip->read_dir($path); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 170 | // Download the file to your desktop. Name it "my_backup.zip" |
| 171 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 173 | 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, |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 175 | you can pass FALSE (boolean) in the second parameter. Example:: |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 176 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 177 | $path = '/path/to/your/directory/'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 178 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 179 | $this->zip->read_dir($path, FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 181 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 184 | .. method:: archive($filepath) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 186 | :param string $filepath: Path to target zip archive |
| 187 | :returns: TRUE on success, FALSE on failure |
| 188 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 189 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 190 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 193 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 194 | $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 195 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 196 | .. method:: download($filename = 'backup.zip') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 197 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 198 | :param string $filename: Archive file name |
| 199 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 200 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 201 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 203 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 204 | $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 205 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 206 | .. 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 209 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 210 | .. method:: get_zip() |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 211 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 212 | :returns: Zip file content |
| 213 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 215 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 218 | $name = 'my_bio.txt'; |
| 219 | $data = 'I was born in an elevator...'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 221 | $this->zip->add_data($name, $data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 222 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 223 | $zip_file = $this->zip->get_zip(); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 224 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 225 | .. method:: clear_data() |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 226 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 227 | :rtype: void |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 228 | |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 229 | 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 232 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 233 | $name = 'my_bio.txt'; |
| 234 | $data = 'I was born in an elevator...'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 235 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 236 | $this->zip->add_data($name, $data); |
| 237 | $zip_file = $this->zip->get_zip(); |
| 238 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 239 | $this->zip->clear_data(); |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 240 | |
| 241 | $name = 'photo.jpg'; |
| 242 | $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents |
| 243 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 244 | $this->zip->download('myphotos.zip'); |