Blame view

common/components/archives/ZipArchiveReader.php 1.75 KB
4f3f27e8   Mihail   temp commit - tes...
1
2
3
4
5
6
7
8
9
10
11
12
13
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 03.11.2015
   * Time: 15:12
   */
  
  namespace common\components\archives;
  
  
  class ZipArchiveReader extends  ArchiveReader {
  
d95262f3   Mihail   finish with mail ...
14
15
16
17
18
19
      /**
       * @param $file - имя открываемого архива
       * @param string $password
       * @return bool
       * @throws \Exception
       */
4f3f27e8   Mihail   temp commit - tes...
20
      public  function open( $file, $password = '' ){
4f3f27e8   Mihail   temp commit - tes...
21
  
d95262f3   Mihail   finish with mail ...
22
23
24
25
26
27
28
29
30
31
          $this->resource = new \ZipArchive;
          if ( $this->resource->open( $file ) === true )
              return true;
  
          $error_number = $this->resource;
              throw new \Exception("Failed opening zip file, error # {$error_number}");
  
  
  
      }
4f3f27e8   Mihail   temp commit - tes...
32
  
d95262f3   Mihail   finish with mail ...
33
34
35
36
37
38
39
      /**
       * @param $destination - путь куда положить распакованные файлы
       * если указан аттрибут $file_name_prefix то происходит переименование файла
       * в результате выполнения метода заполняется аттрибут $extracted_files извлеченными файлами
       * если извлечение не произошло аттрибут $extracted_files - пустой
       */
      public  function extractTo( $destination ){
4f3f27e8   Mihail   temp commit - tes...
40
41
42
  
          for ($i = 0; $i < $this->resource->numFiles; $i++) {
              $filename = $this->resource->getNameIndex($i);
d95262f3   Mihail   finish with mail ...
43
44
45
46
47
48
49
50
51
              $this->resource->renameName($filename,$this->file_name_prefix .$filename);
              $this->setExtractedFiles($destination . '/' . $filename, pathinfo($filename, PATHINFO_EXTENSION));
  
          }
  
          if (! $this->resource->extractTo( $destination ) ) {
  
              $this->extracted_files = [];
  
4f3f27e8   Mihail   temp commit - tes...
52
53
54
55
          }
  
          $this->resource->close();
      }
d95262f3   Mihail   finish with mail ...
56
  
4f3f27e8   Mihail   temp commit - tes...
57
      public static  function getExtension(){
d95262f3   Mihail   finish with mail ...
58
          return 'zip';
4f3f27e8   Mihail   temp commit - tes...
59
60
61
62
63
64
      }
  
  
  
  
  }