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-2007 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 #ifdef G_GNUC_PRINTF
208 G_GNUC_PRINTF(2, 3)
209 #endif
210 ;
212 void trace(char *fmt, ...)
213 #ifdef G_GNUC_PRINTF
214 G_GNUC_PRINTF(2, 3)
215 #endif
216 ;
218 unsigned long crc;
220 std::vector<unsigned char> fileBuf;
221 unsigned long fileBufPos;
223 bool getByte(unsigned char *ch);
224 bool getLong(unsigned long *val);
226 bool putByte(unsigned char ch);
227 bool putLong(unsigned long val);
229 int compressionMethod;
230 };
235 //########################################################################
236 //# Z I P F I L E
237 //########################################################################
240 /**
241 *
242 */
243 class ZipEntry
244 {
245 public:
247 /**
248 *
249 */
250 ZipEntry();
252 /**
253 *
254 */
255 ZipEntry(const std::string &fileName,
256 const std::string &comment);
258 /**
259 *
260 */
261 virtual ~ZipEntry();
263 /**
264 *
265 */
266 virtual std::string getFileName();
268 /**
269 *
270 */
271 virtual void setFileName(const std::string &val);
273 /**
274 *
275 */
276 virtual std::string getComment();
278 /**
279 *
280 */
281 virtual void setComment(const std::string &val);
283 /**
284 *
285 */
286 virtual unsigned long getCompressedSize();
288 /**
289 *
290 */
291 virtual int getCompressionMethod();
293 /**
294 *
295 */
296 virtual void setCompressionMethod(int val);
298 /**
299 *
300 */
301 virtual std::vector<unsigned char> &getCompressedData();
303 /**
304 *
305 */
306 virtual void setCompressedData(const std::vector<unsigned char> &val);
308 /**
309 *
310 */
311 virtual unsigned long getUncompressedSize();
313 /**
314 *
315 */
316 virtual std::vector<unsigned char> &getUncompressedData();
318 /**
319 *
320 */
321 virtual void setUncompressedData(const std::vector<unsigned char> &val);
323 /**
324 *
325 */
326 virtual void write(unsigned char ch);
328 /**
329 *
330 */
331 virtual void finish();
333 /**
334 *
335 */
336 virtual unsigned long getCrc();
338 /**
339 *
340 */
341 virtual void setCrc(unsigned long crc);
343 /**
344 *
345 */
346 virtual bool readFile(const std::string &fileNameArg,
347 const std::string &commentArg);
349 /**
350 *
351 */
352 virtual void setPosition(unsigned long val);
354 /**
355 *
356 */
357 virtual unsigned long getPosition();
359 private:
361 unsigned long crc;
363 std::string fileName;
364 std::string comment;
366 int compressionMethod;
368 std::vector<unsigned char> compressedData;
369 std::vector<unsigned char> uncompressedData;
371 unsigned long position;
372 };
382 /**
383 * This class sits over the zlib and gzip code to
384 * implement a PKWare or Info-Zip .zip file reader and
385 * writer
386 */
387 class ZipFile
388 {
389 public:
391 /**
392 *
393 */
394 ZipFile();
396 /**
397 *
398 */
399 virtual ~ZipFile();
401 //######################
402 //# V A R I A B L E S
403 //######################
405 /**
406 *
407 */
408 virtual void setComment(const std::string &val);
410 /**
411 *
412 */
413 virtual std::string getComment();
415 /**
416 * Return the list of entries currently in this file
417 */
418 std::vector<ZipEntry *> &getEntries();
421 //######################
422 //# U T I L I T Y
423 //######################
425 /**
426 *
427 */
428 virtual ZipEntry *addFile(const std::string &fileNameArg,
429 const std::string &commentArg);
431 /**
432 *
433 */
434 virtual ZipEntry *newEntry(const std::string &fileNameArg,
435 const std::string &commentArg);
437 //######################
438 //# W R I T E
439 //######################
441 /**
442 *
443 */
444 virtual bool write();
446 /**
447 *
448 */
449 virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
451 /**
452 *
453 */
454 virtual bool writeFile(const std::string &fileName);
457 //######################
458 //# R E A D
459 //######################
462 /**
463 *
464 */
465 virtual bool read();
467 /**
468 *
469 */
470 virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
472 /**
473 *
474 */
475 virtual bool readFile(const std::string &fileName);
478 private:
480 //debug messages
481 void error(char *fmt, ...)
482 #ifdef G_GNUC_PRINTF
483 G_GNUC_PRINTF(2, 3)
484 #endif
485 ;
486 void trace(char *fmt, ...)
487 #ifdef G_GNUC_PRINTF
488 G_GNUC_PRINTF(2, 3)
489 #endif
490 ;
492 //# Private writing methods
494 /**
495 *
496 */
497 bool putLong(unsigned long val);
499 /**
500 *
501 */
502 bool putInt(unsigned int val);
505 /**
506 *
507 */
508 bool putByte(unsigned char val);
510 /**
511 *
512 */
513 bool writeFileData();
515 /**
516 *
517 */
518 bool writeCentralDirectory();
521 //# Private reading methods
523 /**
524 *
525 */
526 bool getLong(unsigned long *val);
528 /**
529 *
530 */
531 bool getInt(unsigned int *val);
533 /**
534 *
535 */
536 bool getByte(unsigned char *val);
538 /**
539 *
540 */
541 bool readFileData();
543 /**
544 *
545 */
546 bool readCentralDirectory();
549 std::vector<ZipEntry *> entries;
551 std::vector<unsigned char> fileBuf;
552 unsigned long fileBufPos;
554 std::string comment;
555 };
562 #endif /* __ZIPTOOL_H__ */
565 //########################################################################
566 //# E N D O F F I L E
567 //########################################################################