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 | |
| 9 | Initializing the Class |
| 10 | ====================== |
| 11 | |
| 12 | Like most other classes in CodeIgniter, the Zip class is initialized in |
| 13 | your controller using the $this->load->library function:: |
| 14 | |
| 15 | $this->load->library('zip'); |
| 16 | |
| 17 | Once loaded, the Zip library object will be available using: $this->zip |
| 18 | |
| 19 | Usage Example |
| 20 | ============= |
| 21 | |
| 22 | This example demonstrates how to compress a file, save it to a folder on |
| 23 | your server, and download it to your desktop. |
| 24 | |
| 25 | :: |
| 26 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 27 | $name = 'mydata1.txt'; |
| 28 | $data = 'A Data String!'; |
| 29 | |
| 30 | $this->zip->add_data($name, $data); |
| 31 | |
| 32 | // Write the zip file to a folder on your server. Name it "my_backup.zip" |
| 33 | $this->zip->archive('/path/to/directory/my_backup.zip'); |
| 34 | |
| 35 | // Download the file to your desktop. Name it "my_backup.zip" |
| 36 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
| 38 | ****************** |
| 39 | Function Reference |
| 40 | ****************** |
| 41 | |
| 42 | $this->zip->add_data() |
| 43 | ======================= |
| 44 | |
| 45 | Permits you to add data to the Zip archive. The first parameter must |
| 46 | contain the name you would like given to the file, the second parameter |
| 47 | must contain the file data as a string:: |
| 48 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 49 | $name = 'my_bio.txt'; |
| 50 | $data = 'I was born in an elevator...'; |
| 51 | |
| 52 | $this->zip->add_data($name, $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
| 54 | You are allowed multiple calls to this function in order to add several |
| 55 | files to your archive. Example:: |
| 56 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 57 | $name = 'mydata1.txt'; |
| 58 | $data = 'A Data String!'; |
| 59 | $this->zip->add_data($name, $data); |
| 60 | |
| 61 | $name = 'mydata2.txt'; |
| 62 | $data = 'Another Data String!'; |
| 63 | $this->zip->add_data($name, $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
| 65 | Or you can pass multiple files using an array:: |
| 66 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 67 | $data = array( |
| 68 | 'mydata1.txt' => 'A Data String!', |
| 69 | 'mydata2.txt' => 'Another Data String!' |
| 70 | ); |
| 71 | |
| 72 | $this->zip->add_data($data); |
| 73 | |
| 74 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
| 76 | If you would like your compressed data organized into sub-folders, |
| 77 | include the path as part of the filename:: |
| 78 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 79 | $name = 'personal/my_bio.txt'; |
| 80 | $data = 'I was born in an elevator...'; |
| 81 | |
| 82 | $this->zip->add_data($name, $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
| 84 | The above example will place my_bio.txt inside a folder called |
| 85 | personal. |
| 86 | |
| 87 | $this->zip->add_dir() |
| 88 | ====================== |
| 89 | |
| 90 | Permits you to add a directory. Usually this function is unnecessary |
| 91 | since you can place your data into folders when using |
| 92 | $this->zip->add_data(), but if you would like to create an empty folder |
| 93 | you can do so. Example:: |
| 94 | |
| 95 | $this->zip->add_dir('myfolder'); // Creates a folder called "myfolder" |
| 96 | |
| 97 | $this->zip->read_file() |
| 98 | ======================== |
| 99 | |
| 100 | Permits you to compress a file that already exists somewhere on your |
| 101 | server. Supply a file path and the zip class will read it and add it to |
| 102 | the archive:: |
| 103 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 104 | $path = '/path/to/photo.jpg'; |
| 105 | |
| 106 | $this->zip->read_file($path); |
| 107 | |
| 108 | // Download the file to your desktop. Name it "my_backup.zip" |
| 109 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | If you would like the Zip archive to maintain the directory structure of |
| 112 | the file in it, pass TRUE (boolean) in the second parameter. Example:: |
| 113 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 114 | $path = '/path/to/photo.jpg'; |
| 115 | |
| 116 | $this->zip->read_file($path, TRUE); |
| 117 | |
| 118 | // Download the file to your desktop. Name it "my_backup.zip" |
| 119 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
| 121 | In the above example, photo.jpg will be placed inside two folders: |
| 122 | path/to/ |
| 123 | |
| 124 | $this->zip->read_dir() |
| 125 | ======================= |
| 126 | |
| 127 | Permits you to compress a folder (and its contents) that already exists |
| 128 | somewhere on your server. Supply a file path to the directory and the |
| 129 | zip class will recursively read it and recreate it as a Zip archive. All |
| 130 | files contained within the supplied path will be encoded, as will any |
| 131 | sub-folders contained within it. Example:: |
| 132 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 133 | $path = '/path/to/your/directory/'; |
| 134 | |
| 135 | $this->zip->read_dir($path); |
| 136 | |
| 137 | // Download the file to your desktop. Name it "my_backup.zip" |
| 138 | $this->zip->download('my_backup.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
| 140 | By default the Zip archive will place all directories listed in the |
| 141 | first parameter inside the zip. If you want the tree preceding the |
| 142 | target folder to be ignored you can pass FALSE (boolean) in the second |
| 143 | parameter. Example:: |
| 144 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 145 | $path = '/path/to/your/directory/'; |
| 146 | |
| 147 | $this->zip->read_dir($path, FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
| 149 | This will create a ZIP with the folder "directory" inside, then all |
| 150 | sub-folders stored correctly inside that, but will not include the |
| 151 | folders /path/to/your. |
| 152 | |
| 153 | $this->zip->archive() |
| 154 | ===================== |
| 155 | |
| 156 | Writes the Zip-encoded file to a directory on your server. Submit a |
| 157 | valid server path ending in the file name. Make sure the directory is |
| 158 | writable (666 or 777 is usually OK). Example:: |
| 159 | |
| 160 | $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip |
| 161 | |
| 162 | $this->zip->download() |
| 163 | ====================== |
| 164 | |
| 165 | Causes the Zip file to be downloaded from your server. The function must |
| 166 | be passed the name you would like the zip file called. Example:: |
| 167 | |
| 168 | $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip" |
| 169 | |
| 170 | .. note:: Do not display any data in the controller in which you call |
| 171 | this function since it sends various server headers that cause the |
| 172 | download to happen and the file to be treated as binary. |
| 173 | |
| 174 | $this->zip->get_zip() |
| 175 | ====================== |
| 176 | |
| 177 | Returns the Zip-compressed file data. Generally you will not need this |
| 178 | function unless you want to do something unique with the data. Example:: |
| 179 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 180 | $name = 'my_bio.txt'; |
| 181 | $data = 'I was born in an elevator...'; |
| 182 | |
| 183 | $this->zip->add_data($name, $data); |
| 184 | |
| 185 | $zip_file = $this->zip->get_zip(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |
| 187 | $this->zip->clear_data() |
| 188 | ========================= |
| 189 | |
| 190 | The Zip class caches your zip data so that it doesn't need to recompile |
| 191 | the Zip archive for each function you use above. If, however, you need |
| 192 | to create multiple Zips, each with different data, you can clear the |
| 193 | cache between calls. Example:: |
| 194 | |
Derek Jones | 526362d | 2011-10-05 15:23:43 -0500 | [diff] [blame] | 195 | $name = 'my_bio.txt'; |
| 196 | $data = 'I was born in an elevator...'; |
| 197 | |
| 198 | $this->zip->add_data($name, $data); |
| 199 | $zip_file = $this->zip->get_zip(); |
| 200 | |
| 201 | $this->zip->clear_data(); |
| 202 | |
| 203 | $name = 'photo.jpg'; |
| 204 | $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents |
| 205 | |
| 206 | |
| 207 | $this->zip->download('myphotos.zip'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |