1 #ifndef __ZIPTOOL_H__
2 #define __ZIPTOOL_H__
3 /**
4 * This is intended to be a standalone, reduced capability
5 * implementation of Gzip and Zip functionality. Its
6 * targeted use case is for archiving and retrieving single files
7 * which use these encoding types. Being memory based and
8 * non-optimized, it is not useful in cases where very large
9 * archives are needed or where high performance is desired.
10 * However, it should hopefully work well for smaller,
11 * one-at-a-time tasks. What you get in return is the ability
12 * to drop these files into your project and remove the dependencies
13 * on ZLib and Info-Zip. Enjoy.
14 *
15 * Authors:
16 * Bob Jamison
17 *
18 * Copyright (C) 2006 Bob Jamison
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 */
36 #include <vector>
37 #include <string>
40 //########################################################################
41 //# A D L E R 3 2
42 //########################################################################
44 class Adler32
45 {
46 public:
48 Adler32();
50 virtual ~Adler32();
52 void reset();
54 void update(unsigned char b);
56 void update(char *str);
58 unsigned long getValue();
60 private:
62 unsigned long value;
64 };
67 //########################################################################
68 //# C R C 3 2
69 //########################################################################
71 class Crc32
72 {
73 public:
75 Crc32();
77 virtual ~Crc32();
79 void reset();
81 void update(unsigned char b);
83 void update(char *str);
85 void update(const std::vector<unsigned char> &buf);
87 unsigned long getValue();
89 private:
91 unsigned long value;
93 };
100 //########################################################################
101 //# G Z I P S T R E A M S
102 //########################################################################
104 class GzipFile
105 {
106 public:
108 /**
109 *
110 */
111 GzipFile();
113 /**
114 *
115 */
116 virtual ~GzipFile();
118 /**
119 *
120 */
121 virtual void put(unsigned char ch);
123 /**
124 *
125 */
126 virtual void setData(const std::vector<unsigned char> &str);
128 /**
129 *
130 */
131 virtual void clearData();
133 /**
134 *
135 */
136 virtual std::vector<unsigned char> &getData();
138 /**
139 *
140 */
141 virtual std::string &getFileName();
143 /**
144 *
145 */
146 virtual void setFileName(const std::string &val);
149 //######################
150 //# U T I L I T Y
151 //######################
153 /**
154 *
155 */
156 virtual bool readFile(const std::string &fName);
158 //######################
159 //# W R I T E
160 //######################
162 /**
163 *
164 */
165 virtual bool write();
167 /**
168 *
169 */
170 virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
172 /**
173 *
174 */
175 virtual bool writeFile(const std::string &fileName);
178 //######################
179 //# R E A D
180 //######################
183 /**
184 *
185 */
186 virtual bool read();
188 /**
189 *
190 */
191 virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
193 /**
194 *
195 */
196 virtual bool loadFile(const std::string &fileName);
200 private:
202 std::vector<unsigned char> data;
203 std::string fileName;
205 //debug messages
206 void error(char *fmt, ...);
207 void trace(char *fmt, ...);
209 unsigned long crc;
211 std::vector<unsigned char> fileBuf;
212 unsigned long fileBufPos;
214 bool getByte(unsigned char *ch);
215 bool getLong(unsigned long *val);
217 bool putByte(unsigned char ch);
218 bool putLong(unsigned long val);
220 int compressionMethod;
221 };
226 //########################################################################
227 //# Z I P F I L E
228 //########################################################################
231 /**
232 *
233 */
234 class ZipEntry
235 {
236 public:
238 /**
239 *
240 */
241 ZipEntry();
243 /**
244 *
245 */
246 ZipEntry(const std::string &fileName,
247 const std::string &comment);
249 /**
250 *
251 */
252 virtual ~ZipEntry();
254 /**
255 *
256 */
257 virtual std::string getFileName();
259 /**
260 *
261 */
262 virtual void setFileName(const std::string &val);
264 /**
265 *
266 */
267 virtual std::string getComment();
269 /**
270 *
271 */
272 virtual void setComment(const std::string &val);
274 /**
275 *
276 */
277 virtual unsigned long getCompressedSize();
279 /**
280 *
281 */
282 virtual int getCompressionMethod();
284 /**
285 *
286 */
287 virtual void setCompressionMethod(int val);
289 /**
290 *
291 */
292 virtual std::vector<unsigned char> &getCompressedData();
294 /**
295 *
296 */
297 virtual void setCompressedData(const std::vector<unsigned char> &val);
299 /**
300 *
301 */
302 virtual unsigned long getUncompressedSize();
304 /**
305 *
306 */
307 virtual std::vector<unsigned char> &getUncompressedData();
309 /**
310 *
311 */
312 virtual void setUncompressedData(const std::vector<unsigned char> &val);
314 /**
315 *
316 */
317 virtual void write(unsigned char ch);
319 /**
320 *
321 */
322 virtual void finish();
324 /**
325 *
326 */
327 virtual unsigned long getCrc();
329 /**
330 *
331 */
332 virtual void setCrc(unsigned long crc);
334 /**
335 *
336 */
337 virtual bool readFile(const std::string &fileNameArg,
338 const std::string &commentArg);
340 /**
341 *
342 */
343 virtual void setPosition(unsigned long val);
345 /**
346 *
347 */
348 virtual unsigned long getPosition();
350 private:
352 unsigned long crc;
354 std::string fileName;
355 std::string comment;
357 int compressionMethod;
359 std::vector<unsigned char> compressedData;
360 std::vector<unsigned char> uncompressedData;
362 unsigned long position;
363 };
373 /**
374 * This class sits over the zlib and gzip code to
375 * implement a PKWare or Info-Zip .zip file reader and
376 * writer
377 */
378 class ZipFile
379 {
380 public:
382 /**
383 *
384 */
385 ZipFile();
387 /**
388 *
389 */
390 virtual ~ZipFile();
392 //######################
393 //# V A R I A B L E S
394 //######################
396 /**
397 *
398 */
399 virtual void setComment(const std::string &val);
401 /**
402 *
403 */
404 virtual std::string getComment();
406 /**
407 * Return the list of entries currently in this file
408 */
409 std::vector<ZipEntry *> &getEntries();
412 //######################
413 //# U T I L I T Y
414 //######################
416 /**
417 *
418 */
419 virtual ZipEntry *addFile(const std::string &fileNameArg,
420 const std::string &commentArg);
422 /**
423 *
424 */
425 virtual ZipEntry *newEntry(const std::string &fileNameArg,
426 const std::string &commentArg);
428 //######################
429 //# W R I T E
430 //######################
432 /**
433 *
434 */
435 virtual bool write();
437 /**
438 *
439 */
440 virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
442 /**
443 *
444 */
445 virtual bool writeFile(const std::string &fileName);
448 //######################
449 //# R E A D
450 //######################
453 /**
454 *
455 */
456 virtual bool read();
458 /**
459 *
460 */
461 virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
463 /**
464 *
465 */
466 virtual bool readFile(const std::string &fileName);
469 private:
471 //debug messages
472 void error(char *fmt, ...);
473 void trace(char *fmt, ...);
475 //# Private writing methods
477 /**
478 *
479 */
480 bool putLong(unsigned long val);
482 /**
483 *
484 */
485 bool putInt(unsigned int val);
488 /**
489 *
490 */
491 bool putByte(unsigned char val);
493 /**
494 *
495 */
496 bool writeFileData();
498 /**
499 *
500 */
501 bool writeCentralDirectory();
504 //# Private reading methods
506 /**
507 *
508 */
509 bool getLong(unsigned long *val);
511 /**
512 *
513 */
514 bool getInt(unsigned int *val);
516 /**
517 *
518 */
519 bool getByte(unsigned char *val);
521 /**
522 *
523 */
524 bool readFileData();
526 /**
527 *
528 */
529 bool readCentralDirectory();
532 std::vector<ZipEntry *> entries;
534 std::vector<unsigned char> fileBuf;
535 unsigned long fileBufPos;
537 std::string comment;
538 };
545 #endif /* __ZIPTOOL_H__ */
548 //########################################################################
549 //# E N D O F F I L E
550 //########################################################################