blob: 9cfe9593d63cb11435065dd4f9ac3f9740aeeb6e [file] [log] [blame]
adminae58a5d2006-09-30 19:25:07 +00001<?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 *
24 * I'm not sure this library is all that reliable, but it's the only
25 * zip compressor I'm aware of -- Rick Ellis
26 *
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category Encryption
30 * @author Rick Ellis
31 * @link http://www.codeigniter.com/user_guide/general/encryption.html
32 */
33class Zip {
34
35 var $zdata = array();
36 var $cdir = array();
37 var $offset = 0;
38
39 /**
40 * Add a Directory
41 *
42 * @access public
43 * @param string
44 * @return void
45 */
46 function add_dir($name)
47 {
48 $name =str_replace ("\\", "/", $name);
49
50 $fd = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
51 .pack("V", 0)
52 .pack("V", 0)
53 .pack("V", 0)
54 .pack("v", strlen($name))
55 .pack("v", 0)
56 .$name;
57
58 $this->cdata[] = $fd;
59
60 $cd = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
61 .pack("V", 0)
62 .pack("V", 0)
63 .pack("V", 0)
64 .pack("v", strlen ($name))
65 .pack("v", 0)
66 .pack("v", 0)
67 .pack("v", 0)
68 .pack("v", 0)
69 .pack("V", 16)
70 .pack("V", $this->offset)
71 .$name;
72
73 $this->offset = strlen(implode('', $this->cdata));
74
75 $this->cdir[] = $cd;
76 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Add a File
82 *
83 * @access public
84 * @param string
85 * @return void
86 */
87 function add_file($data, $name)
88 {
89 $name = str_replace("\\", "/", $name);
90
91 $u_len = strlen($data);
92 $crc = crc32($data);
93 $data = gzcompress($data);
94 $data = substr(substr($data, 0,strlen ($data) - 4), 2);
95 $c_len = strlen($data);
96
97 $fd = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00"
98 .pack("V", $crc)
99 .pack("V", $c_len)
100 .pack("V", $u_len)
101 .pack("v", strlen($name))
102 .pack("v", 0)
103 .$name
104 .$data
105 .pack("V", $crc)
106 .pack("V", $c_len)
107 .pack("V", $u_len);
108
109 $this->zdata[] = $fd;
110
111 $cd = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00"
112 .pack("V", $crc)
113 .pack("V", $c_len)
114 .pack("V", $u_len)
115 .pack("v", strlen ($name))
116 .pack("v", 0)
117 .pack("v", 0)
118 .pack("v", 0)
119 .pack("v", 0)
120 .pack("V", 32 )
121 .pack("V", $this->offset)
122 .$name;
123
124 $this->offset = strlen(implode('', $this->zdata));
125
126 $this->cdir[] = $cd;
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Output the zip file
133 *
134 * @access public
135 * @return string
136 */
137 function output_zipfile()
138 {
139 $data = implode("", $this->zdata);
140 $cdir = implode("", $this->cdir);
141
142 return $data
143 .$cdir
144 ."\x50\x4b\x05\x06\x00\x00\x00\x00"
145 .pack("v", sizeof($this->cdir))
146 .pack("v", sizeof($this->cdir))
147 .pack("V", strlen($cdir))
148 .pack("V", strlen($data))
149 ."\x00\x00";
150 }
151
152}
153// END CLASS
154?>