RarArchive::open

rar_open

(PECL rar >= 2.0.0)

RarArchive::open -- rar_openOpen RAR archive

설명

객체 기반 형식 (method):

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

절차식 형식:

RarArchive rar_open ( string $filename [, string $password = NULL [, callable $volume_callback = NULL ]] )

Open specified RAR archive and return RarArchive instance representing it.

Note:

If opening a multi-volume archive, the path of the first volume should be passed as the first parameter. Otherwise, not all files will be shown. This is by design.

인수

filename

Path to the Rar archive.

password

A plain password, if needed to decrypt the headers. It will also be used by default if encrypted files are found. Note that the files may have different passwords in respect to the headers and among them.

volume_callback

A function that receives one parameter – the path of the volume that was not found – and returns a string with the correct path for such volume or NULL if such volume does not exist or is not known. The programmer should ensure the passed function doesn't cause loops as this function is called repeatedly if the path returned in a previous call did not correspond to the needed volume. Specifying this parameter omits the notice that would otherwise be emitted whenever a volume is not found; an implementation that only returns NULL can therefore be used to merely omit such notices.

Warning

Prior to version 2.0.0, this function would not handle relative paths correctly. Use realpath() as a workaround.

반환값

Returns the requested RarArchive instance 실패 시 FALSE를 반환합니다.

변경점

버전 설명
3.0.0 volume_callback was added.

예제

Example #1 객체 기반 형식

<?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.

Example #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);
?>

Example #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(nulldirname(__FILE__) . "/temp_file2.txt");
?>

add a note add a note

User Contributed Notes 1 note

up
1
ohcc at 163 dot com
9 years ago
I'm just confused why Rar::open() would be static, there should be a constructor instead.

I think $rar = new RarArchive(...); is much better than $rar = RarArchive::open(...); that is the real OOP style.
To Top