PhpZipProxy.php.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. require_once 'ZipInterface.php';
  3. class PhpZipProxyException extends Exception
  4. { }
  5. /**
  6. * Proxy class for the PHP Zip Extension
  7. * You need PHP 5.2 at least
  8. * You need Zip Extension or PclZip library
  9. * Encoding : ISO-8859-1
  10. * Last commit by $Author: neveldo $
  11. * Date - $Date: 2009-05-29 10:05:11 +0200 (ven., 29 mai 2009) $
  12. * SVN Revision - $Rev: 28 $
  13. * Id : $Id: odf.php 28 2009-05-29 08:05:11Z neveldo $
  14. *
  15. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  16. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  17. * @version 1.3
  18. */
  19. class PhpZipProxy implements ZipInterface
  20. {
  21. protected $zipArchive;
  22. protected $filename;
  23. /**
  24. * Class constructor
  25. *
  26. * @throws PhpZipProxyException
  27. */
  28. public function __construct()
  29. {
  30. if (! class_exists('ZipArchive')) {
  31. throw new PhpZipProxyException('Zip extension not loaded - check your php settings, PHP5.2 minimum with zip extension
  32. is required for using PhpZipProxy'); ;
  33. }
  34. $this->zipArchive = new ZipArchive();
  35. }
  36. /**
  37. * Open a Zip archive
  38. *
  39. * @param string $filename the name of the archive to open
  40. * @return true if openning has succeeded
  41. */
  42. public function open($filename)
  43. {
  44. $this->filename = $filename;
  45. return $this->zipArchive->open($filename, ZIPARCHIVE::CREATE);
  46. }
  47. /**
  48. * Retrieve the content of a file within the archive from its name
  49. *
  50. * @param string $name the name of the file to extract
  51. * @return the content of the file in a string
  52. */
  53. public function getFromName($name)
  54. {
  55. return $this->zipArchive->getFromName($name);
  56. }
  57. /**
  58. * Add a file within the archive from a string
  59. *
  60. * @param string $localname the local path to the file in the archive
  61. * @param string $contents the content of the file
  62. * @return true if the file has been successful added
  63. */
  64. public function addFromString($localname, $contents)
  65. {
  66. if (file_exists($this->filename) && !is_writable($this->filename)) {
  67. return false;
  68. }
  69. return $this->zipArchive->addFromString($localname, $contents);
  70. }
  71. /**
  72. * Add a file within the archive from a file
  73. *
  74. * @param string $filename the path to the file we want to add
  75. * @param string $localname the local path to the file in the archive
  76. * @return true if the file has been successful added
  77. */
  78. public function addFile($filename, $localname = null)
  79. {
  80. if ((file_exists($this->filename) && !is_writable($this->filename))
  81. || !file_exists($filename)) {
  82. return false;
  83. }
  84. return $this->zipArchive->addFile($filename, $localname);
  85. }
  86. /**
  87. * Close the Zip archive
  88. * @return true
  89. */
  90. public function close()
  91. {
  92. return $this->zipArchive->close();
  93. }
  94. }
  95. ?>