00001
00002
00003
00004
00005 #ifndef _MINI_ZIP_H_
00006 #define _MINI_ZIP_H_
00007
00008 #include <zlib.h>
00009
00010 #include <string>
00011 #include <ctime>
00012 #include <iostream>
00013 #include <vector>
00014
00015 namespace MiniZip
00016 {
00017
00018 struct _LocalFileHeader
00019 {
00020 long locSig;
00021
00022 short minVer;
00023 short flag;
00024 short compr;
00025
00026 unsigned short modTime;
00027 unsigned short modDate;
00028
00029 long crc32;
00030
00031 long szCompr;
00032 long szUncompr;
00033 short lenFilename;
00034 short lenExtra;
00035 } __attribute__((__packed__));
00036
00037 struct _CentralFileHeader
00038 {
00039 long ctrSig;
00040
00041 short verMade;
00042 short verNeeded;
00043 short flag;
00044 short compr;
00045
00046 unsigned short modTime;
00047 unsigned short modDate;
00048
00049 long crc32;
00050
00051 long szCompr;
00052 long szUncompr;
00053
00054 short lenFilename;
00055 short lenExtra;
00056 short lenComment;
00057
00058 short numDiskStart;
00059
00060 short attrInternal;
00061 long attrExternal;
00062
00063 long offLocalHeader;
00064 } __attribute__((__packed__));
00065
00066 struct _EndOfCDir
00067 {
00068 long endSig;
00069
00070 short numDisk;
00071 short numDiskStart;
00072 short numEntriesDisk;
00073 short numEntries;
00074
00075 long szCentr;
00076 long offCentr;
00077
00078 short lenComment;
00079 } __attribute__((__packed__));
00080
00081
00082 inline unsigned long crc32(const std::string& data)
00083 {
00084 unsigned long crc = ::crc32(0, NULL, 0);
00085 return ::crc32(crc, (const Bytef *)data.c_str(), data.length());
00086 };
00087
00088
00089 class ZipInfo
00090 {
00091 public:
00092 ZipInfo(const std::string& filename, const std::tm *time);
00093
00094 void setData(const std::string& data, bool compress);
00095 const std::string& getData() const {return m_data;};
00096
00097 void setOffset(long offset) {m_offset = offset;};
00098
00099 std::string localFileHeader() const;
00100 std::string centralFileHeader() const;
00101
00102 private:
00103 std::string m_filename;
00104 std::tm m_time;
00105 unsigned short m_flags;
00106 unsigned short m_compression;
00107 std::string m_extra;
00108
00109 long m_offset;
00110 long m_origSize;
00111 long m_crc;
00112 std::string m_data;
00113 };
00114
00115 class ZipFile
00116 {
00117 public:
00118 ZipFile(std::ostream& out);
00119
00120 bool addString(const std::string& filename, const std::string& data, bool compress=true);
00121
00122 bool finalize();
00123
00124 private:
00125 void writeCentralDirectory();
00126
00127 std::ostream& m_out;
00128 bool m_finalized;
00129 std::vector<ZipInfo> m_files;
00130 };
00131
00132 }
00133
00134 #endif // _MINI_ZIP_H_