[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/ -> ZipArchive.php (source)

   1  <?php
   2  
   3  if (!defined('PCLZIP_TEMPORARY_DIR')) {
   4      define('PCLZIP_TEMPORARY_DIR', PHPExcel_Shared_File::sys_get_temp_dir() . DIRECTORY_SEPARATOR);
   5  }
   6  require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PCLZip/pclzip.lib.php';
   7  
   8  /**
   9   * PHPExcel_Shared_ZipArchive
  10   *
  11   * Copyright (c) 2006 - 2015 PHPExcel
  12   *
  13   * This library is free software; you can redistribute it and/or
  14   * modify it under the terms of the GNU Lesser General Public
  15   * License as published by the Free Software Foundation; either
  16   * version 2.1 of the License, or (at your option) any later version.
  17   *
  18   * This library is distributed in the hope that it will be useful,
  19   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21   * Lesser General Public License for more details.
  22   *
  23   * You should have received a copy of the GNU Lesser General Public
  24   * License along with this library; if not, write to the Free Software
  25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  26   *
  27   * @category   PHPExcel
  28   * @package    PHPExcel_Shared_ZipArchive
  29   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  30   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  31   * @version    ##VERSION##, ##DATE##
  32   */
  33  class PHPExcel_Shared_ZipArchive
  34  {
  35  
  36      /**    constants */
  37      const OVERWRITE = 'OVERWRITE';
  38      const CREATE    = 'CREATE';
  39  
  40  
  41      /**
  42       * Temporary storage directory
  43       *
  44       * @var string
  45       */
  46      private $tempDir;
  47  
  48      /**
  49       * Zip Archive Stream Handle
  50       *
  51       * @var string
  52       */
  53      private $zip;
  54  
  55  
  56      /**
  57       * Open a new zip archive
  58       *
  59       * @param    string    $fileName    Filename for the zip archive
  60       * @return    boolean
  61       */
  62      public function open($fileName)
  63      {
  64          $this->tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  65          $this->zip = new PclZip($fileName);
  66  
  67          return true;
  68      }
  69  
  70  
  71      /**
  72       * Close this zip archive
  73       *
  74       */
  75      public function close()
  76      {
  77      }
  78  
  79  
  80      /**
  81       * Add a new file to the zip archive from a string of raw data.
  82       *
  83       * @param    string    $localname        Directory/Name of the file to add to the zip archive
  84       * @param    string    $contents        String of data to add to the zip archive
  85       */
  86      public function addFromString($localname, $contents)
  87      {
  88          $filenameParts = pathinfo($localname);
  89  
  90          $handle = fopen($this->tempDir.'/'.$filenameParts["basename"], "wb");
  91          fwrite($handle, $contents);
  92          fclose($handle);
  93  
  94          $res = $this->zip->add($this->tempDir.'/'.$filenameParts["basename"], PCLZIP_OPT_REMOVE_PATH, $this->tempDir, PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"]);
  95          if ($res == 0) {
  96              throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->zip->errorInfo(true));
  97          }
  98  
  99          unlink($this->tempDir.'/'.$filenameParts["basename"]);
 100      }
 101  
 102      /**
 103       * Find if given fileName exist in archive (Emulate ZipArchive locateName())
 104       *
 105       * @param        string        $fileName        Filename for the file in zip archive
 106       * @return        boolean
 107       */
 108      public function locateName($fileName)
 109      {
 110          $list = $this->zip->listContent();
 111          $listCount = count($list);
 112          $list_index = -1;
 113          for ($i = 0; $i < $listCount; ++$i) {
 114              if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
 115                  strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
 116                  $list_index = $i;
 117                  break;
 118              }
 119          }
 120          return ($list_index > -1);
 121      }
 122  
 123      /**
 124       * Extract file from archive by given fileName (Emulate ZipArchive getFromName())
 125       *
 126       * @param        string        $fileName        Filename for the file in zip archive
 127       * @return        string  $contents        File string contents
 128       */
 129      public function getFromName($fileName)
 130      {
 131          $list = $this->zip->listContent();
 132          $listCount = count($list);
 133          $list_index = -1;
 134          for ($i = 0; $i < $listCount; ++$i) {
 135              if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
 136                  strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
 137                  $list_index = $i;
 138                  break;
 139              }
 140          }
 141  
 142          $extracted = "";
 143          if ($list_index != -1) {
 144              $extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
 145          } else {
 146              $filename = substr($fileName, 1);
 147              $list_index = -1;
 148              for ($i = 0; $i < $listCount; ++$i) {
 149                  if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
 150                      strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
 151                      $list_index = $i;
 152                      break;
 153                  }
 154              }
 155              $extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
 156          }
 157          if ((is_array($extracted)) && ($extracted != 0)) {
 158              $contents = $extracted[0]["content"];
 159          }
 160  
 161          return $contents;
 162      }
 163  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1