PclZipProxy.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. if (! defined('ODTPHP_PATHTOPCLZIP')) define('ODTPHP_PATHTOPCLZIP','pclzip/');
  3. require_once ODTPHP_PATHTOPCLZIP.'pclzip.lib.php';
  4. require_once 'ZipInterface.php';
  5. class PclZipProxyException extends Exception
  6. { }
  7. /**
  8. * Proxy class for the PclZip library
  9. * You need PHP 5.2 at least
  10. * You need Zip Extension or PclZip library
  11. * Encoding : ISO-8859-1
  12. *
  13. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  14. * @copyright GPL License 2010 - Laurent Destailleur - eldy@users.sourceforge.net
  15. * @license https://www.gnu.org/copyleft/gpl.html GPL License
  16. * @version 1.4
  17. */
  18. class PclZipProxy implements ZipInterface
  19. {
  20. protected $tmpdir = '/tmp';
  21. protected $openned = false;
  22. protected $filename;
  23. protected $pclzip;
  24. /**
  25. * Class constructor
  26. *
  27. * @throws PclZipProxyException
  28. */
  29. public function __construct($forcedir='')
  30. {
  31. if (! class_exists('PclZip')) {
  32. throw new PclZipProxyException('PclZip class not loaded - PclZip library
  33. is required for using PclZipProxy'); ;
  34. }
  35. if ($forcedir) $this->tmpdir=preg_replace('|[//\/]$|','',$forcedir); // $this->tmpdir must not contains / at the end
  36. }
  37. /**
  38. * Open a Zip archive
  39. *
  40. * @param string $filename the name of the archive to open
  41. * @return true if openning has succeeded
  42. */
  43. public function open($filename)
  44. {
  45. if (true === $this->openned) {
  46. $this->close();
  47. }
  48. $this->filename = $filename;
  49. $this->pclzip = new PclZip($this->filename);
  50. $this->openned = true;
  51. return true;
  52. }
  53. /**
  54. * Retrieve the content of a file within the archive from its name
  55. *
  56. * @param string $name the name of the file to extract
  57. * @return the content of the file in a string
  58. */
  59. public function getFromName($name)
  60. {
  61. if (false === $this->openned) {
  62. return false;
  63. }
  64. $name = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $name);
  65. $extraction = $this->pclzip->extract(PCLZIP_OPT_BY_NAME, $name,
  66. PCLZIP_OPT_EXTRACT_AS_STRING);
  67. if (!empty($extraction)) {
  68. return $extraction[0]['content'];
  69. }
  70. return false;
  71. }
  72. /**
  73. * Add a file within the archive from a string
  74. *
  75. * @param string $localname the local path to the file in the archive
  76. * @param string $contents the content of the file
  77. * @return true if the file has been successful added
  78. */
  79. public function addFromString($localname, $contents)
  80. {
  81. if (false === $this->openned) {
  82. return false;
  83. }
  84. if (file_exists($this->filename) && !is_writable($this->filename)) {
  85. return false;
  86. }
  87. $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
  88. $localpath = dirname($localname);
  89. $tmpfilename = $this->tmpdir . '/' . basename($localname);
  90. if (false !== file_put_contents($tmpfilename, $contents)) {
  91. //print "tmpfilename=".$tmpfilename;
  92. //print "localname=".$localname;
  93. $res=$this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
  94. $add = $this->pclzip->add($tmpfilename,
  95. PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
  96. PCLZIP_OPT_ADD_PATH, $localpath);
  97. unlink($tmpfilename);
  98. if (!empty($add)) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * Add a file within the archive from a file
  106. *
  107. * @param string $filename the path to the file we want to add
  108. * @param string $localname the local path to the file in the archive
  109. * @return true if the file has been successful added
  110. */
  111. public function addFile($filename, $localname = null)
  112. {
  113. if (false === $this->openned) {
  114. return false;
  115. }
  116. if ((file_exists($this->filename) && !is_writable($this->filename))
  117. || !file_exists($filename)) {
  118. return false;
  119. }
  120. if (isSet($localname)) {
  121. $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
  122. $localpath = dirname($localname);
  123. $tmpfilename = $this->tmpdir . '/' . basename($localname);
  124. } else {
  125. $localname = basename($filename);
  126. $tmpfilename = $this->tmpdir . '/' . $localname;
  127. $localpath = '';
  128. }
  129. if (file_exists($filename)) {
  130. copy($filename, $tmpfilename);
  131. $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
  132. $this->pclzip->add($tmpfilename,
  133. PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
  134. PCLZIP_OPT_ADD_PATH, $localpath);
  135. unlink($tmpfilename);
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Close the Zip archive
  142. * @return true
  143. */
  144. public function close()
  145. {
  146. if (false === $this->openned) {
  147. return false;
  148. }
  149. $this->pclzip = $this->filename = null;
  150. $this->openned = false;
  151. return true;
  152. }
  153. }
  154. ?>