0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CodeIgniterのZIP Library

Posted at

CodeIgniterのZIP Libraryは、ZIPアーカイブファイルの作成や操作を簡単に行うためのライブラリです。

基本的な使い方

ライブラリの読み込み

$this->load->library('zip');

主要なメソッド

ファイルの追加

// 単一ファイルを追加
$this->zip->add_data('filename.txt', 'ファイルの内容');

// 複数ファイルを追加
$data = array(
    'file1.txt' => 'ファイル1の内容',
    'file2.txt' => 'ファイル2の内容'
);
$this->zip->add_data($data);

既存ファイルの追加

$this->zip->read_file('/path/to/file.txt');

ディレクトリの追加

$this->zip->read_dir('/path/to/directory/');

ZIPファイルのダウンロード

$this->zip->download('archive.zip');

ZIPファイルの保存

$this->zip->archive('/path/to/save/archive.zip');

実用的な例

class FileController extends CI_Controller {
    
    public function create_zip() {
        $this->load->library('zip');
        
        // データベースからデータを取得してCSVファイルを作成
        $csv_data = $this->generate_csv_data();
        $this->zip->add_data('report.csv', $csv_data);
        
        // 画像ファイルを追加
        $this->zip->read_file('./uploads/image.jpg');
        
        // テキストファイルを動的に作成して追加
        $readme = "このアーカイブにはレポートと関連ファイルが含まれています。";
        $this->zip->add_data('README.txt', $readme);
        
        // ZIPファイルをダウンロード
        $this->zip->download('report_package.zip');
    }
}

注意点

  • メモリ使用量: 大きなファイルを扱う際は、メモリ制限に注意が必要です
  • ファイルパス: read_file()read_dir()を使用する際は、適切なファイルパスを指定してください
  • 文字エンコーディング: 日本語ファイル名を扱う場合は、文字エンコーディングに注意が必要です

ZIP Libraryは簡単な操作でアーカイブファイルを作成できるため、レポートの一括ダウンロード機能やバックアップ機能の実装に便利です。​​​​​​​​​​​​​​​​

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?