Zip 编码类
CodeIgniter的zip编码类允许你创建ZIP压缩文档。文档可以保存在你的桌面或者某个文件夹里
初始化
与其他CodeIgniter里的类一样,ZIP类在控制器里完成初始化工作,函数:$this->load->library
$this->load->library('zip');
一旦加载,ZIP库对象可以用$this->zip来使用
使用样例
这个例子演示了如何压缩一个文件并保存到你服务器一个文件夹,然后下载到你的桌面上。
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// 在你的服务器的文件夹里写.zip文件。命名为"my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// 下载此文件到桌面,命名为"my_backup.zip"
$this->zip->download('my_backup.zip');
函数参考
$this->zip->add_data()
添加数据进zip文件里. 第一个参数是文件名, 第二个参数是你要添加的数据的字符串格式:
$name = 'my_bio.txt';
$data = 'I was born in an elevator...';
$this->zip->add_data($name, $data);
允许你多次调用这个函数按顺序的添加若干个文件到你的存档。例子:
$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);
或者你可以使用一个数组来传递多个文件(的值):
$data = array(
'mydata1.txt' => 'A Data String!',
'mydata2.txt' => 'Another Data String!'
);
$this->zip->add_data($data);
$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);
上面的例子将放置 my_bio.txt 到一个叫personal的文件夹内。
$this->zip->add_dir()
允许你添加一个目录。通常这个函数是不必要的,因为当你使用$this->zip->add_data(), 可以把你的数据放进目录里,但是如果你想要创建一个空目录的话你也可以这样做。例子:
$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"
$this->zip->read_file()
允许你压缩一个服务器某处存在的文件。提供一个文件路径,zip类将读取它并添加到存档:
$path = '/path/to/photo.jpg';
$this->zip->read_file($path);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
如果你想压缩文档保持原来文件的目录结构,可以把第二个参数设置为 TRUE (布尔值)。例子:
$path = '/path/to/photo.jpg';
$this->zip->read_file($path, TRUE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
在上边的例子中,photo.jpg将被放到两层文件夹中:path/to/
$this->zip->read_dir()
允许你压缩一个服务器某处存在的文件夹(以及它里面的文件和子文件夹)。提供一个文件夹路径,zip类将递归读取它并重新创建添加到存档。 All files contained within the supplied path will be encoded, as will any sub-folders contained within it. Example:
$path = '/path/to/your/directory/';
$this->zip->read_dir($path);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
$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 from 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';
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
$this->zip->download('myphotos.zip');