blob: c27718273530a3c6cdc0e901b7d33a9a1f4c5b0c [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
9Initializing the Class
10======================
11
12Like most other classes in CodeIgniter, the Zip class is initialized in
13your controller using the $this->load->library function::
14
15 $this->load->library('zip');
16
17Once loaded, the Zip library object will be available using: $this->zip
18
19Usage Example
20=============
21
22This example demonstrates how to compress a file, save it to a folder on
23your server, and download it to your desktop.
24
25::
26
Derek Jones526362d2011-10-05 15:23:43 -050027 $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 Jones8ede1a22011-10-05 13:34:52 -050037
38******************
39Function Reference
40******************
41
42$this->zip->add_data()
43=======================
44
45Permits you to add data to the Zip archive. The first parameter must
46contain the name you would like given to the file, the second parameter
47must contain the file data as a string::
48
Derek Jones526362d2011-10-05 15:23:43 -050049 $name = 'my_bio.txt';
50 $data = 'I was born in an elevator...';
51
52 $this->zip->add_data($name, $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050053
54You are allowed multiple calls to this function in order to add several
55files to your archive. Example::
56
Derek Jones526362d2011-10-05 15:23:43 -050057 $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 Jones8ede1a22011-10-05 13:34:52 -050064
65Or you can pass multiple files using an array::
66
Derek Jones526362d2011-10-05 15:23:43 -050067 $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 Jones8ede1a22011-10-05 13:34:52 -050075
76If you would like your compressed data organized into sub-folders,
77include the path as part of the filename::
78
Derek Jones526362d2011-10-05 15:23:43 -050079 $name = 'personal/my_bio.txt';
80 $data = 'I was born in an elevator...';
81
82 $this->zip->add_data($name, $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84The above example will place my_bio.txt inside a folder called
85personal.
86
87$this->zip->add_dir()
88======================
89
90Permits you to add a directory. Usually this function is unnecessary
91since you can place your data into folders when using
92$this->zip->add_data(), but if you would like to create an empty folder
93you can do so. Example::
94
95 $this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"
96
97$this->zip->read_file()
98========================
99
100Permits you to compress a file that already exists somewhere on your
101server. Supply a file path and the zip class will read it and add it to
102the archive::
103
Derek Jones526362d2011-10-05 15:23:43 -0500104 $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 Jones8ede1a22011-10-05 13:34:52 -0500110
111If you would like the Zip archive to maintain the directory structure of
112the file in it, pass TRUE (boolean) in the second parameter. Example::
113
Derek Jones526362d2011-10-05 15:23:43 -0500114 $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 Jones8ede1a22011-10-05 13:34:52 -0500120
121In the above example, photo.jpg will be placed inside two folders:
122path/to/
123
124$this->zip->read_dir()
125=======================
126
127Permits you to compress a folder (and its contents) that already exists
128somewhere on your server. Supply a file path to the directory and the
129zip class will recursively read it and recreate it as a Zip archive. All
130files contained within the supplied path will be encoded, as will any
131sub-folders contained within it. Example::
132
Derek Jones526362d2011-10-05 15:23:43 -0500133 $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 Jones8ede1a22011-10-05 13:34:52 -0500139
140By default the Zip archive will place all directories listed in the
141first parameter inside the zip. If you want the tree preceding the
142target folder to be ignored you can pass FALSE (boolean) in the second
143parameter. Example::
144
Derek Jones526362d2011-10-05 15:23:43 -0500145 $path = '/path/to/your/directory/';
146
147 $this->zip->read_dir($path, FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
149This will create a ZIP with the folder "directory" inside, then all
150sub-folders stored correctly inside that, but will not include the
151folders /path/to/your.
152
153$this->zip->archive()
154=====================
155
156Writes the Zip-encoded file to a directory on your server. Submit a
157valid server path ending in the file name. Make sure the directory is
158writable (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
165Causes the Zip file to be downloaded from your server. The function must
166be 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
177Returns the Zip-compressed file data. Generally you will not need this
178function unless you want to do something unique with the data. Example::
179
Derek Jones526362d2011-10-05 15:23:43 -0500180 $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 Jones8ede1a22011-10-05 13:34:52 -0500186
187$this->zip->clear_data()
188=========================
189
190The Zip class caches your zip data so that it doesn't need to recompile
191the Zip archive for each function you use above. If, however, you need
192to create multiple Zips, each with different data, you can clear the
193cache between calls. Example::
194
Derek Jones526362d2011-10-05 15:23:43 -0500195 $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 Jones8ede1a22011-10-05 13:34:52 -0500208