ZipArchive.php 4.8 KB

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