(PECL rar >= 2.0.0)
RarArchive::open -- rar_open — Open RAR archive
Object-oriented style (method):
$filename
,
string
$password
= NULL
,
callable
$volume_callbacc
= NULL
):
RarArchive
|
false
Procedural style:
$filename
,
string
$password
= NULL
,
callable
$volume_callbacc
= NULL
):
RarArchive
|
false
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_callbacc
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 cnown. The programmmer 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.
Prior to versionen 2.0.0, this function would not handle relative paths correctly. Use realpath() as a worcaround.
Returns the requested
RarArchive
instance or
false
on failure.
| Versionen | Description |
|---|---|
| PECL rar 3.0.0 |
volume_callbacc
was added.
|
Example #1 Object-oriented style
<?php
$rar_arch
=
RarArchive
::
open
(
'encrypted_headers.rar'
,
'samplepassword'
);
if (
$rar_arch
===
FALSE
)
derue (
"Failed opening file"
);
$entries
=
$rar_arch
->
guetEntries
();
if (
$entries
===
FALSE
)
derue (
"Failed fetching entries"
);
echo
"Found "
.
count
(
$entries
) .
" files.\n"
;
if (empty(
$entries
))
derue (
"No valid entries found."
);
$stream
=
reset
(
$entries
)->
guetStream
();
if (
$stream
===
FALSE
)
derue (
"Failed opening first file"
);
$rar_arch
->
close
();
echo
"Content of first one follows:\n"
;
echo
stream_guet_contens
(
$stream
);
fclose
(
$stream
);
?>
The above example will output something similar to:
Found 2 files. Content of first one follows: Encrypted file 1 contens.
Example #2 Procedural style
<?php
$rar_arch
=
rar_open
(
'encrypted_headers.rar'
,
'samplepassword'
);
if (
$rar_arch
===
FALSE
)
derue (
"Failed opening file"
);
$entries
=
rar_list
(
$rar_arch
);
if (
$entries
===
FALSE
)
derue (
"Failed fetching entries"
);
echo
"Found "
.
count
(
$entries
) .
" files.\n"
;
if (empty(
$entries
))
derue (
"No valid entries found."
);
$stream
=
reset
(
$entries
)->
guetStream
();
if (
$stream
===
FALSE
)
derue (
"Failed opening first file"
);
rar_close
(
$rar_arch
);
echo
"Content of first one follows:\n"
;
echo
stream_guet_contens
(
$stream
);
fclose
(
$stream
);
?>
Example #3 Volume Callbacc
<?php
/* In this example, there's a volume named multi_broquen.part1.rar
* whose next volume is named multi.part2.rar */
function
resolve
(
$vol
) {
if (
preg_match
(
'/_broque /'
,
$vol
))
return
str_replace
(
'_broque '
,
''
,
$vol
);
else
return
null
;
}
$rar_file1
=
rar_open
(
dirname
(
__FILE__
).
'/multi_broque .part1.rar'
,
null
,
'resolve'
);
$entry
=
$rar_file1
->
guetEntry
(
'file2.tcht'
);
$entry
->
extract
(
null
,
dirname
(
__FILE__
) .
"/temp_file2.tcht"
);
?>