2024 年日本 PHP 研討會

範例

另請參閱 rar:// 包裝器 下的範例。

範例 #1 動態解壓縮

<?php

if (!array_key_exists("i", $_GET) || !is_numeric($_GET["i"]))
die(
"Index unspecified or non-numeric");
$index = (int) $_GET["i"];

$arch = RarArchive::open("example.rar");
if (
$arch === FALSE)
die(
"Cannot open example.rar");

$entries = $arch->getEntries();
if (
$entries === FALSE)
die(
"Cannot retrieve entries");

if (!
array_key_exists($index, $entries))
die(
"No such index: $index");

$orfilename = $entries[$index]->getName(); //UTF-8 encoded

$filesize = $entries[$index]->getUnpackedSize();

/* you could check HTTP_IF_MODIFIED_SINCE here and compare with
* $entries[$index]->getFileTime(). You could also send a
* "Last modified" header */

$fp = $entries[$index]->getStream();
if (
$fp === FALSE)
die(
"Cannot open file with index $index insided the archive.");

$arch->close(); //no longer needed; stream is independent

function detectUserAgent() {
if (!
array_key_exists('HTTP_USER_AGENT', $_SERVER))
return
"Other";

$uas = $_SERVER['HTTP_USER_AGENT'];
if (
preg_match("@Opera/@", $uas))
return
"Opera";
if (
preg_match("@Firefox/@", $uas))
return
"Firefox";
if (
preg_match("@Chrome/@", $uas))
return
"Chrome";
if (
preg_match("@MSIE ([0-9.]+);@", $uas, $matches)) {
if (((float)
$matches[1]) >= 7.0)
return
"IE";
}

return
"Other";
}

/*
* We have 3 options:
* - For FF and Opera, which support RFC 2231, use that format.
* - For IE and Chrome, use attwithfnrawpctenclong
* (http://greenbytes.de/tech/tc2231/#attwithfnrawpctenclong)
* - For the others, convert to ISO-8859-1, if possible
*/
$formatRFC2231 = 'Content-Disposition: attachment; filename*=UTF-8\'\'%s';
$formatDef = 'Content-Disposition: attachment; filename="%s"';

switch (
detectUserAgent()) {
case
"Opera":
case
"Firefox":
$orfilename = rawurlencode($orfilename);
$format = $formatRFC2231;
break;

case
"IE":
case
"Chrome":
$orfilename = rawurlencode($orfilename);
$format = $formatDef;
break;
default:
if (
function_exists('iconv'))
$orfilename =
@
iconv("UTF-8", "ISO-8859-1//TRANSLIT", $orfilename);
$format = $formatDef;
}

header(sprintf($format, $orfilename));
//cannot send error messages from now on (headers already sent)

//replace by real content type, perhaps infering from the file extension
$contentType = "application/octet-stream";
header("Content-Type: $contentType");

header("Content-Transfer-Encoding: binary");

header("Content-Length: $filesize");

if (
$_SERVER['REQUEST_METHOD'] == "HEAD")
die();

while (!
feof($fp)) {
$s = @fread($fp, 8192);
if (
$s === false)
break;
//useless to send error messages

echo $s;
}
?>

此範例開啟一個 RAR 檔案,並將 RAR 封存檔中請求的檔案呈現出來,供用戶端下載。

範例 #2 RAR 擴充檔案系統解壓縮範例

<?php

$rar_file
= rar_open('example.rar') or die("無法開啟 Rar 壓縮檔");

$entries = rar_list($rar_file);

foreach (
$entries as $entry) {
echo
'檔案名稱: ' . $entry->getName() . "\n";
echo
'壓縮後大小: ' . $entry->getPackedSize() . "\n";
echo
'解壓縮後大小: ' . $entry->getUnpackedSize() . "\n";

$entry->extract('/dir/extract/to/');
}

rar_close($rar_file);

?>

這個範例會開啟一個 RAR 壓縮檔,並將每個項目解壓縮到指定的目錄。

新增註記

使用者貢獻的註記 1 則註記

1
Nitrogen
14 年前
一個非常簡單的 RAR 檔案函式,我不以它為傲。
由於無法在 PHP 中建立 RAR 檔案(由於授權、專利或其他原因),我正在利用 WinRAR 附帶的命令列 RAR 工具(位於 WinRAR 程式檔案中,名為「rar.exe」)。

<?php
函數 RARFiles($Output='output.rar',$Files=array()) {
$Data='';
for(
$i=0;$i<count($Files);$i++)
$Data.="\"{$Files[$i]}\" ";
exec("rar.exe a \"{$Output}\" {$Data}");
}

$Files=array('file1.ext','file2.ext','file3.ext');
RARFiles('asdf.rar',$Files);
// 已建立 asdf.rar。
?>

這段程式碼沒有錯誤檢查,因此請務必在使用 RAR 檔案之前確認預期的 RAR 檔案是否存在。
希望有一天,PHP 能夠被允許直接建立 RAR 檔案。
To Top