RarArchiveReader.php
1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
 * Created by PhpStorm.
 * User: Tsurkanov
 * Date: 03.11.2015
 * Time: 15:12
 */
namespace common\components\archives;
class RarArchiveReader extends  ArchiveReader {
    /**
     * @param $file - имя открываемого архива
     * @param string $password
     * @return bool
     * @throws \Exception
     */
    public  function open( $file, $password = '' ){
        $this->resource = \RarArchive::open( $file, $password );
        if ($this->resource === FALSE)
            throw new \Exception("Failed opening rar file {$file}");
        return true;
    }
    /**
     * @param $destination - путь куда положить распакованные файлы
     * если указан аттрибут $file_name_prefix то происходит переименование файла
     * в результате выполнения метода заполняется аттрибут $extracted_files извлеченными файлами
     * если извлечение не произошло аттрибут $extracted_files - пустой
     */
    public  function extractTo( $destination){
        $list = $this->resource->getEntries();
        foreach($list as $entry) {
            $new_destination = $destination . '/'  . $this->file_name_prefix .$entry->getName();
            $entry->extract( $destination, $new_destination );
            $this->setExtractedFiles( $new_destination, pathinfo( $entry->getName(), PATHINFO_EXTENSION ) );
        }
        $this->resource->close();
    }
    public static  function getExtension(){
        return 'rar';
    }
}