📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の3版が非表示)
1行目: 1行目:
== 概要 ==
== 概要 ==
PHPには、主に、Zlibライブラリを利用した圧縮機能が組み込まれている。<br>
最も一般的なものは、gzcompress関数、gzuncompress関数があり、ZLIBフォーマットでデータを圧縮・解凍できる。<br>
<br>
基本的な使用例を以下に示す。<br>
<syntaxhighlight lang="php">
$data = "圧縮したいテキストデータ";
$compressed = gzcompress($data);
$original = gzuncompress($compressed);
</syntaxhighlight>
<br>
代替的な圧縮方式であるGZIPフォーマットを使用する場合は、gzencode関数およびgzdecode関数が使用できる。<br>
これらは、HTTPでの転送に適している。<br>
<syntaxhighlight lang="php">
$compressed = gzencode($data);
$original = gzdecode($compressed);
</syntaxhighlight>
<br>
大きなファイルを扱う場合、PHPは圧縮ストリームをサポートしている。<br>
<syntaxhighlight lang="php">
// 圧縮ファイルの書き込み
$gz = gzopen('file.gz', 'w9');
gzwrite($gz, $data);
gzclose($gz);
// 圧縮ファイルの読み込み
$gz = gzopen('file.gz', 'r');
$content = gzread($gz, 4096);
gzclose($gz);
</syntaxhighlight>
<br>
複数のファイルを扱う場合は、ZIPアーカイブが便利である。<br>
PHPのZIPアーカイブ操作では、ZipArchiveクラスを使用する。<br>
<syntaxhighlight lang="php">
$zip = new ZipArchive();
if ($zip->open('archive.zip', ZipArchive::CREATE) === TRUE) {
    $zip->addFromString('test.txt', 'ファイルの内容');
    $zip->addFile('existing.txt', 'new_name.txt');
    $zip->close();
}
</syntaxhighlight>
<br>
圧縮レベルは、0 (無圧縮) から9 (最大圧縮) まで指定可能である。<br>
ただし、高い圧縮レベルはCPU負荷が高くなるため、状況に応じて適切なレベルを選択することが重要である。<br>
<br>
また、バイナリデータを扱う場合は、データの破損を防ぐため、base64エンコードとの組み合わせを推奨する。<br>
<syntaxhighlight lang="php">
$compressed = base64_encode(gzcompress($data));
$original = gzuncompress(base64_decode($compressed));
</syntaxhighlight>
<br>
これらの機能を組み合わせることにより、効率的なデータ圧縮と転送が実現できる。<br>
特に大容量データの取り扱いやバックアップ処理等で重宝する。<br>
<br><br>
<br><br>


375行目: 427行目:
  {
  {
     try {
     try {
      // ファイルの検証
      validateFile($sourceFile);
       if (!file_exists($sourceFile)) {
       if (!file_exists($sourceFile)) {
           throw new RuntimeException('指定されたZIPファイルが存在しません');
           throw new RuntimeException('指定されたZIPファイルが存在しません');
401行目: 456行目:
     catch (\Exception $e) {
     catch (\Exception $e) {
       throw new RuntimeException("ZIP解凍処理中にエラーが発生しました: {$e->getMessage()}");
       throw new RuntimeException("ZIP解凍処理中にエラーが発生しました: {$e->getMessage()}");
    }
}
/**
  * ファイルの存在確認と権限チェックを行うプライベートメソッド
  *
  * @param string $filePath チェック対象のファイルパス
  * @throws RuntimeException チェック失敗時
  */
function validateFile(string $filePath): void
{
    if (!file_exists($filePath)) {
      throw new RuntimeException("ファイルが存在しません: {$filePath}");
    }
 
    if (!is_readable($filePath)) {
      throw new RuntimeException("ファイルの読み取り権限がありません: {$filePath}");
    }
 
    if (!is_file($filePath)) {
      throw new RuntimeException("指定されたパスがファイルではありません: {$filePath}");
     }
     }
  }
  }
420行目: 496行目:
<br>
<br>
==== tar.gz形式 ====
==== tar.gz形式 ====
<u>tar.gz圧縮を行う場合は、システムにtarコマンドがインストールされている必要がある。</u><br>
<br>
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
  declare(strict_types=1);
  declare(strict_types=1);
438行目: 516行目:
  {
  {
     try {
     try {
      // ファイルの検証
      validateFile($sourceFile);
       if (!file_exists($sourceFile)) {
       if (!file_exists($sourceFile)) {
           throw new RuntimeException('指定されたtar.gzファイルが存在しません');
           throw new RuntimeException('指定されたtar.gzファイルが存在しません');
485行目: 566行目:
     catch (\Exception $e) {
     catch (\Exception $e) {
       throw new RuntimeException("tar.gz解凍処理中にエラーが発生しました: {$e->getMessage()}");
       throw new RuntimeException("tar.gz解凍処理中にエラーが発生しました: {$e->getMessage()}");
    }
}
/**
  * ファイルの存在確認と権限チェックを行うプライベートメソッド
  *
  * @param string $filePath チェック対象のファイルパス
  * @throws RuntimeException チェック失敗時
  */
function validateFile(string $filePath): void
{
    if (!file_exists($filePath)) {
      throw new RuntimeException("ファイルが存在しません: {$filePath}");
    }
 
    if (!is_readable($filePath)) {
      throw new RuntimeException("ファイルの読み取り権限がありません: {$filePath}");
    }
 
    if (!is_file($filePath)) {
      throw new RuntimeException("指定されたパスがファイルではありません: {$filePath}");
     }
     }
  }
  }
503行目: 605行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
==== tar.xz形式 ====
==== tar.xz形式 ====
<u>tar.xz形式の圧縮には、PHPのXZ拡張機能が必要である。</u><br>
<u>また、tar.xz圧縮を行う場合は、システムにtarコマンドがインストールされている必要がある。</u><br>
<br>
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
  declare(strict_types=1);
  declare(strict_types=1);
522行目: 628行目:
  {
  {
     try {
     try {
      // ファイルの検証
      validateFile($sourceFile);
       if (!extension_loaded('xz')) {
       if (!extension_loaded('xz')) {
           throw new RuntimeException('XZ拡張機能がインストールされていません');
           throw new RuntimeException('XZ拡張機能がインストールされていません');
573行目: 682行目:
     catch (\Exception $e) {
     catch (\Exception $e) {
       throw new RuntimeException("tar.xz解凍処理中にエラーが発生しました: {$e->getMessage()}");
       throw new RuntimeException("tar.xz解凍処理中にエラーが発生しました: {$e->getMessage()}");
    }
}
/**
  * ファイルの存在確認と権限チェックを行うプライベートメソッド
  *
  * @param string $filePath チェック対象のファイルパス
  * @throws RuntimeException チェック失敗時
  */
function validateFile(string $filePath): void
{
    if (!file_exists($filePath)) {
      throw new RuntimeException("ファイルが存在しません: {$filePath}");
    }
 
    if (!is_readable($filePath)) {
      throw new RuntimeException("ファイルの読み取り権限がありません: {$filePath}");
    }
 
    if (!is_file($filePath)) {
      throw new RuntimeException("指定されたパスがファイルではありません: {$filePath}");
     }
     }
  }
  }
591行目: 721行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
==== 非同期処理 ====
==== 非同期処理 ====
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
606行目: 737行目:
   
   
     try {
     try {
      // ファイルの検証
      validateFile($sourceFile);
       switch ($type) {
       switch ($type) {
           case 'zip':
           case 'zip':
628行目: 762行目:
       yield "解凍処理中にエラーが発生しました: {$e->getMessage()}";
       yield "解凍処理中にエラーが発生しました: {$e->getMessage()}";
       throw $e;
       throw $e;
    }
}
/**
  * ファイルの存在確認と権限チェックを行うプライベートメソッド
  *
  * @param string $filePath チェック対象のファイルパス
  * @throws RuntimeException チェック失敗時
  */
function validateFile(string $filePath): void
{
    if (!file_exists($filePath)) {
      throw new RuntimeException("ファイルが存在しません: {$filePath}");
    }
 
    if (!is_readable($filePath)) {
      throw new RuntimeException("ファイルの読み取り権限がありません: {$filePath}");
    }
 
    if (!is_file($filePath)) {
      throw new RuntimeException("指定されたパスがファイルではありません: {$filePath}");
     }
     }
  }
  }