admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Zip Compression Class |
| 20 | * |
| 21 | * This class is based on a library aquired at Zend: |
| 22 | * http://www.zend.com/codex.php?id=696&single=1 |
| 23 | * |
admin | c8b7bda | 2006-09-30 19:26:08 +0000 | [diff] [blame] | 24 | * I'm not sure this library is all that reliable, and the |
| 25 | * directory feature doesn't seem to work right, but it's the only |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 26 | * zip compressor I'm aware of -- Rick Ellis |
| 27 | * |
| 28 | * @package CodeIgniter |
| 29 | * @subpackage Libraries |
| 30 | * @category Encryption |
| 31 | * @author Rick Ellis |
| 32 | * @link http://www.codeigniter.com/user_guide/general/encryption.html |
| 33 | */ |
| 34 | class Zip { |
| 35 | |
| 36 | var $zdata = array(); |
| 37 | var $cdir = array(); |
| 38 | var $offset = 0; |
| 39 | |
| 40 | /** |
| 41 | * Add a Directory |
| 42 | * |
| 43 | * @access public |
| 44 | * @param string |
| 45 | * @return void |
| 46 | */ |
| 47 | function add_dir($name) |
| 48 | { |
| 49 | $name =str_replace ("\\", "/", $name); |
| 50 | |
| 51 | $fd = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 52 | .pack("V", 0) |
| 53 | .pack("V", 0) |
| 54 | .pack("V", 0) |
| 55 | .pack("v", strlen($name)) |
| 56 | .pack("v", 0) |
| 57 | .$name; |
| 58 | |
| 59 | $this->cdata[] = $fd; |
| 60 | |
| 61 | $cd = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 62 | .pack("V", 0) |
| 63 | .pack("V", 0) |
| 64 | .pack("V", 0) |
| 65 | .pack("v", strlen ($name)) |
| 66 | .pack("v", 0) |
| 67 | .pack("v", 0) |
| 68 | .pack("v", 0) |
| 69 | .pack("v", 0) |
| 70 | .pack("V", 16) |
| 71 | .pack("V", $this->offset) |
| 72 | .$name; |
| 73 | |
| 74 | $this->offset = strlen(implode('', $this->cdata)); |
| 75 | |
| 76 | $this->cdir[] = $cd; |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Add a File |
| 83 | * |
| 84 | * @access public |
| 85 | * @param string |
| 86 | * @return void |
| 87 | */ |
| 88 | function add_file($data, $name) |
| 89 | { |
| 90 | $name = str_replace("\\", "/", $name); |
| 91 | |
| 92 | $u_len = strlen($data); |
| 93 | $crc = crc32($data); |
| 94 | $data = gzcompress($data); |
| 95 | $data = substr(substr($data, 0,strlen ($data) - 4), 2); |
| 96 | $c_len = strlen($data); |
| 97 | |
| 98 | $fd = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 99 | .pack("V", $crc) |
| 100 | .pack("V", $c_len) |
| 101 | .pack("V", $u_len) |
| 102 | .pack("v", strlen($name)) |
| 103 | .pack("v", 0) |
| 104 | .$name |
| 105 | .$data |
| 106 | .pack("V", $crc) |
| 107 | .pack("V", $c_len) |
| 108 | .pack("V", $u_len); |
| 109 | |
| 110 | $this->zdata[] = $fd; |
| 111 | |
| 112 | $cd = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 113 | .pack("V", $crc) |
| 114 | .pack("V", $c_len) |
| 115 | .pack("V", $u_len) |
| 116 | .pack("v", strlen ($name)) |
| 117 | .pack("v", 0) |
| 118 | .pack("v", 0) |
| 119 | .pack("v", 0) |
| 120 | .pack("v", 0) |
| 121 | .pack("V", 32 ) |
| 122 | .pack("V", $this->offset) |
| 123 | .$name; |
| 124 | |
| 125 | $this->offset = strlen(implode('', $this->zdata)); |
| 126 | |
| 127 | $this->cdir[] = $cd; |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Output the zip file |
| 134 | * |
| 135 | * @access public |
| 136 | * @return string |
| 137 | */ |
| 138 | function output_zipfile() |
| 139 | { |
| 140 | $data = implode("", $this->zdata); |
| 141 | $cdir = implode("", $this->cdir); |
| 142 | |
| 143 | return $data |
| 144 | .$cdir |
| 145 | ."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 146 | .pack("v", sizeof($this->cdir)) |
| 147 | .pack("v", sizeof($this->cdir)) |
| 148 | .pack("V", strlen($cdir)) |
| 149 | .pack("V", strlen($data)) |
| 150 | ."\x00\x00"; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | // END CLASS |
| 155 | ?> |