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 | |
| 28 | Once loaded, the Zip library object will be available using: $this->zip |
| 29 | |
| 30 | Usage Example |
| 31 | ============= |
| 32 | |
| 33 | This example demonstrates how to compress a file, save it to a folder on |
| 34 | your server, and download it to your desktop. |
| 35 | |
| 36 | :: |
| 37 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 38 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 49 | *************** |
| 50 | Class Reference |
| 51 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 53 | .. class:: CI_Zip |
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 | .. method:: add_data($filepath[, $data = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 57 | :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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 60 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 61 | 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] | 62 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 63 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 65 | $name = 'mydata1.txt'; |
| 66 | $data = 'A Data String!'; |
| 67 | $this->zip->add_data($name, $data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 68 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 69 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 75 | $data = array( |
| 76 | 'mydata1.txt' => 'A Data String!', |
| 77 | 'mydata2.txt' => 'Another Data String!' |
| 78 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 80 | $this->zip->add_data($data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 81 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 82 | If you would like your compressed data organized into sub-directories, simply include the path as part of the filename(s):: |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 83 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 84 | $name = 'personal/my_bio.txt'; |
| 85 | $data = 'I was born in an elevator...'; |
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 | $this->zip->add_data($name, $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 89 | 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] | 90 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 91 | .. method:: add_dir($directory) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 93 | :param mixed $directory: Directory name string or an array of multiple directories |
| 94 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 96 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 99 | $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Andrey Andreev | 45082b6 | 2014-01-07 15:28:17 +0200 | [diff] [blame] | 101 | .. method:: read_file($path[, $archive_filepath = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 103 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 108 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 111 | $path = '/path/to/photo.jpg'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 113 | $this->zip->read_file($path); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 115 | // Download the file to your desktop. Name it "my_backup.zip" |
| 116 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 118 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 121 | $path = '/path/to/photo.jpg'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 123 | $this->zip->read_file($path, TRUE); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 124 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 125 | // Download the file to your desktop. Name it "my_backup.zip" |
| 126 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 128 | 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] | 129 | |
Andrey Andreev | 45082b6 | 2014-01-07 15:28:17 +0200 | [diff] [blame] | 130 | 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 Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 140 | .. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 142 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 148 | 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 151 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 152 | $path = '/path/to/your/directory/'; |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 153 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 154 | $this->zip->read_dir($path); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 155 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 156 | // Download the file to your desktop. Name it "my_backup.zip" |
| 157 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 159 | 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 161 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 162 | $path = '/path/to/your/directory/'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 164 | $this->zip->read_dir($path, FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 166 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 169 | .. method:: archive($filepath) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 171 | :param string $filepath: Path to target zip archive |
| 172 | :returns: TRUE on success, FALSE on failure |
| 173 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 175 | Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 176 | Make sure the directory is writable (755 is usually OK). Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 178 | $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] | 179 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 180 | .. method:: download($filename = 'backup.zip') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 182 | :param string $filename: Archive file name |
| 183 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 184 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 185 | Causes the Zip file to be downloaded from your server. 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] | 186 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 187 | $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] | 188 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 189 | .. 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 192 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 193 | .. method:: get_zip() |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 194 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 195 | :returns: Zip file content |
| 196 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 197 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 198 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 199 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 200 | $name = 'my_bio.txt'; |
| 201 | $data = 'I was born in an elevator...'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 203 | $this->zip->add_data($name, $data); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 204 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 205 | $zip_file = $this->zip->get_zip(); |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 206 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 207 | .. method:: clear_data() |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 208 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 209 | :rtype: void |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 210 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 211 | 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 Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 213 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 214 | $name = 'my_bio.txt'; |
| 215 | $data = 'I was born in an elevator...'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 216 | |
Andrey Andreev | 2c08e4e | 2013-09-23 15:20:21 +0300 | [diff] [blame] | 217 | $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'); |