ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
ZipStreamWrapper.php
Go to the documentation of this file.
1 <?php
42  private $_archive;
43 
49  private $_fileNameInArchive = '';
50 
56  private $_position = 0;
57 
63  private $_data = '';
64 
68  public static function register() {
69  @stream_wrapper_unregister("zip");
70  @stream_wrapper_register("zip", __CLASS__);
71  }
72 
76  public function stream_open($path, $mode, $options, &$opened_path) {
77  // Check for mode
78  if ($mode{0} != 'r') {
79  throw new Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
80  }
81 
82  $pos = strrpos($path, '#');
83  $url['host'] = substr($path, 6, $pos - 6); // 6: strlen('zip://')
84  $url['fragment'] = substr($path, $pos + 1);
85 
86  // Open archive
87  $this->_archive = new ZipArchive();
88  $this->_archive->open($url['host']);
89 
90  $this->_fileNameInArchive = $url['fragment'];
91  $this->_position = 0;
92  $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive );
93 
94  return true;
95  }
96 
100  public function stream_stat() {
101  return $this->_archive->statName( $this->_fileNameInArchive );
102  }
103 
107  function stream_read($count) {
108  $ret = substr($this->_data, $this->_position, $count);
109  $this->_position += strlen($ret);
110  return $ret;
111  }
112 
116  public function stream_tell() {
117  return $this->_position;
118  }
119 
123  public function stream_eof() {
124  return $this->_position >= strlen($this->_data);
125  }
126 
130  public function stream_seek($offset, $whence) {
131  switch ($whence) {
132  case SEEK_SET:
133  if ($offset < strlen($this->_data) && $offset >= 0) {
134  $this->_position = $offset;
135  return true;
136  } else {
137  return false;
138  }
139  break;
140 
141  case SEEK_CUR:
142  if ($offset >= 0) {
143  $this->_position += $offset;
144  return true;
145  } else {
146  return false;
147  }
148  break;
149 
150  case SEEK_END:
151  if (strlen($this->_data) + $offset >= 0) {
152  $this->_position = strlen($this->_data) + $offset;
153  return true;
154  } else {
155  return false;
156  }
157  break;
158 
159  default:
160  return false;
161  }
162  }
163 }