PHP8 RarArchive::open

2024-02-23 11:45 更新

rar_open

(PECL rar >= 2.0.0)

RarArchive::open -- rar_open — 打開 RAR 存檔

說明 

面向?qū)ο箫L格 (method):

public static RarArchive::open(string $filename, string $password = NULL, callable $volume_callback = NULL): RarArchive|false

過程化風格:

rar_open(string $filename, string $password = NULL, callable $volume_callback = NULL): RarArchive|false

打開指定的 RAR 存檔并返回表示它的 RarArchive 實例。

注意:如果打開多卷歸檔文件,則應(yīng)將第一個卷的路徑作為第一個參數(shù)傳遞。 否則,不會顯示所有文件。這是設(shè)計使然。

參數(shù) 

filename

Rar 存檔的路徑。

password

一個普通的密碼,如果需要解密標頭。默認情況下也會使用它 如果找到加密文件。請注意,這些文件可能具有 關(guān)于標頭和標頭之間的不同密碼。

volume_callback

接收一個參數(shù)的函數(shù) – 卷的路徑 未找到 – 并返回具有正確路徑的字符串 對于此類卷,如果此類卷為 null,則為 null 不存在或未知。程序員應(yīng)確保 傳遞的函數(shù)不會導致循環(huán),因為此函數(shù)被調(diào)用 如果上一個調(diào)用中返回的路徑未返回,則重復 對應(yīng)于所需的體積。指定此參數(shù)將省略 每當卷 未找到;因此,僅返回 null 的實現(xiàn)可用于省略此類通知。

警告

在版本 2.0.0 之前,此函數(shù)不會處理相對 正確的路徑。使用 realpath() 作為解決方法。

返回值 

Returns the requested RarArchive instance 或者在失敗時返回 false.

更新日志 

版本說明
PECL rar 3.0.0volume_callback被添加。

示例 

示例 #1 面向?qū)ο箫L格

<?php
$rar_arch = RarArchive::open('encrypted_headers.rar', 'samplepassword');
if ($rar_arch === FALSE)
    die("Failed opening file");
    
$entries = $rar_arch->getEntries();
if ($entries === FALSE)
    die("Failed fetching entries");

echo "Found " . count($entries) . " files.\n";

if (empty($entries))
    die("No valid entries found.");
    
$stream = reset($entries)->getStream();
if ($stream === FALSE)
    die("Failed opening first file");

$rar_arch->close();

echo "Content of first one follows:\n";
echo stream_get_contents($stream);

fclose($stream);
?>

以上示例的輸出類似于:

Found 2 files.
Content of first one follows:
Encrypted file 1 contents.

示例 #2 過程化風格

<?php
$rar_arch = rar_open('encrypted_headers.rar', 'samplepassword');
if ($rar_arch === FALSE)
    die("Failed opening file");
    
$entries = rar_list($rar_arch);
if ($entries === FALSE)
    die("Failed fetching entries");

echo "Found " . count($entries) . " files.\n";

if (empty($entries))
    die("No valid entries found.");
    
$stream = reset($entries)->getStream();
if ($stream === FALSE)
    die("Failed opening first file");

rar_close($rar_arch);

echo "Content of first one follows:\n";
echo stream_get_contents($stream);

fclose($stream);
?>

示例 #3 Volume Callback

<?php
/* In this example, there's a volume named multi_broken.part1.rar
 * whose next volume is named multi.part2.rar */
function resolve($vol) {
    if (preg_match('/_broken/', $vol))
        return str_replace('_broken', '', $vol);
    else
        return null;
}
$rar_file1 = rar_open(dirname(__FILE__).'/multi_broken.part1.rar', null, 'resolve');
$entry = $rar_file1->getEntry('file2.txt');
$entry->extract(null, dirname(__FILE__) . "/temp_file2.txt");
?>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號