ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ZipStreamWrapper.php
Go to the documentation of this file.
1 <?php
31 
32 
46  private $_archive;
47 
53  private $_fileNameInArchive = '';
54 
60  private $_position = 0;
61 
67  private $_data = '';
68 
72  public static function register() {
73  @stream_wrapper_unregister("zip");
74  @stream_wrapper_register("zip", __CLASS__);
75  }
76 
80  public function stream_open($path, $mode, $options, &$opened_path) {
81  // Check for mode
82  if ($mode{0} != 'r') {
83  throw new Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
84  }
85 
86  // Parse URL
87  $url = @parse_url($path);
88 
89  // Fix URL
90  if (!is_array($url)) {
91  $url['host'] = substr($path, strlen('zip://'));
92  $url['path'] = '';
93  }
94  if (strpos($url['host'], '#') !== false) {
95  if (!isset($url['fragment'])) {
96  $url['fragment'] = substr($url['host'], strpos($url['host'], '#') + 1) . $url['path'];
97  $url['host'] = substr($url['host'], 0, strpos($url['host'], '#'));
98  unset($url['path']);
99  }
100  } else {
101  $url['host'] = $url['host'] . $url['path'];
102  unset($url['path']);
103  }
104 
105  // Open archive
106  $this->_archive = new ZipArchive();
107  $this->_archive->open($url['host']);
108 
109  $this->_fileNameInArchive = $url['fragment'];
110  $this->_position = 0;
111  $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive );
112 
113  return true;
114  }
115 
119  public function stream_stat() {
120  return $this->_archive->statName( $this->_fileNameInArchive );
121  }
122 
126  function stream_read($count) {
127  $ret = substr($this->_data, $this->_position, $count);
128  $this->_position += strlen($ret);
129  return $ret;
130  }
131 
135  public function stream_tell() {
136  return $this->_position;
137  }
138 
142  public function stream_eof() {
143  return $this->_position >= strlen($this->_data);
144  }
145 
149  public function stream_seek($offset, $whence) {
150  switch ($whence) {
151  case SEEK_SET:
152  if ($offset < strlen($this->_data) && $offset >= 0) {
153  $this->_position = $offset;
154  return true;
155  } else {
156  return false;
157  }
158  break;
159 
160  case SEEK_CUR:
161  if ($offset >= 0) {
162  $this->_position += $offset;
163  return true;
164  } else {
165  return false;
166  }
167  break;
168 
169  case SEEK_END:
170  if (strlen($this->_data) + $offset >= 0) {
171  $this->_position = strlen($this->_data) + $offset;
172  return true;
173  } else {
174  return false;
175  }
176  break;
177 
178  default:
179  return false;
180  }
181  }
182 }