PHP自帶ZIP壓縮、解壓縮類ZipArchiv介紹及使用

--------------------- 使用ZipArchive對象的前提 --------------------- 要使用該PHP擴展類,須要(PHP 5 >= 5.2.0, PECL zip >= 1.1.0),部分方法須要 PHP 5.2.+,且php.ini配置支持zip 對於win系統,直接去掉php_zip.dll 擴展的註釋,而後重啓http服務(IIS或Apache)便可 Linux尚未試驗,理論上差異不會很大 功能: 一、解壓縮zip文件 二、將文件壓縮成zip文件 三、追加文件到zip文件 四、將文件夾打包成zip文件(須要循環添加文件與建立空文件夾) 五、刪除壓縮文件中的條目 --------------------- ZipArchive對象經常使用方法介紹 --------------------- 測試約定: 測試文件爲text.zip,該壓縮文件包含了三個被壓縮的文件(hello.txt、word.txt、ooxx.jpg),以下所示 text.zip     hello.txt     word.txt     ooxx.jpg 打開zip文件,以便進一步操做 ZipArchive::open (PHP 5 >= 5.2.0, PECL zip >= 1.1.0) mixed ZipArchive::open ( string $filename [, int $flags ] ) 第2個參數講解 ZIPARCHIVE::OVERWRITE    老是建立一個新的文件,若是指定的zip文件存在,則會覆蓋掉 ZIPARCHIVE::CREATE        若是指定的zip文件不存在,則新建一個 ZIPARCHIVE::EXCL        若是指定的zip文件存在,則會報錯    ZIPARCHIVE::CHECKCONS 返回值: 若是返回值等於下面的屬性,表示對應的錯誤 或者 返回TRUE $res == ZipArchive::ER_EXISTS    File already exists.(文件已經存在) $res == ZipArchive::ER_INCONS    Zip archive inconsistent.(壓縮文件不一致) $res == ZipArchive::ER_INVAL    Invalid argument.(無效的參數) $res == ZipArchive::ER_MEMORY    Malloc failure.(內存錯誤?這個不肯定) $res == ZipArchive::ER_NOENT    No such file.(沒有這樣的文件) $res == ZipArchive::ER_NOZIP    Not a zip archive.(沒有一個壓縮文件) $res == ZipArchive::ER_OPEN        Can't open file.(不能打開文件) $res == ZipArchive::ER_READ        Read error.(讀取錯誤) $res == ZipArchive::ER_SEEK        Seek error.(查找錯誤) <?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; } ?>      根據壓縮文件內的列表索引,返回被壓縮文件的名稱 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(); ?> 根據壓縮內的文件名稱,獲取該文件的文本流 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); ?> 根據壓縮文件內的索引(從0開始)修改壓縮文件內的文件名 ZipArchive::renameIndex bool ZipArchive::renameIndex ( int $index , string $newname ) (PHP 5 >= 5.2.0, PECL zip >= 1.5.0) 成功時返回 TRUE, 或者在失敗時返回 FALSE。 <?php $zip = new ZipArchive; $res = $zip->open('test.zip'); if ($res === TRUE) {     //把壓縮文件內第一個文件修改爲newname.txt     $zip->renameIndex(0,'newname.txt');     $zip->close(); } else {     echo 'failed, code:' . $res; } ?> 根據壓縮文件內的文件名,修改壓縮文件內的文件名 ZipArchive::renameName (PHP 5 >= 5.2.0, PECL zip >= 1.5.0) <?php $zip = new ZipArchive; $res = $zip->open('test.zip'); if ($res === TRUE) {     //把壓縮文件內的word.txt修改爲newword.txt     $zip->renameName('word.txt','newword.txt');     $zip->close(); } else {     echo 'failed, code:' . $res; } ?> 獲取壓縮文件的註釋(zip的文件註釋) ZipArchive::getArchiveComment (PHP 5 >= 5.2.0, PECL zip >= 1.1.0) string ZipArchive::getArchiveComment ([ int $flags ] ) 參數:ZipArchive::FL_UNCHANGED 若是參數設置爲 ZipArchive::FL_UNCHANGED, 返回原始的尚未改變的註釋 例如,在處理該壓縮文件時,使用setArchiveComment()方法改變或設置註釋時 若是加上ZipArchive::FL_UNCHANGED這個參數,則表示獲取改變以前的註釋內容,不然獲取已經改變的註釋內容 相似的還有: ZipArchive::getCommentIndex 根據壓縮文件內的文件索引獲取【文件註釋】 ZipArchive::getCommentName    根據壓縮文件內的文件名稱獲取【文件註釋】 注意:這裏的是文件註釋,不是壓縮文件(zip)的註釋 設置或修改壓縮文件的註釋(zip的文件註釋) 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'; } ?> 根據壓縮文件內的索引刪除壓縮文件內的文件(也就是刪除檔案內的條目) ZipArchive::deleteIndex (PHP 5 >= 5.2.0, PECL zip >= 1.5.0) 1、如何解壓縮一個zip文件 extractTo() $zip = new ZipArchive(); 1、如何建立壓縮文件? addFromString() addFile() 便是是把一個或多個文件打包成一個zip文件 一、只須要new一個ZipArchive對象 二、而後使用該對象的open方法建立一個zip文件 三、接着使用addFile方法,將要打包的文件寫入剛剛建立的zip文件中 四、最後記得關閉該對象 <?php //創建一個新的ZipArchive的對象 $zip = new ZipArchive; $res = $zip->open('test.zip'); //若是打開成功 if ($res === TRUE) { //若是打開失敗 } else {     //輸出出錯的代碼     echo 'failed, code:' . $res; } $zip->close(); --------------------- 查閱的相關文檔 --------------------- 查閱的相關文檔: 官方文檔 http://www.php.net/manual/zh/book.zip.php PHP ZipArchive 實現壓縮解壓Zip文件 http://www.yuansir-web.com/2011/08/31/php-ziparchive-%E5%AE%9E%E7%8E%B0%E5%8E%8B%E7%BC%A9%E8%A7%A3%E5%8E%8Bzip%E6%96%87%E4%BB%B6/ PHP 使用ZipArchive壓縮文件並下載 http://courages.us/archives/176 PHP中ZipArchive壓縮文件並下載打包好的文件介紹 http://www.php100.com/html/php/lei/2013/0905/5288.html
相關文章
相關標籤/搜索