PHP自帶ZIP壓縮、解壓縮類(lèi)ZipArchiv使用指南

字號(hào):


    這篇文章主要介紹了PHP自帶ZIP壓縮、解壓縮類(lèi)ZipArchiv使用指南,十分詳細(xì),需要的朋友可以參考下
    要使用該P(yáng)HP擴(kuò)展類(lèi),需要(PHP 5 >= 5.2.0, PECL zip >= 1.1.0),部分方法需要 PHP 5.2.+,且php.ini配置支持zip
    對(duì)于win系統(tǒng),直接去掉php_zip.dll 擴(kuò)展的注釋?zhuān)缓笾貑ttp服務(wù)(IIS或Apache)即可
    Linux還沒(méi)有試驗(yàn),理論上差別不會(huì)很大
    功能:
    1、解壓縮zip文件
    2、將文件壓縮成zip文件
    3、追加文件到zip文件
    4、將文件夾打包成zip文件(需要循環(huán)添加文件與創(chuàng)建空文件夾)
    5、刪除壓縮文件中的條目
    --------------------- ZipArchive對(duì)象常用方法介紹 ---------------------
    測(cè)試約定:
    測(cè)試文件為text.zip,該壓縮文件包含了三個(gè)被壓縮的文件(hello.txt、word.txt、ooxx.jpg),如下所示
    代碼如下:
    text.zip
    hello.txt
    word.txt
    ooxx.jpg
    打開(kāi)zip文件,以便進(jìn)一步操作
    ZipArchive::open
    (PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
    mixed ZipArchive::open ( string $filename [, int $flags ] )
    第2個(gè)參數(shù)講解
    ZIPARCHIVE::OVERWRITE 總是創(chuàng)建一個(gè)新的文件,如果指定的zip文件存在,則會(huì)覆蓋掉
    ZIPARCHIVE::CREATE 如果指定的zip文件不存在,則新建一個(gè)
    ZIPARCHIVE::EXCL 如果指定的zip文件存在,則會(huì)報(bào)錯(cuò)
    ZIPARCHIVE::CHECKCONS
    返回值:
    如果返回值等于下面的屬性,表示對(duì)應(yīng)的錯(cuò)誤 或者 返回TRUE
    $res == ZipArchive::ER_EXISTS File already exists.(文件已經(jīng)存在)
    $res == ZipArchive::ER_INCONS Zip archive inconsistent.(壓縮文件不一致)
    $res == ZipArchive::ER_INVAL Invalid argument.(無(wú)效的參數(shù))
    $res == ZipArchive::ER_MEMORY Malloc failure.(內(nèi)存錯(cuò)誤?這個(gè)不確定)
    $res == ZipArchive::ER_NOENT No such file.(沒(méi)有這樣的文件)
    $res == ZipArchive::ER_NOZIP Not a zip archive.(沒(méi)有一個(gè)壓縮文件)
    $res == ZipArchive::ER_OPEN Can't open file.(不能打開(kāi)文件)
    $res == ZipArchive::ER_READ Read error.(讀取錯(cuò)誤)
    $res == ZipArchive::ER_SEEK Seek error.(查找錯(cuò)誤)
    代碼如下:
    <?php
    $zip = new ZipArchive;
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
    echo 'ok';
    //解壓縮到test文件夾
    $zip->extractTo('test');
    $zip->close();
    } else {
    echo 'failed, code:' . $res;
    }
    ?>
    根據(jù)壓縮文件內(nèi)的列表索引,返回被壓縮文件的名稱(chēng)
    ZipArchive::getNameIndex
    string ZipArchive::getNameIndex ( int $index [, int $flags ] )
    代碼如下:
    <?php
    $zip = new ZipArchive();
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
    var_dump($zip->getNameIndex(0)); // hello.txt
    var_dump($zip->getNameIndex(1)); // word.txt
    var_dump($zip->getNameIndex(2)); // ooxx.jpg
    } else {
    echo 'failed, code:' . $res;
    }
    $zip->close();
    ?>
    根據(jù)壓縮內(nèi)的文件名稱(chēng),獲取該文件的文本流
    ZipArchive::getStream
    resource ZipArchive::getStream ( string $name )
    代碼如下:
    <?php
    $zip = new ZipArchive();
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
    $stream = $zip->getStream('hello.txt');
    } else {
    echo 'failed, code:' . $res;
    }
    $zip->close();
    $str = stream_get_contents($stream); //這里注意獲取到的文本編碼
    var_dump($str);
    ?>
    根據(jù)壓縮文件內(nèi)的索引(從0開(kāi)始)修改壓縮文件內(nèi)的文件名
    ZipArchive::renameIndex
    bool ZipArchive::renameIndex ( int $index , string $newname )
    (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
    成功時(shí)返回 TRUE, 或者在失敗時(shí)返回 FALSE。
    代碼如下:
    <?php
    $zip = new ZipArchive;
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
    //把壓縮文件內(nèi)第一個(gè)文件修改成newname.txt
    $zip->renameIndex(0,'newname.txt');
    $zip->close();
    } else {
    echo 'failed, code:' . $res;
    }
    ?>
    根據(jù)壓縮文件內(nèi)的文件名,修改壓縮文件內(nèi)的文件名
    ZipArchive::renameName
    (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
    代碼如下:
    <?php
    $zip = new ZipArchive;
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
    //把壓縮文件內(nèi)的word.txt修改成newword.txt
    $zip->renameName('word.txt','newword.txt');
    $zip->close();
    } else {
    echo 'failed, code:' . $res;
    }
    ?>
    獲取壓縮文件的注釋?zhuān)▃ip的文件注釋?zhuān)?BR>    ZipArchive::getArchiveComment
    (PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
    string ZipArchive::getArchiveComment ([ int $flags ] )
    參數(shù):ZipArchive::FL_UNCHANGED
    如果參數(shù)設(shè)置為 ZipArchive::FL_UNCHANGED, 返回原始的還沒(méi)有改變的注釋
    例如,在處理該壓縮文件時(shí),使用setArchiveComment()方法改變或設(shè)置注釋時(shí)
    如果加上ZipArchive::FL_UNCHANGED這個(gè)參數(shù),則表示獲取改變之前的注釋內(nèi)容,否則獲取已經(jīng)改變的注釋內(nèi)容
    類(lèi)似的還有:
    ZipArchive::getCommentIndex 根據(jù)壓縮文件內(nèi)的文件索引獲取【文件注釋】
    ZipArchive::getCommentName 根據(jù)壓縮文件內(nèi)的文件名稱(chēng)獲取【文件注釋】
    注意:這里的是文件注釋?zhuān)皇菈嚎s文件(zip)的注釋
    設(shè)置或修改壓縮文件的注釋?zhuān)▃ip的文件注釋?zhuān)?BR>    ZipArchive::setArchiveComment
    (PHP 5 >= 5.2.0, PECL zip >= 1.4.0)
    bool ZipArchive::setArchiveComment ( string $comment )
    代碼如下:
    <?php
    $zip = new ZipArchive;
    $res = $zip->open('test.zip', ZipArchive::CREATE);
    if ($res === TRUE) {
    //$zip->addFromString('test.txt', 'file content goes here');
    $zip->setArchiveComment('new archive comment');
    $zip->close();
    echo 'ok';
    } else {
    echo 'failed';
    }
    ?>
    根據(jù)壓縮文件內(nèi)的索引刪除壓縮文件內(nèi)的文件(也就是刪除檔案內(nèi)的條目)
    ZipArchive::deleteIndex
    (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
    一、如何解壓縮一個(gè)zip文件 extractTo()
    代碼如下:
    $zip = new ZipArchive();
    一、如何創(chuàng)建壓縮文件? addFromString() addFile()
    即是是把一個(gè)或多個(gè)文件打包成一個(gè)zip文件
    1、只需要new一個(gè)ZipArchive對(duì)象
    2、然后使用該對(duì)象的open方法創(chuàng)建一個(gè)zip文件
    3、接著使用addFile方法,將要打包的文件寫(xiě)入剛剛創(chuàng)建的zip文件中
    4、最后記得關(guān)閉該對(duì)象
    代碼如下:
    <?php
    //建立一個(gè)新的ZipArchive的對(duì)象
    $zip = new ZipArchive;
    $res = $zip->open('test.zip');
    //如果打開(kāi)成功
    if ($res === TRUE) {
    //如果打開(kāi)失敗
    } else {
    //輸出出錯(cuò)的代碼
    echo 'failed, code:' . $res;
    }
    $zip->close();
    以上所述就是本文的全部?jī)?nèi)容了,希望能對(duì)大家有所幫助。