From e95014f7752ed459075b7270c324b3a8f68dc4e7 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 1 Oct 2006 21:44:52 +0000 Subject: --- user_guide/libraries/zip.html | 146 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 127 insertions(+), 19 deletions(-) (limited to 'user_guide/libraries') diff --git a/user_guide/libraries/zip.html b/user_guide/libraries/zip.html index 1994e3462..f126bd0b7 100644 --- a/user_guide/libraries/zip.html +++ b/user_guide/libraries/zip.html @@ -62,66 +62,174 @@ Zip Encoding Class

Zip Encoding Class

-

Code Igniter's Zip Encoding Class classes permit you to create Zip archives.

+

Code Igniter's Zip Encoding Class classes permit you to create Zip archives. Archives can be downloaded to your +desktop or saved to a directory.

Initializing the Class

Like most other classes in Code Igniter, the Zip class is initialized in your controller using the $this->load->library function:

$this->load->library('zip'); -

Once loaded, the Parser library object will be available using: $this->zip

+

Once loaded, the Zip library object will be available using: $this->zip

-

Usage Examples

+

Usage Example

-

Simple example demonstrating how to compress a file and download it to your desktop.

+

This example demonstrates how to compress a file, save it to a folder on your server, and download it to your desktop.

$name = 'mydata1.txt';
$data = 'A Data String!';
-$this->zip->add_data($name, $data);

-$name = 'mydata2.txt';
-$data = 'Another Data String!';
$this->zip->add_data($name, $data);

+// Write the zip file to a folder on your server. Name it "my_backup.zip"
+$this->zip->archive('/path/to/directory/my_backup.zip'); +

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


Function Reference

+ +

$this->zip->add_data()

+ +

Permits you to add data to the Zip archive. The first parameter must contain the name you would like +given to the file, the second parameter must contain the file data:

+ + +$name = 'my_bio.txt';
+$data = 'I was born in an elevator...';
+
+$this->zip->add_data($name, $data); +
+ -$this->zip->download('my_backup.zip'); // Parameter contains name of the downloaded file +

If you would like to compress a file that exists somewhere on your server you will need to read the data before +passing it to the function:

+ + +$name = 'photo.jpg';

+$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
+
+$this->zip->add_data($name, $data);
-

If you would like to place your data into a folder just include the directory as part of the filename:

+

You are allowed to have multiple calls to this function in order to +add several files to your archive. Example:

-$name = 'myfolder/mydata1.txt';
+$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);

-$name = 'myfolder/mydata2.txt';
+$name = 'mydata2.txt';
$data = 'Another Data String!';
$this->zip->add_data($name, $data);
-
- -$this->zip->download('my_backup.zip'); // Parameter contains name of the downloaded file
-

You can also pass your file data as an array:

- +

Or you can pass multiple files using an array:

$data = array(
-                'myfolder/mydata1.txt' => 'A Data String!',
-                'myfolder/mydata2.txt' => 'Another Data String!'
+                'mydata1.txt' => 'A Data String!',
+                'mydata2.txt' => 'Another Data String!',
+                'photo.jpg' => file_get_contents("/path/to/photo.jpg")
            );

$this->zip->add_data($data);

-$this->zip->download('my_backup.zip'); // Parameter contains name of the downloaded file +$this->zip->download('my_backup.zip'); +
+ +

If you would like your compressed data organized into sub-folders, include the path as part of the filename:

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

The above example will place my_bio.txt inside a folder called personal.

+ + +

$this->zip->add_dir()

+ +

Permits you to add a directory. Usually this function is unnecessary since you can place your data into folders when +using $this->zip->add_data(), but if you would like to create an empty folder you can do so. Example:

+ +$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder" + + +

$this->zip->archive()

+ +

Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. Make sure the +directory is writable (666 or 777 is usually OK). Example:

+ +$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip + + +

$this->zip->download()

+ +

Causes the Zip file to be downloaded to your server. The function must be passed the name you would like the zip file called. +Example:

+ +$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip" + +

Note:  Do not display any data in the controller in which you call this function since it sends various server headers +that cause the download to happen and the file to be treated as binary.

+ + +

$this->zip->get_zip()

+ +

Returns the Zip-compressed file data. Generally you will not need this function unless you want to do something unique with the data. +Example:

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

+ +$zip_file = $this->zip->get_zip(); +
+ + +

$this->zip->clear_data()

+ +

The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each function you use above. +If, however, you need to create multiple Zips, each with different data, you can clear the cache between calls. Example:

+ + +$name = 'my_bio.txt';
+$data = 'I was born in an elevator...';
+
+$this->zip->add_data($name, $data);
+$zip_file = $this->zip->get_zip();
+
+$this->zip->clear_data(); +

+ +$name = 'photo.jpg';
+$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
+$this->zip->add_data($name, $data); +

+$this->zip->download('myphotos.zip');
+ + + + + + + + -- cgit v1.2.3-24-g4f1b