SQLの窓

2011年06月11日


Pear を使用してダウンロード時のみ zip 圧縮してブラウザにデータを送る( プログレスバーはでません )

Pear のインストールは Windows 環境で Pear のインストール を参照して下さい。インストールは Windows ベースですが、Pear ディレクトリ内のライブラリをレンタルサーバーにアップロードして動作させる事ができます。

※ zip 圧縮には、File_Archive が必要です

このサンプルでは、相対参照によるファイルのパス一覧を配列にセットする事によって全てを zip 圧縮してダウンロードさせます

$files[] = "./" の指定で、中にあるサブフォルダも対象となります。

圧縮という仕様上、ファイルサイズを content-length で送る事ができないので、ダウンロード時にプログレスバーが出る事はありません。どうしても必要な場合は、zip ファイルを一時名でサーバーで作成して、そのファイルを読み込んでダウンロードすれば良いと思いますが、効率が良いとはけっして言えないので小さなファイルならばこれで十分だと思います
<?php
// *********************************************
// PHP 5.3 以降で必要
// *********************************************
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE );

// *********************************************
// Pear ライブリを使用する為に、include パスを設定し、
// 必要なライブラリを読み込む
// *********************************************
set_include_path( "c:/php5217/PEAR" );
require_once "File/Archive.php";

// *********************************************
// 圧縮レベル
// "zipCompressionLevel"
//		Value between 0 and 9 specifying the default 
//		compression level used by Zip writers 
//		(0 no compression, 9 highest compression)
//		Default: 9
// *********************************************
File_Archive::setOption('zipCompressionLevel', 9);

// ダウンロード用アーカイブを決定
$zip = File_Archive::toArchive(
	"zip_sample.zip",
	File_Archive::toOutput()
);

// このディレクトリそのものを追加する
$files[] = "./";
// 別のディレクトリのファイルを追加
$files[] = "../../twitter/callback.php";

File_Archive::extract( 
	$files, $zip
);

?>


※ 単独ファイルの場合は、二つ以上の上の階層まで読みだせるのですが、
※ ディレクトリの場合は、一つ上の階層までしか読めませんでした。
※ ディレクトリが読める場合は、"*.php" のようなワイルドカードを指定できます


▼ 以下は、setOption のオプションの一覧です
"cache"
	Instance of a Cache_Lite object used to cache
	some compressed	data to speed up future compressions
	of files
	Default: null (no cache used)

"zipCompressionLevel"
	Value between 0 and 9 specifying the default
	compression level used by Zip writers
	(0 no compression, 9 highest compression)
	Default: 9

"gzCompressionLevel"
	Value between 0 and 9 specifying the default
	compression level used by Gz writers 
	(0 no compression, 9 highest compression)
	Default: 9

"tmpDirectory"
	Directory where the temporary files generated by
	File_Archive will be created
	Default: '.'

"appendRemoveDuplicates"
	If set to true, the appender created will by 
	default remove the file present in the archive when
	adding a new one. This will slow the appending of
	files to archives
	Default: false

"blockSize"
	To transfer data from a reader to a writer, some
	chunks a read from the source and written to the
	writer. This parameter controls the size of the	chunks
	Default: 64kB

"cacheCondition"
	This parameter specifies when a cache should be used.
	When the cache is used, the data of the reader is
	saved in a temporary file for future access.The cached
	reader will be read only once, even if you read
	it several times.This can be usefull to read compressed
	files or downloaded files (from http or ftp)
	The possible values for this option are
	 - false: never use cache
	 - a regexp: A cache will be used if the specified
		URL matches the regexp
	preg_match is used
	Default: false
	Example: '/^(http|ftp):\/\//' will cache all files
		downloaded via http or ftp

以下は、動的な単独ファイルの追加と、配列の変わりに File_Archive::readMultiを使用しています
<?php
// *********************************************
// PHP 5.3 以降で必要
// *********************************************
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE );

// *********************************************
// Pear ライブリを使用する為に、include パスを設定し、
// 必要なライブラリを読み込む
// *********************************************
set_include_path( "c:/php5217/PEAR" );
require_once "File/Archive.php";

// *********************************************
// 圧縮レベル
// "zipCompressionLevel"
//		Value between 0 and 9 specifying the default 
//		compression level used by Zip writers 
//		(0 no compression, 9 highest compression)
//		Default: 9
// *********************************************
File_Archive::setOption('zipCompressionLevel', 9);

// ダウンロード用アーカイブを決定
$zip = File_Archive::toArchive(
	"zip_sample.zip",
	File_Archive::toOutput()
);

// 動的な追加
$zip->newFile("mydata/readme.txt");
$zip->writeData( "My name is lightbox." ); 
$zip->close();

File_Archive::extract( 
	File_Archive::readMulti(
		array(
			// このディレクトリそのものを追加する
			File_Archive::read('./'),
			// 別のディレクトリのファイルを追加
			File_Archive::read('../../twitter_api/GetAccessToken/phase_1.php'),
			// 一つ上のディレクトリの内容を追加
			File_Archive::read('../twitter/*.php' )
		)
	), $zip
);

?>




【PHPの最新記事】
posted by lightbox at 2011-06-11 20:01 | PHP | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり