以 Ubuntu
為例:
sudo apt update
sudo apt install -y php7.4-zip
產生壓縮檔
<?php
//new ZipArchive 物件
$zip = new ZipArchive;
$zip->open('new.zip', ZipArchive::CREATE);
其中 $zip->open()
第一參數為要產壓縮檔路徑
第二參數為產生檔案的模式
ZipArchive::CREATE
:產生新檔案ZipArchive::OVERWRITE
: 若壓縮檔存在,則覆蓋檔案加入一個空資料夾
$zip->addEmptyDir($dirname);
參數說明
$dirname
資料夾名稱將檔案加入壓縮檔
$zip->addaddFile($filePath, $newFileName);
參數說明
$filePath
為檔案路徑$newFileName
重新命名 (選填)將字串加入文字檔並壓縮
$zip->addFromString($filename, $content);
參數說明
$filename
檔案名稱$content
文字內容依索引刪除檔案
$zip->deleteIndex($index);
參數說明
$index
刪除索引的檔案依檔名刪除檔案
$zip->deleteName($name);
參數說明
$name
刪除檔案名稱更改檔名
$zip->renameName($oldName, $newName);
參數說明
$oldName
原本檔名$newName
新檔名關閉壓縮檔
$zip->close();
範例程式
$zip = new ZipArchive;
$zip->open($zipFile);
$zip->extractTo($filePath);
$zip->close();
參數說明
$zipFile
壓縮檔位置$filePath
解壓縮檔路徑