blob: 5d0f22ae7ef5ca81f6844005b4aeb65e22813ba5 [file] [log] [blame]
admina59c2a12006-10-01 19:02:11 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Zip Encoding Class</title>
admina59c2a12006-10-01 19:02:11 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../nav/nav.js"></script>
11<script type="text/javascript" src="../nav/prototype.lite.js"></script>
12<script type="text/javascript" src="../nav/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
admina59c2a12006-10-01 19:02:11 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
admina59c2a12006-10-01 19:02:11 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000036<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
admina59c2a12006-10-01 19:02:11 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
admina59c2a12006-10-01 19:02:11 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Zip Encoding Class
51</td>
Derek Allardbc030912007-06-24 18:25:29 +000052<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
admina59c2a12006-10-01 19:02:11 +000053</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>Zip Encoding Class</h1>
Derek Allardd2df9bc2007-04-15 17:41:17 +000065<p>CodeIgniter's Zip Encoding Class classes permit you to create Zip archives. Archives can be downloaded to your
admine95014f2006-10-01 21:44:52 +000066desktop or saved to a directory.</p>
admina59c2a12006-10-01 19:02:11 +000067
68
admin70d553b2006-10-01 19:20:24 +000069<h2>Initializing the Class</h2>
Derek Allardd2df9bc2007-04-15 17:41:17 +000070<p>Like most other classes in CodeIgniter, the Zip class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
admin70d553b2006-10-01 19:20:24 +000071
72<code>$this->load->library('zip');</code>
admine95014f2006-10-01 21:44:52 +000073<p>Once loaded, the Zip library object will be available using: <dfn>$this->zip</dfn></p>
adminb1fddc02006-10-09 21:29:07 +000074
admin70d553b2006-10-01 19:20:24 +000075
admine95014f2006-10-01 21:44:52 +000076<h2>Usage Example</h2>
admin70d553b2006-10-01 19:20:24 +000077
admine95014f2006-10-01 21:44:52 +000078<p>This example demonstrates how to compress a file, save it to a folder on your server, and download it to your desktop.</p>
79
80<code>
81$name = 'mydata1.txt';<br />
82$data = 'A Data String!';<br />
83<br />
84$this->zip->add_data($name, $data);<br />
85<br />
86// Write the zip file to a folder on your server. Name it "my_backup.zip"<br />
87$this->zip->archive('/path/to/directory/my_backup.zip');
88<br /><br />
89 // Download the file to your desktop. Name it "my_backup.zip"<br />
90$this->zip->download('my_backup.zip');
91</code>
92
admin78ce3cc2006-10-02 02:58:03 +000093<h1>Function Reference</h1>
admine95014f2006-10-01 21:44:52 +000094
95<h2>$this->zip->add_data()</h2>
96
97<p>Permits you to add data to the Zip archive. The first parameter must contain the name you would like
admin9f8243b2006-10-23 20:00:15 +000098given to the file, the second parameter must contain the file data as a string:</p>
admine95014f2006-10-01 21:44:52 +000099
100<code>
101$name = 'my_bio.txt';<br />
102$data = 'I was born in an elevator...';<br />
103<br />
104$this->zip->add_data($name, $data);
105</code>
106
admin9f8243b2006-10-23 20:00:15 +0000107<p>You are allowed multiple calls to this function in order to
admine95014f2006-10-01 21:44:52 +0000108add several files to your archive. Example:</p>
admin70d553b2006-10-01 19:20:24 +0000109
110<code>
111$name = 'mydata1.txt';<br />
112$data = 'A Data String!';<br />
113$this->zip->add_data($name, $data);<br />
114<br />
115$name = 'mydata2.txt';<br />
116$data = 'Another Data String!';<br />
117$this->zip->add_data($name, $data);<br />
admin70d553b2006-10-01 19:20:24 +0000118</code>
119
admine95014f2006-10-01 21:44:52 +0000120<p>Or you can pass multiple files using an array:</p>
admin70d553b2006-10-01 19:20:24 +0000121
admin73fbde22006-10-01 19:26:15 +0000122<code>
123$data = array(<br />
admine95014f2006-10-01 21:44:52 +0000124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata1.txt' => 'A Data String!',<br />
admin9f8243b2006-10-23 20:00:15 +0000125&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata2.txt' => 'Another Data String!'<br />
admin73fbde22006-10-01 19:26:15 +0000126&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
127<br />
128$this->zip->add_data($data);<br />
129<br />
admine95014f2006-10-01 21:44:52 +0000130$this->zip->download('my_backup.zip');
admin73fbde22006-10-01 19:26:15 +0000131</code>
admina59c2a12006-10-01 19:02:11 +0000132
admine95014f2006-10-01 21:44:52 +0000133<p>If you would like your compressed data organized into sub-folders, include the path as part of the filename:</p>
134
135<code>
136$name = '<kbd>personal/</kbd>my_bio.txt';<br />
137$data = 'I was born in an elevator...';<br />
138<br />
139$this->zip->add_data($name, $data);
140</code>
141
142<p>The above example will place <dfn>my_bio.txt</dfn> inside a folder called <kbd>personal</kbd>.</p>
143
144
145<h2>$this->zip->add_dir()</h2>
146
147<p>Permits you to add a directory. Usually this function is unnecessary since you can place your data into folders when
148using <dfn>$this->zip->add_data()</dfn>, but if you would like to create an empty folder you can do so. Example:</p>
149
150<code>$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"</code>
151
152
admin9f8243b2006-10-23 20:00:15 +0000153
154<h2>$this->zip->read_file()</h2>
155
156<p>Permits you to compress a file that already exists somewhere on your server. Supply a file path and the zip class will
157read it and add it to the archive:</p>
158
159<code>
160$path = '/path/to/photo.jpg';<br /><br />
161$this->zip->read_file($path);
162<br /><br />
163 // Download the file to your desktop. Name it "my_backup.zip"<br />
164$this->zip->download('my_backup.zip');
165</code>
166
167<p>If you would like the Zip archive to maintain the directory structure the file is in, pass <kbd>TRUE</kbd> (boolean) in the
Derek Allardc6441282007-07-04 23:54:32 +0000168second parameter. Example:</p>
admin9f8243b2006-10-23 20:00:15 +0000169
170
171<code>
172$path = '/path/to/photo.jpg';<br /><br />
173$this->zip->read_file($path, <kbd>TRUE</kbd>);
174<br /><br />
175 // Download the file to your desktop. Name it "my_backup.zip"<br />
176$this->zip->download('my_backup.zip');
177</code>
178
179<p>In the above example, <dfn>photo.jpg</dfn> will be placed inside two folders: <kbd>path/to/</kbd></p>
180
181
182
183<h2>$this->zip->read_dir()</h2>
184
185<p>Permits you to compress a folder (and its contents) that already exists somewhere on your server. Supply a file path to the
186directory and the zip class will recursively read it and recreate it as a Zip archive. All files contained within the
Rick Ellis6afaff22007-07-12 23:07:48 +0000187supplied path will be encoded, as will any sub-folders contained within it. Example:</p>
admin9f8243b2006-10-23 20:00:15 +0000188
189<code>
190$path = '/path/to/your/directory/';<br /><br />
191$this->zip->read_dir($path);
192<br /><br />
193 // Download the file to your desktop. Name it "my_backup.zip"<br />
194$this->zip->download('my_backup.zip');
195</code>
196
197
198
199
admine95014f2006-10-01 21:44:52 +0000200<h2>$this->zip->archive()</h2>
201
202<p>Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. Make sure the
203directory is writable (666 or 777 is usually OK). Example:</p>
204
205<code>$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip</code>
206
207
208<h2>$this->zip->download()</h2>
209
210<p>Causes the Zip file to be downloaded to your server. The function must be passed the name you would like the zip file called.
211Example:</p>
212
213<code>$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"</code>
214
215<p class="important"><strong>Note:</strong>&nbsp; Do not display any data in the controller in which you call this function since it sends various server headers
216that cause the download to happen and the file to be treated as binary.</p>
217
218
219<h2>$this->zip->get_zip()</h2>
220
221<p>Returns the Zip-compressed file data. Generally you will not need this function unless you want to do something unique with the data.
222Example:</p>
223
224<code>
225$name = 'my_bio.txt';<br />
226$data = 'I was born in an elevator...';<br />
227<br />
228$this->zip->add_data($name, $data);<br /><br />
229
230$zip_file = $this->zip->get_zip();
231</code>
232
233
234<h2>$this->zip->clear_data()</h2>
235
236<p>The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each function you use above.
237If, however, you need to create multiple Zips, each with different data, you can clear the cache between calls. Example:</p>
238
239<code>
240$name = 'my_bio.txt';<br />
241$data = 'I was born in an elevator...';<br />
242<br />
243$this->zip->add_data($name, $data);<br />
244$zip_file = $this->zip->get_zip();<br />
245<br />
246<kbd>$this->zip->clear_data();</kbd>
247<br /><br />
248
249$name = 'photo.jpg';<br />
admin9f8243b2006-10-23 20:00:15 +0000250$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents<br />
admine95014f2006-10-01 21:44:52 +0000251<br /><br />
252$this->zip->download('myphotos.zip');
253</code>
254
255
256
257
258
259
260
261
262
admina59c2a12006-10-01 19:02:11 +0000263
264
265
266
267</div>
268<!-- END CONTENT -->
269
270
271<div id="footer">
272<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000273Previous Topic:&nbsp;&nbsp;<a href="xmlrpc.html"> XML-RPC Class</a>
admina59c2a12006-10-01 19:02:11 +0000274&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
275<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
276<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
277Next Topic:&nbsp;&nbsp;<a href="../helpers/array_helper.html">Array Helper</a>
Derek Allardc6441282007-07-04 23:54:32 +0000278</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000279<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
admina59c2a12006-10-01 19:02:11 +0000280</div>
281
282</body>
283</html>