Overview

Packages

  • Compress

Classes

  • Horde_Compress
  • Horde_Compress_Base
  • Horde_Compress_Dbx
  • Horde_Compress_Exception
  • Horde_Compress_Gzip
  • Horde_Compress_Rar
  • Horde_Compress_Tar
  • Horde_Compress_Tnef
  • Horde_Compress_Translation
  • Horde_Compress_Zip
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * The Horde_Compress_Gzip class allows gzip files to be read.
 4:  *
 5:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
 6:  *
 7:  * See the enclosed file COPYING for license information (LGPL). If you
 8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 9:  *
10:  * @author   Michael Cochrane <mike@graftonhall.co.nz>
11:  * @author   Michael Slusarz <slusarz@horde.org>
12:  * @category Horde
13:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
14:  * @package  Compress
15:  */
16: class Horde_Compress_Gzip extends Horde_Compress_Base
17: {
18:     /**
19:      */
20:     public $canDecompress = true;
21: 
22:     /**
23:      * Gzip file flags.
24:      *
25:      * @var array
26:      */
27:     protected $_flags = array(
28:         'FTEXT'     =>  0x01,
29:         'FHCRC'     =>  0x02,
30:         'FEXTRA'    =>  0x04,
31:         'FNAME'     =>  0x08,
32:         'FCOMMENT'  =>  0x10
33:     );
34: 
35:     /**
36:      * @return string  The uncompressed data.
37:      */
38:     public function decompress($data, array $params = array())
39:     {
40:         /* If gzip is not compiled into PHP, return now. */
41:         if (!Horde_Util::extensionExists('zlib')) {
42:             throw new Horde_Compress_Exception(Horde_Compress_Translation::t("This server can't uncompress gzip files."));
43:         }
44: 
45:         /* Gzipped File - decompress it first. */
46:         $position = 0;
47:         $info = @unpack('CCM/CFLG/VTime/CXFL/COS', substr($data, $position + 2));
48:         if (!$info) {
49:             throw new Horde_Compress_Exception(Horde_Compress_Translation::t("Unable to decompress data."));
50:         }
51:         $position += 10;
52: 
53:         if ($info['FLG'] & $this->_flags['FEXTRA']) {
54:             $XLEN = unpack('vLength', substr($data, $position + 0, 2));
55:             $XLEN = $XLEN['Length'];
56:             $position += $XLEN + 2;
57:         }
58: 
59:         if ($info['FLG'] & $this->_flags['FNAME']) {
60:             $filenamePos = strpos($data, "\x0", $position);
61:             $filename = substr($data, $position, $filenamePos - $position);
62:             $position = $filenamePos + 1;
63:         }
64: 
65:         if ($info['FLG'] & $this->_flags['FCOMMENT']) {
66:             $commentPos = strpos($data, "\x0", $position);
67:             $comment = substr($data, $position, $commentPos - $position);
68:             $position = $commentPos + 1;
69:         }
70: 
71:         if ($info['FLG'] & $this->_flags['FHCRC']) {
72:             $hcrc = unpack('vCRC', substr($data, $position + 0, 2));
73:             $hcrc = $hcrc['CRC'];
74:             $position += 2;
75:         }
76: 
77:         $result = @gzinflate(substr($data, $position, strlen($data) - $position));
78:         if (empty($result)) {
79:             throw new Horde_Compress_Exception(Horde_Compress_Translation::t("Unable to decompress data."));
80:         }
81: 
82:         return $result;
83:     }
84: 
85: }
86: 
API documentation generated by ApiGen