Skip to content

Commit 2c08e4e

Browse files
committed
[ci skip] Update Zip library docs
1 parent 5371810 commit 2c08e4e

File tree

1 file changed

+116
-113
lines changed
  • user_guide_src/source/libraries

1 file changed

+116
-113
lines changed

user_guide_src/source/libraries/zip.rst

Lines changed: 116 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ CodeIgniter's Zip Encoding Class classes permit you to create Zip
66
archives. Archives can be downloaded to your desktop or saved to a
77
directory.
88

9+
.. contents::
10+
:local:
11+
12+
.. raw:: html
13+
14+
<div class="custom-index container"></div>
15+
16+
****************************
17+
Using the Zip Encoding Class
18+
****************************
19+
920
Initializing the Class
1021
======================
1122

@@ -35,174 +46,166 @@ your server, and download it to your desktop.
3546
// Download the file to your desktop. Name it "my_backup.zip"
3647
$this->zip->download('my_backup.zip');
3748

38-
******************
39-
Function Reference
40-
******************
49+
***************
50+
Class Reference
51+
***************
4152

42-
$this->zip->add_data()
43-
=======================
53+
.. class:: CI_Zip
4454

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::
55+
.. method:: add_data($filepath[, $data = NULL])
4856

49-
$name = 'my_bio.txt';
50-
$data = 'I was born in an elevator...';
57+
:param mixed $filepath: a single file path or an array of file => data pairs
58+
:param array $data: single file contents
59+
:returns: void
5160

52-
$this->zip->add_data($name, $data);
61+
Adds data to the Zip archive. Can work both in single and multiple files mode.
5362

54-
You are allowed multiple calls to this function in order to add several
55-
files to your archive. Example::
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::
5664

57-
$name = 'mydata1.txt';
58-
$data = 'A Data String!';
59-
$this->zip->add_data($name, $data);
65+
$name = 'mydata1.txt';
66+
$data = 'A Data String!';
67+
$this->zip->add_data($name, $data);
6068

61-
$name = 'mydata2.txt';
62-
$data = 'Another Data String!';
63-
$this->zip->add_data($name, $data);
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::
6474

65-
Or you can pass multiple files using an array::
75+
$data = array(
76+
'mydata1.txt' => 'A Data String!',
77+
'mydata2.txt' => 'Another Data String!'
78+
);
6679

67-
$data = array(
68-
'mydata1.txt' => 'A Data String!',
69-
'mydata2.txt' => 'Another Data String!'
70-
);
80+
$this->zip->add_data($data);
7181

72-
$this->zip->add_data($data);
82+
If you would like your compressed data organized into sub-directories, simply include the path as part of the filename(s)::
7383

74-
$this->zip->download('my_backup.zip');
84+
$name = 'personal/my_bio.txt';
85+
$data = 'I was born in an elevator...';
7586

76-
If you would like your compressed data organized into sub-folders,
77-
include the path as part of the filename::
87+
$this->zip->add_data($name, $data);
7888

79-
$name = 'personal/my_bio.txt';
80-
$data = 'I was born in an elevator...';
89+
The above example will place my_bio.txt inside a folder called personal.
8190

82-
$this->zip->add_data($name, $data);
91+
.. method:: add_dir($directory)
8392

84-
The above example will place my_bio.txt inside a folder called
85-
personal.
93+
:param mixed $directory: string directory name or an array of multiple directories
94+
:returns: void
8695

87-
$this->zip->add_dir()
88-
======================
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::
8998

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::
99+
$this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"
94100

95-
$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"
101+
.. method:: read_file($path[, $preserve_filepath = FALSE])
96102

97-
$this->zip->read_file()
98-
========================
103+
:param string $path: path to file
104+
:param bool $preserve_filepath: whether to maintain the original filepath
105+
:returns: bool
99106

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::
107+
Permits you to compress a file that already exists somewhere on your server.
108+
Supply a file path and the zip class will read it and add it to the archive::
103109

104-
$path = '/path/to/photo.jpg';
110+
$path = '/path/to/photo.jpg';
105111

106-
$this->zip->read_file($path);
112+
$this->zip->read_file($path);
107113

108-
// Download the file to your desktop. Name it "my_backup.zip"
109-
$this->zip->download('my_backup.zip');
114+
// Download the file to your desktop. Name it "my_backup.zip"
115+
$this->zip->download('my_backup.zip');
110116

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::
117+
If you would like the Zip archive to maintain the directory structure of
118+
the file in it, pass TRUE (boolean) in the second parameter. Example::
113119

114-
$path = '/path/to/photo.jpg';
120+
$path = '/path/to/photo.jpg';
115121

116-
$this->zip->read_file($path, TRUE);
122+
$this->zip->read_file($path, TRUE);
117123

118-
// Download the file to your desktop. Name it "my_backup.zip"
119-
$this->zip->download('my_backup.zip');
124+
// Download the file to your desktop. Name it "my_backup.zip"
125+
$this->zip->download('my_backup.zip');
120126

121-
In the above example, photo.jpg will be placed inside two folders:
122-
path/to/
127+
In the above example, photo.jpg will be placed into the *path/to/* directory.
123128

124-
$this->zip->read_dir()
125-
=======================
129+
.. method:: read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])
126130

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::
131+
:param string $path: path to directory
132+
:param bool $preserve_filepath: whether to maintain the original path
133+
:param string $root_path: part of the path to exclude from the archive directory
134+
:returns: bool
132135

133-
$path = '/path/to/your/directory/';
136+
Permits you to compress a directory (and its contents) that already exists somewhere on your server.
137+
Supply a path to the directory and the zip class will recursively read and recreate it as a Zip archive.
138+
All files contained within the supplied path will be encoded, as will any sub-directories contained within it. Example::
134139

135-
$this->zip->read_dir($path);
140+
$path = '/path/to/your/directory/';
136141

137-
// Download the file to your desktop. Name it "my_backup.zip"
138-
$this->zip->download('my_backup.zip');
142+
$this->zip->read_dir($path);
139143

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+
// Download the file to your desktop. Name it "my_backup.zip"
145+
$this->zip->download('my_backup.zip');
144146

145-
$path = '/path/to/your/directory/';
147+
By default the Zip archive will place all directories listed in the first parameter inside the zip.
148+
If you want the tree preceding the target directory to be ignored you can pass FALSE (boolean) in the second parameter. Example::
146149

147-
$this->zip->read_dir($path, FALSE);
150+
$path = '/path/to/your/directory/';
148151

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+
$this->zip->read_dir($path, FALSE);
152153

153-
$this->zip->archive()
154-
=====================
154+
This will create a ZIP with a directory named "directory" inside, then all sub-directories stored correctly inside that, but will not include the
155+
*/path/to/your* part of the path.
155156

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::
157+
.. method:: archive($filepath)
159158

160-
$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
159+
:param string $filepath: path to target zip archive
160+
:returns: bool
161161

162-
$this->zip->download()
163-
======================
162+
Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name.
163+
Make sure the directory is writable (660 or 666 is usually OK). Example::
164164

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::
165+
$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip
167166

168-
$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
167+
.. method:: download($filename = 'backup.zip')
169168

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.
169+
:param string $filename: the archive file name
170+
:returns: void
173171

174-
$this->zip->get_zip()
175-
======================
172+
Causes the Zip file to be downloaded from your server. You must pass the name you would like the zip file called. Example::
176173

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::
174+
$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"
179175

180-
$name = 'my_bio.txt';
181-
$data = 'I was born in an elevator...';
176+
.. note:: Do not display any data in the controller in which you call
177+
this method since it sends various server headers that cause the
178+
download to happen and the file to be treated as binary.
182179

183-
$this->zip->add_data($name, $data);
180+
.. method:: get_zip()
184181

185-
$zip_file = $this->zip->get_zip();
182+
:returns: string
186183

187-
$this->zip->clear_data()
188-
=========================
184+
Returns the Zip-compressed file data. Generally you will not need this method unless you want to do something unique with the data. Example::
189185

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::
186+
$name = 'my_bio.txt';
187+
$data = 'I was born in an elevator...';
194188

195-
$name = 'my_bio.txt';
196-
$data = 'I was born in an elevator...';
189+
$this->zip->add_data($name, $data);
197190

198-
$this->zip->add_data($name, $data);
199-
$zip_file = $this->zip->get_zip();
191+
$zip_file = $this->zip->get_zip();
192+
193+
.. method:: clear_data()
194+
195+
:returns: void
196+
197+
The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each method you use above.
198+
If, however, you need to create multiple Zip archives, each with different data, you can clear the cache between calls. Example::
200199

201-
$this->zip->clear_data();
200+
$name = 'my_bio.txt';
201+
$data = 'I was born in an elevator...';
202202

203-
$name = 'photo.jpg';
204-
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
203+
$this->zip->add_data($name, $data);
204+
$zip_file = $this->zip->get_zip();
205205

206+
$this->zip->clear_data();
206207

207-
$this->zip->download('myphotos.zip');
208+
$name = 'photo.jpg';
209+
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
208210

211+
$this->zip->download('myphotos.zip');

0 commit comments

Comments
 (0)