ZipArchive.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  28. define('PCLZIP_TEMPORARY_DIR', PHPExcel_Shared_File::sys_get_temp_dir());
  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 - 2013 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. }