Projet

Général

Profil

Paste
Télécharger (3,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / system / system.archiver.inc @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Archiver implementations provided by the system module.
6
 */
7

    
8
/**
9
 * Archiver for .tar files.
10
 */
11
class ArchiverTar implements ArchiverInterface {
12

    
13
  /**
14
   * The underlying Archive_Tar instance that does the heavy lifting.
15
   *
16
   * @var Archive_Tar
17
   */
18
  protected $tar;
19

    
20
  public function __construct($file_path) {
21
    $this->tar = new Archive_Tar($file_path);
22
  }
23

    
24
  public function add($file_path) {
25
    $this->tar->add($file_path);
26

    
27
    return $this;
28
  }
29

    
30
  public function remove($file_path) {
31
    // @todo Archive_Tar doesn't have a remove operation
32
    // so we'll have to simulate it somehow, probably by
33
    // creating a new archive with everything but the removed
34
    // file.
35

    
36
    return $this;
37
  }
38

    
39
  public function extract($path, Array $files = array()) {
40
    if ($files) {
41
      $this->tar->extractList($files, $path);
42
    }
43
    else {
44
      $this->tar->extract($path);
45
    }
46

    
47
    return $this;
48
  }
49

    
50
  public function listContents() {
51
    $files = array();
52
    foreach ($this->tar->listContent() as $file_data) {
53
      $files[] = $file_data['filename'];
54
    }
55
    return $files;
56
  }
57

    
58
  /**
59
   * Retrieve the tar engine itself.
60
   *
61
   * In some cases it may be necessary to directly access the underlying
62
   * Archive_Tar object for implementation-specific logic. This is for advanced
63
   * use only as it is not shared by other implementations of ArchiveInterface.
64
   *
65
   * @return
66
   *   The Archive_Tar object used by this object.
67
   */
68
  public function getArchive() {
69
    return $this->tar;
70
  }
71
}
72

    
73
/**
74
 * Archiver for .zip files.
75
 *
76
 * @link http://php.net/zip
77
 */
78
class ArchiverZip implements ArchiverInterface {
79

    
80
  /**
81
   * The underlying ZipArchive instance that does the heavy lifting.
82
   *
83
   * @var ZipArchive
84
   */
85
  protected $zip;
86

    
87
  public function __construct($file_path) {
88
    $this->zip = new ZipArchive();
89
    if ($this->zip->open($file_path) !== TRUE) {
90
      // @todo: This should be an interface-specific exception some day.
91
      throw new Exception(t('Cannot open %file_path', array('%file_path' => $file_path)));
92
    }
93
  }
94

    
95
  public function add($file_path) {
96
    $this->zip->addFile($file_path);
97

    
98
    return $this;
99
  }
100

    
101
  public function remove($file_path) {
102
    $this->zip->deleteName($file_path);
103

    
104
    return $this;
105
  }
106

    
107
  public function extract($path, Array $files = array()) {
108
    if ($files) {
109
      $this->zip->extractTo($path, $files);
110
    }
111
    else {
112
      $this->zip->extractTo($path);
113
    }
114

    
115
    return $this;
116
  }
117

    
118
  public function listContents() {
119
    $files = array();
120
    for ($i=0; $i < $this->zip->numFiles; $i++) {
121
      $files[] = $this->zip->getNameIndex($i);
122
    }
123
    return $files;
124
  }
125

    
126
  /**
127
   * Retrieve the zip engine itself.
128
   *
129
   * In some cases it may be necessary to directly access the underlying
130
   * ZipArchive object for implementation-specific logic. This is for advanced
131
   * use only as it is not shared by other implementations of ArchiveInterface.
132
   *
133
   * @return
134
   *   The ZipArchive object used by this object.
135
   */
136
  public function getArchive() {
137
    return $this->zip;
138
  }
139
}