50e7122f6239904e60b3ee2ce3a4e6329a209c80.svn-base 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_ZipArchive
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  28. define('PCLZIP_TEMPORARY_DIR', PHPExcel_Shared_File::sys_get_temp_dir() . DIRECTORY_SEPARATOR);
  29. }
  30. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PCLZip/pclzip.lib.php';
  31. /**
  32. * PHPExcel_Shared_ZipArchive
  33. *
  34. * @category PHPExcel
  35. * @package PHPExcel_Shared_ZipArchive
  36. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  37. */
  38. class PHPExcel_Shared_ZipArchive
  39. {
  40. /** constants */
  41. const OVERWRITE = 'OVERWRITE';
  42. const CREATE = 'CREATE';
  43. /**
  44. * Temporary storage directory
  45. *
  46. * @var string
  47. */
  48. private $_tempDir;
  49. /**
  50. * Zip Archive Stream Handle
  51. *
  52. * @var string
  53. */
  54. private $_zip;
  55. /**
  56. * Open a new zip archive
  57. *
  58. * @param string $fileName Filename for the zip archive
  59. * @return boolean
  60. */
  61. public function open($fileName)
  62. {
  63. $this->_tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  64. $this->_zip = new PclZip($fileName);
  65. return true;
  66. }
  67. /**
  68. * Close this zip archive
  69. *
  70. */
  71. public function close()
  72. {
  73. }
  74. /**
  75. * Add a new file to the zip archive from a string of raw data.
  76. *
  77. * @param string $localname Directory/Name of the file to add to the zip archive
  78. * @param string $contents String of data to add to the zip archive
  79. */
  80. public function addFromString($localname, $contents)
  81. {
  82. $filenameParts = pathinfo($localname);
  83. $handle = fopen($this->_tempDir.'/'.$filenameParts["basename"], "wb");
  84. fwrite($handle, $contents);
  85. fclose($handle);
  86. $res = $this->_zip->add($this->_tempDir.'/'.$filenameParts["basename"],
  87. PCLZIP_OPT_REMOVE_PATH, $this->_tempDir,
  88. PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"]
  89. );
  90. if ($res == 0) {
  91. throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->_zip->errorInfo(true));
  92. }
  93. unlink($this->_tempDir.'/'.$filenameParts["basename"]);
  94. }
  95. /**
  96. * Find if given fileName exist in archive (Emulate ZipArchive locateName())
  97. *
  98. * @param string $fileName Filename for the file in zip archive
  99. * @return boolean
  100. */
  101. public function locateName($fileName)
  102. {
  103. $list = $this->_zip->listContent();
  104. $listCount = count($list);
  105. $list_index = -1;
  106. for ($i = 0; $i < $listCount; ++$i) {
  107. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  108. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  109. $list_index = $i;
  110. break;
  111. }
  112. }
  113. return ($list_index > -1);
  114. }
  115. /**
  116. * Extract file from archive by given fileName (Emulate ZipArchive getFromName())
  117. *
  118. * @param string $fileName Filename for the file in zip archive
  119. * @return string $contents File string contents
  120. */
  121. public function getFromName($fileName)
  122. {
  123. $list = $this->_zip->listContent();
  124. $listCount = count($list);
  125. $list_index = -1;
  126. for ($i = 0; $i < $listCount; ++$i) {
  127. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  128. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  129. $list_index = $i;
  130. break;
  131. }
  132. }
  133. $extracted = "";
  134. if ($list_index != -1) {
  135. $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  136. } else {
  137. $filename = substr($fileName, 1);
  138. $list_index = -1;
  139. for ($i = 0; $i < $listCount; ++$i) {
  140. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  141. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  142. $list_index = $i;
  143. break;
  144. }
  145. }
  146. $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  147. }
  148. if ((is_array($extracted)) && ($extracted != 0)) {
  149. $contents = $extracted[0]["content"];
  150. }
  151. return $contents;
  152. }
  153. }