PHPExcel_Shared
[ class tree: PHPExcel_Shared ] [ index: PHPExcel_Shared ] [ all elements ]

Source for file ZipStreamWrapper.php

Documentation is available at ZipStreamWrapper.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Shared
  23.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  26.  */
  27.  
  28.  
  29. /** Register new zip wrapper */
  30.  
  31.  
  32. /**
  33.  * PHPExcel_Shared_ZipStreamWrapper
  34.  *
  35.  * @category   PHPExcel
  36.  * @package    PHPExcel_Shared
  37.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  38.  */
  39.     /**
  40.      * Internal ZipAcrhive
  41.      *
  42.      * @var ZipAcrhive 
  43.      */
  44.     private $_archive;
  45.     
  46.     /**
  47.      * Filename in ZipAcrhive
  48.      *
  49.      * @var string 
  50.      */
  51.     private $_fileNameInArchive = '';
  52.     
  53.     /**
  54.      * Position in file
  55.      *
  56.      * @var int 
  57.      */
  58.     private $_position = 0;
  59.     
  60.     /**
  61.      * Data
  62.      *
  63.      * @var mixed 
  64.      */
  65.     private $_data = '';
  66.     
  67.     /**
  68.      * Register wrapper
  69.      */
  70.     public static function register() {
  71.         @stream_wrapper_unregister("zip");
  72.         @stream_wrapper_register("zip", __CLASS__);
  73.     }
  74.     
  75.     /**
  76.      * Open stream
  77.      */
  78.     public function stream_open($path, $mode, $options, &$opened_path) {
  79.         // Check for mode
  80.         if (substr($mode, 0, 1) != 'r') {
  81.             throw new Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
  82.         }
  83.         
  84.         // Parse URL
  85.         $url = @parse_url($path);
  86.         
  87.         // Fix URL
  88.         if (!is_array($url)) {
  89.             $url['host'] = substr($path, strlen('zip://'));
  90.             $url['path'] = '';
  91.         }
  92.         if (strpos($url['host'], '#') !== false) {
  93.             if (!isset($url['fragment'])) {
  94.                 $url['fragment']    = substr($url['host'], strpos($url['host'], '#') + 1) . $url['path'];
  95.                 $url['host']        = substr($url['host'], 0, strpos($url['host'], '#'));
  96.                 unset($url['path']);
  97.             }
  98.         }
  99.  
  100.         // Open archive
  101.         $this->_archive = new ZipArchive();
  102.         $this->_archive->open($url['host']);
  103.        
  104.         $this->_fileNameInArchive = $url['fragment'];
  105.         $this->_position = 0;
  106.         $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive );
  107.         
  108.         return true;
  109.     }
  110.     
  111.     /**
  112.      * Stat stream
  113.      */
  114.     public function stream_stat() {
  115.         return $this->_archive->statName( $this->_fileNameInArchive );
  116.     }
  117.     
  118.     /**
  119.      * Read stream
  120.      */
  121.     function stream_read($count) {
  122.         $ret = substr($this->_data, $this->_position, $count);
  123.         $this->_position += strlen($ret);
  124.         return $ret;
  125.     }
  126.  
  127.     /**
  128.      * Tell stream
  129.      */
  130.     public function stream_tell() {
  131.         return $this->_position;
  132.     }
  133.  
  134.     /**
  135.      * EOF stream
  136.      */
  137.     public function stream_eof() {
  138.         return $this->_position >= strlen($this->_data);
  139.     }
  140.  
  141.     /**
  142.      * Seek stream
  143.      */
  144.     public function stream_seek($offset, $whence) {
  145.         switch ($whence) {
  146.             case SEEK_SET:
  147.                 if ($offset < strlen($this->_data) && $offset >= 0) {
  148.                      $this->_position = $offset;
  149.                      return true;
  150.                 } else {
  151.                      return false;
  152.                 }
  153.                 break;
  154.                
  155.             case SEEK_CUR:
  156.                 if ($offset >= 0) {
  157.                      $this->_position += $offset;
  158.                      return true;
  159.                 } else {
  160.                      return false;
  161.                 }
  162.                 break;
  163.                
  164.             case SEEK_END:
  165.                 if (strlen($this->_data) + $offset >= 0) {
  166.                      $this->_position = strlen($this->_data) + $offset;
  167.                      return true;
  168.                 } else {
  169.                      return false;
  170.                 }
  171.                 break;
  172.                
  173.             default:
  174.                 return false;
  175.         }
  176.     }
  177. }

Documentation generated on Mon, 25 Aug 2008 13:54:49 +0200 by phpDocumentor 1.4.1