/Zend/Filter/Compress/Gz.php

https://github.com/MontmereLimited/ZendFramework-v1 · PHP · 228 lines · 104 code · 22 blank · 102 comment · 22 complexity · 97568c883ff72ab5a6091b2de16535fc MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Gz.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Filter_Compress_CompressAbstract
  23. */
  24. // // // // // // // // // // require_once 'Zend/Filter/Compress/CompressAbstract.php';
  25. /**
  26. * Compression adapter for Gzip (ZLib)
  27. *
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Filter_Compress_Gz extends Zend_Filter_Compress_CompressAbstract
  34. {
  35. /**
  36. * Compression Options
  37. * array(
  38. * 'level' => Compression level 0-9
  39. * 'mode' => Compression mode, can be 'compress', 'deflate'
  40. * 'archive' => Archive to use
  41. * )
  42. *
  43. * @var array
  44. */
  45. protected $_options = array(
  46. 'level' => 9,
  47. 'mode' => 'compress',
  48. 'archive' => null,
  49. );
  50. /**
  51. * Class constructor
  52. *
  53. * @param array|Zend_Config|null $options (Optional) Options to set
  54. */
  55. public function __construct($options = null)
  56. {
  57. if (!extension_loaded('zlib')) {
  58. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  59. throw new Zend_Filter_Exception('This filter needs the zlib extension');
  60. }
  61. parent::__construct($options);
  62. }
  63. /**
  64. * Returns the set compression level
  65. *
  66. * @return integer
  67. */
  68. public function getLevel()
  69. {
  70. return $this->_options['level'];
  71. }
  72. /**
  73. * Sets a new compression level
  74. *
  75. * @param integer $level
  76. * @return Zend_Filter_Compress_Gz
  77. */
  78. public function setLevel($level)
  79. {
  80. if (($level < 0) || ($level > 9)) {
  81. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  82. throw new Zend_Filter_Exception('Level must be between 0 and 9');
  83. }
  84. $this->_options['level'] = (int) $level;
  85. return $this;
  86. }
  87. /**
  88. * Returns the set compression mode
  89. *
  90. * @return string
  91. */
  92. public function getMode()
  93. {
  94. return $this->_options['mode'];
  95. }
  96. /**
  97. * Sets a new compression mode
  98. *
  99. * @param string $mode Supported are 'compress', 'deflate' and 'file'
  100. */
  101. public function setMode($mode)
  102. {
  103. if (($mode != 'compress') && ($mode != 'deflate')) {
  104. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  105. throw new Zend_Filter_Exception('Given compression mode not supported');
  106. }
  107. $this->_options['mode'] = $mode;
  108. return $this;
  109. }
  110. /**
  111. * Returns the set archive
  112. *
  113. * @return string
  114. */
  115. public function getArchive()
  116. {
  117. return $this->_options['archive'];
  118. }
  119. /**
  120. * Sets the archive to use for de-/compression
  121. *
  122. * @param string $archive Archive to use
  123. * @return Zend_Filter_Compress_Gz
  124. */
  125. public function setArchive($archive)
  126. {
  127. $this->_options['archive'] = (string) $archive;
  128. return $this;
  129. }
  130. /**
  131. * Compresses the given content
  132. *
  133. * @param string $content
  134. * @return string
  135. */
  136. public function compress($content)
  137. {
  138. $archive = $this->getArchive();
  139. if (!empty($archive)) {
  140. $file = gzopen($archive, 'w' . $this->getLevel());
  141. if (!$file) {
  142. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  143. throw new Zend_Filter_Exception("Error opening the archive '" . $this->_options['archive'] . "'");
  144. }
  145. gzwrite($file, $content);
  146. gzclose($file);
  147. $compressed = true;
  148. } else if ($this->_options['mode'] == 'deflate') {
  149. $compressed = gzdeflate($content, $this->getLevel());
  150. } else {
  151. $compressed = gzcompress($content, $this->getLevel());
  152. }
  153. if (!$compressed) {
  154. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  155. throw new Zend_Filter_Exception('Error during compression');
  156. }
  157. return $compressed;
  158. }
  159. /**
  160. * Decompresses the given content
  161. *
  162. * @param string $content
  163. * @return string
  164. */
  165. public function decompress($content)
  166. {
  167. $archive = $this->getArchive();
  168. $mode = $this->getMode();
  169. if (file_exists($content)) {
  170. $archive = $content;
  171. }
  172. if (file_exists($archive)) {
  173. $handler = fopen($archive, "rb");
  174. if (!$handler) {
  175. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  176. throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
  177. }
  178. fseek($handler, -4, SEEK_END);
  179. $packet = fread($handler, 4);
  180. $bytes = unpack("V", $packet);
  181. $size = end($bytes);
  182. fclose($handler);
  183. $file = gzopen($archive, 'r');
  184. $compressed = gzread($file, $size);
  185. gzclose($file);
  186. } else if ($mode == 'deflate') {
  187. $compressed = gzinflate($content);
  188. } else {
  189. $compressed = gzuncompress($content);
  190. }
  191. if (!$compressed) {
  192. // // // // // // // // // // require_once 'Zend/Filter/Exception.php';
  193. throw new Zend_Filter_Exception('Error during compression');
  194. }
  195. return $compressed;
  196. }
  197. /**
  198. * Returns the adapter name
  199. *
  200. * @return string
  201. */
  202. public function toString()
  203. {
  204. return 'Gz';
  205. }
  206. }