author | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
commit | 541bb378ddece2eab135a8066a16994e94436dea (patch) (unidiff) | |
tree | ff160ea3e26f7fe07fcfd401387c5a0232ca715e /backend/php/src/setup/setup_library/class.zipfile.php | |
parent | 1bf431fd3d45cbdf4afa3e12afefe5d24f4d3bc7 (diff) | |
parent | ecad5e895831337216544e81f1a467e0c68c4a6a (diff) | |
download | clipperz-541bb378ddece2eab135a8066a16994e94436dea.zip clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.gz clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.bz2 |
Merge pull request #1 from gcsolaroli/master
First version of the restructured repository
Diffstat (limited to 'backend/php/src/setup/setup_library/class.zipfile.php') (more/less context) (ignore whitespace changes)
-rw-r--r-- | backend/php/src/setup/setup_library/class.zipfile.php | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/backend/php/src/setup/setup_library/class.zipfile.php b/backend/php/src/setup/setup_library/class.zipfile.php new file mode 100644 index 0000000..4bbe779 --- a/dev/null +++ b/backend/php/src/setup/setup_library/class.zipfile.php | |||
@@ -0,0 +1,212 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Class to dynamically create a zip file (archive) | ||
5 | * | ||
6 | * @author Rochak Chauhan. Extended by Joel Wan & Mark Slemko | ||
7 | */ | ||
8 | |||
9 | class createZip { | ||
10 | var $compressedData = array(); | ||
11 | var $centralDirectory = array(); // central directory | ||
12 | var $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record | ||
13 | var $oldOffset = 0; | ||
14 | |||
15 | /** | ||
16 | * Function to create the directory where the file(s) will be unzipped | ||
17 | * | ||
18 | * @param $directoryName string | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | function addDirectory($directoryName) { | ||
23 | $directoryName = str_replace("\\", "/", $directoryName); | ||
24 | |||
25 | $feedArrayRow = "\x50\x4b\x03\x04"; | ||
26 | $feedArrayRow .= "\x0a\x00"; | ||
27 | $feedArrayRow .= "\x00\x00"; | ||
28 | $feedArrayRow .= "\x00\x00"; | ||
29 | $feedArrayRow .= "\x00\x00\x00\x00"; | ||
30 | |||
31 | $feedArrayRow .= pack("V",0); | ||
32 | $feedArrayRow .= pack("V",0); | ||
33 | $feedArrayRow .= pack("V",0); | ||
34 | $feedArrayRow .= pack("v", strlen($directoryName) ); | ||
35 | $feedArrayRow .= pack("v", 0 ); | ||
36 | $feedArrayRow .= $directoryName; | ||
37 | |||
38 | $feedArrayRow .= pack("V",0); | ||
39 | $feedArrayRow .= pack("V",0); | ||
40 | $feedArrayRow .= pack("V",0); | ||
41 | |||
42 | $this -> compressedData[] = $feedArrayRow; | ||
43 | |||
44 | $newOffset = strlen(implode("", $this->compressedData)); | ||
45 | |||
46 | $addCentralRecord = "\x50\x4b\x01\x02"; | ||
47 | $addCentralRecord .="\x00\x00"; | ||
48 | $addCentralRecord .="\x0a\x00"; | ||
49 | $addCentralRecord .="\x00\x00"; | ||
50 | $addCentralRecord .="\x00\x00"; | ||
51 | $addCentralRecord .="\x00\x00\x00\x00"; | ||
52 | $addCentralRecord .= pack("V",0); | ||
53 | $addCentralRecord .= pack("V",0); | ||
54 | $addCentralRecord .= pack("V",0); | ||
55 | $addCentralRecord .= pack("v", strlen($directoryName) ); | ||
56 | $addCentralRecord .= pack("v", 0 ); | ||
57 | $addCentralRecord .= pack("v", 0 ); | ||
58 | $addCentralRecord .= pack("v", 0 ); | ||
59 | $addCentralRecord .= pack("v", 0 ); | ||
60 | $ext = "\x00\x00\x10\x00"; | ||
61 | $ext = "\xff\xff\xff\xff"; | ||
62 | $addCentralRecord .= pack("V", 16 ); | ||
63 | |||
64 | $addCentralRecord .= pack("V", $this -> oldOffset ); | ||
65 | $this -> oldOffset = $newOffset; | ||
66 | |||
67 | $addCentralRecord .= $directoryName; | ||
68 | |||
69 | $this -> centralDirectory[] = $addCentralRecord; | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Function to add file(s) to the specified directory in the archive | ||
74 | * | ||
75 | * @param $directoryName string | ||
76 | * | ||
77 | */ | ||
78 | |||
79 | function addFile($data, $directoryName) { | ||
80 | |||
81 | $directoryName = str_replace("\\", "/", $directoryName); | ||
82 | |||
83 | $feedArrayRow = "\x50\x4b\x03\x04"; | ||
84 | $feedArrayRow .= "\x14\x00"; | ||
85 | $feedArrayRow .= "\x00\x00"; | ||
86 | $feedArrayRow .= "\x08\x00"; | ||
87 | $feedArrayRow .= "\x00\x00\x00\x00"; | ||
88 | |||
89 | $uncompressedLength = strlen($data); | ||
90 | $compression = crc32($data); | ||
91 | $gzCompressedData = gzcompress($data); | ||
92 | $gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2); | ||
93 | $compressedLength = strlen($gzCompressedData); | ||
94 | $feedArrayRow .= pack("V",$compression); | ||
95 | $feedArrayRow .= pack("V",$compressedLength); | ||
96 | $feedArrayRow .= pack("V",$uncompressedLength); | ||
97 | $feedArrayRow .= pack("v", strlen($directoryName) ); | ||
98 | $feedArrayRow .= pack("v", 0 ); | ||
99 | $feedArrayRow .= $directoryName; | ||
100 | |||
101 | $feedArrayRow .= $gzCompressedData; | ||
102 | |||
103 | $feedArrayRow .= pack("V",$compression); | ||
104 | $feedArrayRow .= pack("V",$compressedLength); | ||
105 | $feedArrayRow .= pack("V",$uncompressedLength); | ||
106 | |||
107 | $this -> compressedData[] = $feedArrayRow; | ||
108 | |||
109 | $newOffset = strlen(implode("", $this->compressedData)); | ||
110 | |||
111 | $addCentralRecord = "\x50\x4b\x01\x02"; | ||
112 | $addCentralRecord .="\x00\x00"; | ||
113 | $addCentralRecord .="\x14\x00"; | ||
114 | $addCentralRecord .="\x00\x00"; | ||
115 | $addCentralRecord .="\x08\x00"; | ||
116 | $addCentralRecord .="\x00\x00\x00\x00"; | ||
117 | $addCentralRecord .= pack("V",$compression); | ||
118 | $addCentralRecord .= pack("V",$compressedLength); | ||
119 | $addCentralRecord .= pack("V",$uncompressedLength); | ||
120 | $addCentralRecord .= pack("v", strlen($directoryName) ); | ||
121 | $addCentralRecord .= pack("v", 0 ); | ||
122 | $addCentralRecord .= pack("v", 0 ); | ||
123 | $addCentralRecord .= pack("v", 0 ); | ||
124 | $addCentralRecord .= pack("v", 0 ); | ||
125 | $addCentralRecord .= pack("V", 32 ); | ||
126 | |||
127 | $addCentralRecord .= pack("V", $this -> oldOffset ); | ||
128 | $this -> oldOffset = $newOffset; | ||
129 | |||
130 | $addCentralRecord .= $directoryName; | ||
131 | |||
132 | $this -> centralDirectory[] = $addCentralRecord; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Fucntion to return the zip file | ||
137 | * | ||
138 | * @return zipfile (archive) | ||
139 | */ | ||
140 | |||
141 | function getZippedfile() { | ||
142 | |||
143 | $data = implode("", $this -> compressedData); | ||
144 | $controlDirectory = implode("", $this -> centralDirectory); | ||
145 | |||
146 | return | ||
147 | $data. | ||
148 | $controlDirectory. | ||
149 | $this -> endOfCentralDirectory. | ||
150 | pack("v", sizeof($this -> centralDirectory)). | ||
151 | pack("v", sizeof($this -> centralDirectory)). | ||
152 | pack("V", strlen($controlDirectory)). | ||
153 | pack("V", strlen($data)). | ||
154 | "\x00\x00"; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * | ||
159 | * Function to force the download of the archive as soon as it is created | ||
160 | * | ||
161 | * @param archiveName string - name of the created archive file | ||
162 | */ | ||
163 | |||
164 | function forceDownload($archiveName) { | ||
165 | $headerInfo = ''; | ||
166 | header("Pragma: public"); | ||
167 | header("Expires: 0"); | ||
168 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | ||
169 | header("Cache-Control: private",false); | ||
170 | header("Content-Type: application/zip"); | ||
171 | header("Content-Disposition: attachment; filename=".basename($archiveName).";" ); | ||
172 | header("Content-Transfer-Encoding: binary"); | ||
173 | echo $this->getZippedfile(); | ||
174 | |||
175 | } | ||
176 | |||
177 | /** | ||
178 | * Generates zip file from POG package. | ||
179 | * | ||
180 | * @param multi-d array $package | ||
181 | * @param array $paths | ||
182 | */ | ||
183 | function addPOGPackage($package, $paths=array()) | ||
184 | { | ||
185 | |||
186 | $i = 0; | ||
187 | foreach ($package as $key=>$value) | ||
188 | { | ||
189 | $path = ''; | ||
190 | foreach ($paths as $p) | ||
191 | { | ||
192 | $path .= (($path == '') ? $p : "/$p"); | ||
193 | } | ||
194 | if (strpos($key, ".") == false) | ||
195 | { | ||
196 | $paths[] = $key; | ||
197 | $this->addDirectory((($path == '') ? "$key/" : "$path/$key/")); | ||
198 | $this->addPOGPackage($package[$key], &$paths); | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | $this->addFile(base64_decode($value), (($path == '') ? $key : "$path/$key")); | ||
203 | } | ||
204 | if ($i == (sizeof($package)-1)) | ||
205 | { | ||
206 | array_pop($paths); | ||
207 | } | ||
208 | $i++; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | ?> \ No newline at end of file | ||