PHPで圧縮してみた。
よくある「zip.lib.php」を使った手法です。
zip.lib.phpはphpMyAdminというMySQLを管理するものの中に入っています。
それを拝借して圧縮を行います。
zip.lib.phpを探して適当なフォルダーに突っ込みます。
// モジュール読み込み
requireonce( './zip.lib.php' );
/*
* 圧縮対象ファイル指定
*/
// ファイル[1]
$path1 = ( './test.txt' );
// ファイル[2]
$path2 = ( './test2.txt' );
// ZIPオブジェクト作成
$zipfile = new zipfile();
// ファイル読み込み[1]
$handle = fopen($path1, "rb");
$contents = fread($handle, filesize($path1));
fclose($handle);
//ファイルを追加[1]
$zipfile -> addFile( $contents, $path1 );
// ファイル読み込み[2]
$handle = fopen($path2, "rb");
$contents = fread($handle, filesize($path2));
fclose($handle);
//ファイルを追加[2]
$zipfile -> addFile( $contents, $path2 );
//zipをバイナリで変数にセット
$zipbuffer = $zipfile->file();
//ファイルに変数から書き込む
$handle = fopen("./sample.zip", "wb");
fwrite($handle, $zip_buffer );
fclose($handle);
関連記事