PHPのZipArchiveの使い方は何ですか?

ZipArchiveはPHPでZIP圧縮ファイルを操作するためのクラスです。ZIPファイルの作成、開く、読み込む、追加、更新、削除、解凍する機能が提供されています。

ZipArchiveの一般的な使い方の例を以下に示します:

  1. 空のZIPファイルを作成する。
$zip = new ZipArchive();
$zip->open('path/to/archive.zip', ZipArchive::CREATE);
$zip->close();
  1. 既存のZIPファイルを開く:
$zip = new ZipArchive();
$zip->open('path/to/archive.zip');
  1. ZIPファイルにファイルを追加する。
$zip->addFile('path/to/file.txt', 'file.txt');
  1. ZIPファイルに目次とその内容を追加する。
$zip->addEmptyDir('path/to/directory');
$zip->addGlob('path/to/directory/*');
  1. 指定されたディレクトリにZIPファイルからファイルを解凍する。
$zip->extractTo('path/to/destination');
  1. ZIPファイル内のファイルリストを取得する。
$fileList = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
    $fileList[] = $zip->getNameIndex($i);
}
  1. ZIPファイル内のファイルを削除する。
$zip->deleteName('file.txt');
  1. ZIPファイルを閉じる。
$zip->close();

これらはZipArchiveクラスの一般的な使用例の一部であり、他にもさまざまな方法があります。具体的な使用方法は、PHP公式ドキュメントのZipArchiveクラスのドキュメントを参照してください:https://www.php.net/manual/en/class.ziparchive.php

bannerAds