出自TechWiki
[編輯] 介紹
- PclZip library 能夠壓縮與解壓縮 Zip 格式的壓縮檔(WinZip、PKZIP);且能對此類類檔案進行處理,包括產生壓縮檔、列出壓縮檔的內容以及解壓縮檔案等等。由於能夠在伺服器端進行壓縮與解壓縮的動作,所以相當方便使用。
- PclZip 定義一個 PclZip 類別,其類別物件可視為一個 ZIP 檔案,亦提供method 來進行處理。
[編輯] 如何使用
[編輯] 基礎
- 所有的功能都由 pclzip.lib.php 這個檔案提供,PclZip library可於其首頁(www.phpconcept.net/pclzip/index.en.php)下載。所有的 PKZIP 檔案其實就是一個 PclZip 的類別物件。當產生一個PclZip 檔案(i.e., PclZip 類別物件),就會先產生一個壓縮檔,且檔名已經指定,但此壓縮檔的內容尚未存在:
<?PHP
require_once('pclzip.lib.php');
$archive = new PclZip("archive.zip");
?>
- 此物件提供了一些 public method 可用來處理此檔案。
[編輯] 參數
- 每一個 method 有其各自可使用的參數,包括有必須與非必須的參數:
<?php
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
$v_list = $archive->add('dev/file.txt',
PCLZIP_OPT_REMOVE_PATH, 'dev');
?>
- 上例中的 'dev/file.txt' 就是必須參數;'PCLZIP_OPT_REMOVE_PATH' 則為非必須參數。當然有些 method 也可以只包含非必須的參數:
<?php
$list = $archive->extract(PCLZIP_OPT_PATH, "folder",
PCLZIP_OPT_REMOVE_PATH, "data",
PCLZIP_CB_PRE_EXTRACT, "callback_pre_extract",);
?>
- 上例中原本壓縮檔內檔案存放的路徑為 /data,不過你可以指定解壓縮至 /folder 中。此外,在解壓縮之前,會呼叫 callback function('callback_pre_extract()'),此 function 可讓使用者在解壓縮的過程中變更檔案存放路徑與檔名,或是選擇某些檔案不解壓縮。
- 所有可用的非必要參數可參考網址(www.phpconcept.net/pclzip/man/en/index.php)。
[編輯] 回傳值
- 每個 method 所回傳的值可能會不同,將會在每個 method 中說明。不過大部分的method回傳0、error或是陣列。
[編輯] 錯誤處理
- 從版本1.3之後,錯誤處理已經整合至 PclZip 類別中,當一個 method 回傳錯誤碼,可以得知一些額外的訊息以方便錯誤處理:
* errorName():回傳錯誤名稱
* errorCode():回傳錯誤碼
* errorInfo():回傳錯誤的描述
[編輯] 範例
- 接下來會舉幾個例子來說明如何使用 PclZip。
[編輯] 1、產生ZIP壓縮檔
- PclZip($zipname): 為 PclZip constructor,$zipname為 PKZIP 壓縮檔的檔名。主要是產生一個 PclZip 物件,即一個 PKZIP 壓縮檔;但此時,只有壓縮檔產生出來,並做一些檢查(例如是否有開啟 zlib extension...等),除此之外,並沒有做其他動作。
- create($filelist, [optional arguments list]): 將參數 $filelist 指定的檔案或目錄(包含當中所有檔案與子目錄)加入上述所產生的壓縮檔中。而非必要的參數則能夠修改壓縮檔內的檔案存放路徑。此 method 可用的參數可以參考網誌(www.phpconcept.net/pclzip/man/en/index.php)。
- 下面的範例說明如何產生 PKZIP 壓縮檔(檔名為 archive.zip),並將 file.txt、data/text.txt以及目錄 folder(包含當中的檔案與子目錄)加入剛剛產生的 archive.zip中:
<?php
include_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
$v_list = $archive->create('file.txt,data/text.txt,folder');
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
?>
- 下面的範例說明基本上與上例一樣產生 archive.zip,但在將 file.txt 與 text.txt 壓縮於其中時,將路徑由 data/ 改為 install/ ;因此,在 archive.zip 中這兩個檔案的路徑會是install/file.txt 與 install/text.txt:
<?php
include_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
$v_list = $archive->create('data/file.txt,data/text.txt',
PCLZIP_OPT_REMOVE_PATH, 'data',
PCLZIP_OPT_ADD_PATH, 'install');
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
?>
[編輯] 2、列出壓縮檔內容
- listContent( ) :列出壓縮檔中的內容,包括檔案的屬性與目錄:
<?php
include_once('pclzip.lib.php');
$zip = new PclZip("test.zip");
if (($list = $zip->listContent()) == 0) {
die("Error : ".$zip->errorInfo(true));
}
for ($i=0; $i<sizeof($list); $i++) {
for(reset($list[$i]); $key = key($list[$i]); next($list[$i])) {
echo "File $i / [$key] = ".$list[$i][$key]."<br>";
}
echo "<br>";
}
?>
- 上例將會回傳結果:
File 0 / [filename] = data/file1.txt
File 0 / [stored_filename] = data/file1.txt
File 0 / [size] = 53
File 0 / [compressed_size] = 36
File 0 / [mtime] = 1010440428
File 0 / [comment] =
File 0 / [folder] = 0
File 0 / [index] = 0
File 0 / [status] = ok
File 1 / [filename] = data/file2.txt
File 1 / [stored_filename] = data/file2.txt
File 1 / [size] = 54
File 1 / [compressed_size] = 53
File 1 / [mtime] = 1011197724
File 1 / [comment] =
File 1 / [folder] = 0
File 1 / [index] = 1
File 1 / [status] = ok
[編輯] 3、解壓縮檔案
- extract([options list]) :解壓縮 PKZIP 中的檔案或目錄。
- [options list] 可用的參數可參考網址(www.phpconcept.net/pclzip/man/en/index.php)。這些參數能讓使用者在解壓縮的時候有更多的選項,譬如指定變更解壓縮檔案的路徑、指定只解壓縮某些檔案或不解壓縮某些檔案或者是將檔案解壓縮成字串輸出(可用於 readme 檔)。
- 下例是一個簡單的解壓縮檔案範例,將壓縮檔 archive.zip 內的檔案解壓縮至目前的目錄:
<?php
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if ($archive->extract() == 0) {
die("Error : ".$archive->errorInfo(true));
}
?>
- 下例是進階的解壓縮檔案使用,archive.zip 中所有檔案都解壓縮於 data/ 中,而特別指明在 install/release 中的所有檔案也直接丟於 data/ 中,而非 data/install/release:
<?php
include('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if ($archive->extract(PCLZIP_OPT_PATH, 'data',
PCLZIP_OPT_REMOVE_PATH, 'install/release') == 0) {
die("Error : ".$archive->errorInfo(true));
}
?>