PhpZipProxy.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. *
  11. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  12. * @license https://www.gnu.org/copyleft/gpl.html GPL License
  13. * @version 1.3
  14. */
  15. class PhpZipProxy implements ZipInterface
  16. {
  17. protected $zipArchive;
  18. protected $filename;
  19. /**
  20. * Class constructor
  21. *
  22. * @throws PhpZipProxyException
  23. */
  24. public function __construct()
  25. {
  26. if (! class_exists('ZipArchive')) {
  27. throw new PhpZipProxyException('Zip extension not loaded - check your php settings, PHP5.2 minimum with zip extension
  28. is required for using PhpZipProxy'); ;
  29. }
  30. $this->zipArchive = new ZipArchive();
  31. }
  32. /**
  33. * Open a Zip archive
  34. *
  35. * @param string $filename the name of the archive to open
  36. * @return true if openning has succeeded
  37. */
  38. public function open($filename)
  39. {
  40. $this->filename = $filename;
  41. return $this->zipArchive->open($filename, ZIPARCHIVE::CREATE);
  42. }
  43. /**
  44. * Retrieve the content of a file within the archive from its name
  45. *
  46. * @param string $name the name of the file to extract
  47. * @return the content of the file in a string
  48. */
  49. public function getFromName($name)
  50. {
  51. return $this->zipArchive->getFromName($name);
  52. }
  53. /**
  54. * Add a file within the archive from a string
  55. *
  56. * @param string $localname the local path to the file in the archive
  57. * @param string $contents the content of the file
  58. * @return true if the file has been successful added
  59. */
  60. public function addFromString($localname, $contents)
  61. {
  62. if (file_exists($this->filename) && !is_writable($this->filename)) {
  63. return false;
  64. }
  65. return $this->zipArchive->addFromString($localname, $contents);
  66. }
  67. /**
  68. * Add a file within the archive from a file
  69. *
  70. * @param string $filename the path to the file we want to add
  71. * @param string $localname the local path to the file in the archive
  72. * @return true if the file has been successful added
  73. */
  74. public function addFile($filename, $localname = null)
  75. {
  76. if ((file_exists($this->filename) && !is_writable($this->filename))
  77. || !file_exists($filename)) {
  78. return false;
  79. }
  80. return $this->zipArchive->addFile($filename, $localname);
  81. }
  82. /**
  83. * Close the Zip archive
  84. * @return true
  85. */
  86. public function close()
  87. {
  88. return $this->zipArchive->close();
  89. }
  90. }
  91. ?>