RarEntry
在线手册:中文 英文
PHP手册

RarEntry::extract

(PECL rar >= 0.1)

RarEntry::extractExtract entry from the archive

说明

public bool RarEntry::extract ( string $dir [, string $filepath = '' [, string $password = NULL [, bool $extended_data = false ]]] )

RarEntry::extract() extracts the entry's data. It will create new file in the specified dir with the name identical to the entry's name, unless the second argument is specified. See below for more information.

参数

dir

Path to the directory where files should be extracted. This parameter is considered if and only if filepath is not. If both parameters are empty an extraction to the current directory will be attempted.

filepath

Path (relative or absolute) containing the directory and filename of the extracted file. This parameter overrides both the parameter dir and the original file name.

password

The password used to encrypt this entry. If the entry is not encrypted, this value will not be used and can be omitted. If this parameter is omitted and the entry is encrypted, the password given to rar_open(), if any, will be used. If a wrong password is given, either explicitly or implicitly via rar_open(), CRC checking will fail and this method will fail and return FALSE. If no password is given and one is required, this method will fail and return FALSE. You can check whether an entry is encrypted with RarEntry::isEncrypted().

extended_data

If TRUE, extended information such as NTFS ACLs and Unix owner information will be set in the extract files, as long as it's present in the archive.

Warning

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

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE.

更新日志

版本 说明
3.0.0 extended_data was added.
3.0.0 Support for RAR archives with repeated entry names is no longer defective.

范例

Example #1 RarEntry::extract() example

<?php

$rar_file 
rar_open('example.rar') or die("Failed to open Rar archive");

$entry rar_entry_get($rar_file'Dir/file.txt') or die("Failed to find such entry");

$entry->extract('/dir/to'); // create /dir/to/Dir/file.txt
$entry->extract(false'/dir/to/new_name.txt'); // create /dir/to/new_name.txt

?>

Example #2 How to extract all files in archive:

<?php

/* example by Erik Jenssen aka erix */

$filename "foobar.rar";
$filepath "/home/foo/bar/";

$rar_file rar_open($filepath.$filename);
$list rar_list($rar_file);
foreach(
$list as $file) {
    
$entry rar_entry_get($rar_file$file);
    
$entry->extract("."); // extract to the current dir
}
rar_close($rar_file);

?>

参见


RarEntry
在线手册:中文 英文
PHP手册
PHP手册 - N: Extract entry from the archive

用户评论:

thankful at iam dot com (28-Mar-2012 06:38)

And for someone who "knows how to code" you are having to come and use this extension! Funny I do not see your alternative here, correctly coded or otherwise, and I also fail to see why his "mixed" version which DOES work is wrong and bad practice, does it work, does it blow up someone's server, does it cause the national debt to increase exponentially, does it carry the plague, or is it some malevolent code aimed at taking over the world?

I am pretty sure you get the idea by now. Maybe you are too full of yourself, just a little yes?

This coder has made quite a few mistakes in their examples for this extension, if you look through all related pages you will see some quite bad ones and it's severely lacking in documentation too, but, and a rather large but, at least they had the foresight and decency to share with us all, as it seems no matter how arrogant we are (by we I do infer YOU), we have not created our own alternative rar manipulation code, have we?

Thank you to the creator of the code, creating examples may not be your forte but delivering extremely useful and working extensions is!

Anonymous (28-Nov-2011 07:18)

Of course, mixing procedural and object oriented style is bad practice, so a better example to extract all files is:

<?php

/* example by somebody who does know how to code */

$filename = 'foobar.rar';
$filepath = '/home/foo/bar/';

$rar_file = RarArchive::open($filepath.$filename);

foreach(
$rar_file->getEntries() as $entry)
{
 
$entry->extract(''); // extract to the current dir
}
$rar_file->close();

?>