云南網(wǎng)站建設(shè)創(chuàng)新企業(yè) 昆明多彩網(wǎng)絡(luò)公司

在線qq:540105663

PclZip(php在線壓縮類)中文手冊

來源:昆明網(wǎng)絡(luò)公司 日期:2010-09-15 閱讀: 發(fā)表評論

PclZip是一款功能非常強(qiáng)大的php在線壓縮和解壓類,對于網(wǎng)站制作有著很強(qiáng)大的輔助作用,這里提供PclZip的中文手冊參考

介紹
PclZip library 能夠壓縮與解壓縮 Zip 格式的壓縮檔(WinZip、PKZIP);且能對此類類檔桉進(jìn)行處理,包括產(chǎn)生壓縮檔、列出壓縮檔的內(nèi)容以及解壓縮檔桉等等。由于能夠在伺服器端進(jìn)行壓縮與解壓縮的動作,所以相當(dāng)方便使用。
PclZip 定義一個 PclZip 類別,其類別物件可視為一個 ZIP 檔桉,亦提供method 來進(jìn)行處理。

 如何使用
 基礎(chǔ)
所有的功能都由 pclzip.lib.php 這個檔桉提供,PclZip library可于其首頁(www.phpconcept.net/pclzip/index.en.php)下載。所有的 PKZIP 檔桉其實就是一個 PclZip 的類別物件。當(dāng)產(chǎn)生一個PclZip 檔桉(i.e., PclZip 類別物件),就會先產(chǎn)生一個壓縮檔,且檔名已經(jīng)指定,但此壓縮檔的內(nèi)容尚未存在:
<?PHP
        require_once('pclzip.lib.php');
        $archive = new PclZip("archive.zip");
    ?>
此物件提供了一些 public method 可用來處理此檔桉。

 參數(shù)
每一個 method 有其各自可使用的參數(shù),包括有必須與非必須的參數(shù):
<?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' 就是必須參數(shù);'PCLZIP_OPT_REMOVE_PATH' 則為非必須參數(shù)。當(dāng)然有些 method 也可以只包含非必須的參數(shù):
<?php
        $list = $archive->extract(PCLZIP_OPT_PATH, "folder",
                         PCLZIP_OPT_REMOVE_PATH, "data",
                               PCLZIP_CB_PRE_EXTRACT, "callback_pre_extract",);
    ?>
上例中愿本壓縮檔內(nèi)檔桉存放的路徑為 /data,不過你可以指定解壓縮至 /folder 中。此外,在解壓縮之前,會呼叫 callback function('callback_pre_extract()'),此 function 可讓使用者在解壓縮的過程中變更檔桉存放路徑與檔名,或是選擇某些檔桉不解壓縮。
所有可用的非必要參數(shù)可參考網(wǎng)址(www.phpconcept.net/pclzip/man/en/index.php)。

 回傳值
每個 method 所回傳的值可能會不同,將會在每個 method 中說明。不過大部分的method回傳0、error或是陣列。

 錯誤處理
從版本1.3之后,錯誤處理已經(jīng)整合至 PclZip 類別中,當(dāng)一個 method 回傳錯誤碼,可以得知一些額外的訊息以方便錯誤處理:
   * errorName():回傳錯誤名稱
   * errorCode():回傳錯誤碼
   * errorInfo():回傳錯誤的描述

 范例
接下來會舉幾個例子來說明如何使用 PclZip。
 1、產(chǎn)生ZIP壓縮檔
PclZip($zipname): 為 PclZip constructor,$zipname為 PKZIP 壓縮檔的檔名。主要是產(chǎn)生一個 PclZip 物件,即一個 PKZIP 壓縮檔;但此時,只有壓縮檔產(chǎn)生出來,并做一些檢查(例如是否有開啟 zlib extension...等),除此之外,并沒有做其他動作。
create($filelist, [optional arguments list]): 將參數(shù) $filelist 指定的檔桉或目錄(包含當(dāng)中所有檔桉與子目錄)加入上述所產(chǎn)生的壓縮檔中。而非必要的參數(shù)則能夠修改壓縮檔內(nèi)的檔桉存放路徑。此 method 可用的參數(shù)可以參考網(wǎng)志(www.phpconcept.net/pclzip/man/en/index.php)。
下面的范例說明如何產(chǎn)生 PKZIP 壓縮檔(檔名為 archive.zip),并將 file.txt、data/text.txt以及目錄 folder(包含當(dāng)中的檔桉與子目錄)加入剛剛產(chǎn)生的 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));
        }
    ?>
下面的范例說明基本上與上例一樣產(chǎn)生 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、列出壓縮檔內(nèi)容
listContent( ) :列出壓縮檔中的內(nèi)容,包括檔桉的屬性與目錄:
<?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>";
        }
    ?>
上例將會回傳結(jié)果:
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] 可用的參數(shù)可參考網(wǎng)址(www.phpconcept.net/pclzip/man/en/index.php)。這些參數(shù)能讓使用者在解壓縮的時候有更多的選項,譬如指定變更解壓縮檔桉的路徑、指定只解壓縮某些檔桉或不解壓縮某些檔桉或者是將檔桉解壓縮成字串輸出(可用于 readme 檔)。
下例是一個簡單的解壓縮檔桉范例,將壓縮檔 archive.zip 內(nèi)的檔桉解壓縮至目前的目錄:
<?php
        require_once('pclzip.lib.php');
        $archive = new PclZip('archive.zip');
 
 
        if ($archive->extract() == 0) {
            die("Error : ".$archive->errorInfo(true));
        }
    ?>
下例是進(jìn)階的解壓縮檔桉使用,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));
        }
    ?>

參數(shù):PCLZIP_OPT_REMOVE_PATH, 'install/release' 也可以忽略。直接解壓到data文件夾中
 

發(fā)表評論評論列表(有 條評論)

相關(guān)文章:
暫無相關(guān)文章
最熱文章:
新手怎么制作網(wǎng)頁? 閱讀:7660
jcarousellite中文文檔 閱讀:6507
表單元素input、按鈕、文字完美垂 閱讀:6247
怎么使chrome(谷歌google 閱讀:6145
phpMailer中文文檔手冊 閱讀:4639