PHP ZipArchive 實現壓縮解壓Zip檔案

來源:互聯網
上載者:User
PHP ZipArchive 是PHP內建的擴充類,可以輕鬆實現ZIP檔案的壓縮和解壓,使用前首先要確保PHP ZIP 擴充已經開啟,具體開啟方法就不說了,不同的平台開啟PHP擴增的方法網上都有,如有疑問歡迎交流。這裡整理一下常用的樣本供參考。

一、解壓縮zip檔案

$zip = new ZipArchive;//建立一個ZipArchive的對象/*通過ZipArchive的對象處理zip檔案$zip->open這個方法的參數表示處理的zip檔案名稱。如果對zip檔案對象操作成功,$zip->open這個方法會返回TRUE*/if ($zip->open('test.zip') === TRUE){$zip->extractTo('images');//假設解壓縮到在當前路徑下images檔案夾的子檔案夾php$zip->close();//關閉處理的zip檔案}

二、將檔案壓縮成zip檔案

$zip = new ZipArchive;/*$zip->open這個方法第一個參數表示處理的zip檔案名稱。第二個參數表示處理模式,ZipArchive::OVERWRITE表示如果zip檔案存在,就覆蓋掉原來的zip檔案。如果參數使用ZIPARCHIVE::CREATE,系統就會往原來的zip檔案裡新增內容。如果不是為了多次新增內容到zip檔案,建議使用ZipArchive::OVERWRITE。使用這兩個參數,如果zip檔案不存在,系統都會自動建立。如果對zip檔案對象操作成功,$zip->open這個方法會返回TRUE*/if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE){$zip->addFile('image.txt');//假設加入的檔案名稱是image.txt,在當前路徑下$zip->close();}

三、檔案追加內容添加到zip檔案

$zip = new ZipArchive;$res = $zip->open('test.zip', ZipArchive::CREATE);if ($res === TRUE) {$zip->addFromString('test.txt', 'file content goes here');$zip->close();echo 'ok';} else {echo 'failed';}


四、將檔案夾打包成zip檔案
function addFileToZip($path, $zip) {$handler = opendir($path); //開啟當前檔案夾由$path指定。/*迴圈的讀取檔案夾下的所有檔案和檔案夾其中$filename = readdir($handler)是每次迴圈的時候將讀取的檔案名稱賦值給$filename,為了不陷於死迴圈,所以還要讓$filename !== false。一定要用!==,因為如果某個檔案名稱如果叫'0',或者某些被系統認為是代表false,用!=就會停止迴圈*/while (($filename = readdir($handler)) !== false) {if ($filename != "." && $filename != "..") {//檔案夾檔案名稱字為'.'和‘..’,不要對他們進行操作if (is_dir($path . "/" . $filename)) {// 如果讀取的某個對象是檔案夾,則遞迴addFileToZip($path . "/" . $filename, $zip);} else { //將檔案加入zip對象$zip->addFile($path . "/" . $filename);}}}@closedir($path);}$zip = new ZipArchive();if ($zip->open('images.zip', ZipArchive::OVERWRITE) === TRUE) {addFileToZip('images/', $zip); //調用方法,對要打包的根目錄進行操作,並將ZipArchive的對象傳遞給方法$zip->close(); //關閉處理的zip檔案}

ZipArchive方法如下:

  • ZipArchive::addEmptyDir — Add a new directory
  • ZipArchive::addFile — Adds a file to a ZIP archive from the given path
  • ZipArchive::addFromString — Add a file to a ZIP archive using its contents
  • ZipArchive::close — Close the active archive (opened or newly created)
  • ZipArchive::deleteIndex — delete an entry in the archive using its index
  • ZipArchive::deleteName — delete an entry in the archive using its name
  • ZipArchive::extractTo — Extract the archive contents
  • ZipArchive::getArchiveComment — Returns the Zip archive comment
  • ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index
  • ZipArchive::getCommentName — Returns the comment of an entry using the entry name
  • ZipArchive::getFromIndex — Returns the entry contents using its index
  • ZipArchive::getFromName — Returns the entry contents using its name
  • ZipArchive::getNameIndex — Returns the name of an entry using its index
  • ZipArchive::getStatusString — Returns the status error message, system and/or zip messages
  • ZipArchive::getStream — Get a file handler to the entry defined by its name (read only).
  • ZipArchive::locateName — Returns the index of the entry in the archive
  • ZipArchive::open — Open a ZIP file archive
  • ZipArchive::renameIndex — Renames an entry defined by its index
  • ZipArchive::renameName — Renames an entry defined by its name
  • ZipArchive::setArchiveComment — Set the comment of a ZIP archive
  • ZipArchive::setCommentIndex — Set the comment of an entry defined by its index
  • ZipArchive::setCommentName — Set the comment of an entry defined by its name
  • ZipArchive::statIndex — Get the details of an entry defined by its index.
  • ZipArchive::statName — Get the details of an entry defined by its name.
  • ZipArchive::unchangeAll — Undo all changes done in the archive
  • ZipArchive::unchangeArchive — Revert all global changes done in the archive.
  • ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index
  • ZipArchive::unchangeName — Revert all changes done to an entry with the given name.

  • 以上就介紹了PHP ZipArchive 實現壓縮解壓Zip檔案,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.