PHPのZipArchiveの使い方は何ですか?
ZipArchiveはPHPでZIP圧縮ファイルを操作するためのクラスです。ZIPファイルの作成、開く、読み込む、追加、更新、削除、解凍する機能が提供されています。
ZipArchiveの一般的な使い方の例を以下に示します:
- 空のZIPファイルを作成する。
$zip = new ZipArchive();
$zip->open('path/to/archive.zip', ZipArchive::CREATE);
$zip->close();
- 既存のZIPファイルを開く:
$zip = new ZipArchive();
$zip->open('path/to/archive.zip');
- ZIPファイルにファイルを追加する。
$zip->addFile('path/to/file.txt', 'file.txt');
- ZIPファイルに目次とその内容を追加する。
$zip->addEmptyDir('path/to/directory');
$zip->addGlob('path/to/directory/*');
- 指定されたディレクトリにZIPファイルからファイルを解凍する。
$zip->extractTo('path/to/destination');
- ZIPファイル内のファイルリストを取得する。
$fileList = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$fileList[] = $zip->getNameIndex($i);
}
- ZIPファイル内のファイルを削除する。
$zip->deleteName('file.txt');
- ZIPファイルを閉じる。
$zip->close();
これらはZipArchiveクラスの一般的な使用例の一部であり、他にもさまざまな方法があります。具体的な使用方法は、PHP公式ドキュメントのZipArchiveクラスのドキュメントを参照してください:https://www.php.net/manual/en/class.ziparchive.php