Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef ZipDataSourceH
00033 #define ZipDataSourceH
00034
00035 #include <zeusbase/System/Interfaces/IInputStream.hpp>
00036 #include <zeusbase/System/ArrayPtr.hpp>
00037 #include <zeusbase/System/StringMap.hpp>
00038 #include <zeusbase/System/SingleLinkedList.hpp>
00039 #include <zeusbase/System/Interfaces/IStringList.hpp>
00040 #include <zeusbase/System/ZObject.h>
00041
00042 BEGIN_NAMESPACE_Zeus
00043
00044 class IOutputStream;
00045
00046 #pragma pack(1)
00047
00048
00051
00052 struct TypeZipLocalFileHeader
00053 {
00054
00055
00056 Uint16 usVersion;
00057 Uint16 usFlags;
00058 Uint16 usCompressionMethod;
00059 Uint16 usLastModTime;
00060 Uint16 usLastModDate;
00061 Uint32 ulCRC32;
00062 Uint32 ulCompressedSize;
00063 Uint32 ulUncompressedSize;
00064 Uint16 usFileNameLength;
00065 Uint16 usExtraFieldLength;
00066 };
00067
00068
00071
00072 struct TypeZipCentralDirectoryFileHeader
00073 {
00074
00075
00076 Uint16 usVersionMadeOf;
00077 Uint16 usVersion;
00078 Uint16 usFlags;
00079 Uint16 usCompressionMethod;
00080 Uint16 usLastModTime;
00081 Uint16 usLastModDate;
00082 Uint32 ulCRC32;
00083 Uint32 ulCompressedSize;
00084 Uint32 ulUncompressedSize;
00085 Uint16 usFileNameLength;
00086 Uint16 usExtraFieldLength;
00087 Uint16 usFileCommentLength;
00088 Uint16 usDiskNumberStart;
00089 Uint16 usInternalFileAttr;
00090 Uint32 ulExternalFileAttr;
00091 Uint32 ulOffsetToEntry;
00092 };
00093
00094
00095
00098
00099 struct TypeFileDescriptor
00100 {
00101 Uint32 ulCRC32;
00102 Uint32 ulCompressedSize;
00103 Uint32 ulUncompressedSize;
00104 };
00105
00106 #pragma pack()
00107
00108
00111
00112 struct TypeFile
00113 {
00114 TypeZipLocalFileHeader Header;
00115 TArrayPtr<char> ptrLocalExtraData;
00116 TArrayPtr<char> ptrData;
00117 TypeFileDescriptor Descriptor;
00118 bool bHasDescriptor;
00119 TypeZipCentralDirectoryFileHeader CentralDirHeader;
00120 TArrayPtr<char> ptrCentralExtraData;
00121
00123 inline TypeFile()
00124 {
00125 bHasDescriptor = false;
00126 }
00127
00129 inline ~TypeFile()
00130 {}
00131 };
00132
00133
00136
00137 enum ECompressionMethod
00138 {
00139 etNotDefined = -1,
00140 etNoCompression = 0,
00141 etShrunk = 1,
00142 etReducedToCFactor_1 = 2,
00143 etReducedToCFactor_2 = 3,
00144 etReducedToCFactor_3 = 4,
00145 etReducedToCFactor_4 = 5,
00146 etImploded = 6,
00147 etRevTokenComp = 7,
00148 etDeflated = 8,
00149 etEnhancedDelated = 9,
00150 etPkWareCompr = 10,
00151
00152 etBZIP_2 = 12,
00153
00154 etLZMA = 14,
00155
00156
00157
00158 etIBM_TERSE = 18,
00159 etIBM_LZ77 = 19,
00160 etWavPack = 97,
00161 etPPMd_Version1 = 98,
00162 };
00163
00164
00167
00168 zeusbase_class TZippedFileEntry : public TZObject
00169 {
00170 public:
00171 TZippedFileEntry(const IString& rFileEntryName, TypeFile& rEntry);
00172 TZippedFileEntry(const IString& rFileEntryName, const IString& rFileNameToCompress);
00173
00174 Retval compressFile(const IString& rPassword, Int iZipLevel = 4);
00175
00177 inline Uint32 getChecksum() const { return m_rEntry.Header.ulCRC32; }
00179 inline Uint getCompressedSize() const { return m_rEntry.Header.ulCompressedSize; }
00181 inline ECompressionMethod getCompressionMethod() const { return (ECompressionMethod)m_rEntry.Header.usCompressionMethod;}
00182 Float getCompressionRate() const;
00184 inline TString getComment() const { return m_strComment; }
00186 inline TString getFileEntryName() const { return m_strEntryName; }
00187 Timeval getModificationTime() const;
00188 Uint64 getSize() const;
00189 Retval getUnzippedData(IByteArray& rData) const;
00190 Retval getUnzippedData(IByteArray& rData, const IString& rPassword) const;
00191
00193 inline bool isCompressed() const { return m_bCompressed; }
00194 inline bool isDirectory() const { return (m_strEntryName.endsWith(L"\\") || m_strEntryName.endsWith(L"/")); }
00195 inline bool isEncrypted() const { return (m_rEntry.Header.usFlags & 0x0001); }
00196
00198 inline void setComment(const IString& rValue) { m_strComment = rValue; }
00199
00200 protected:
00201 virtual ~TZippedFileEntry();
00202 Uint generateOutput(IOutputStream& rOutStream);
00203 Uint generateOutputForCentralDirectory(IOutputStream& rOutStream, Uint uiOffsetToLocalHeader);
00204
00205 inline TypeFile& getFileEntryStructure() { return m_rEntry; }
00206 inline void setSourceFile(const IString& rFileName) { m_strFileName = rFileName; }
00207
00208 private:
00210 TypeFile& m_rEntry;
00212 TString m_strFileName;
00214 TString m_strEntryName;
00216 TString m_strComment;
00218 bool m_bCompressed;
00219
00220
00221 friend class TZipDataSource;
00222 };
00223
00224
00225
00226
00229
00230 inline Retval TZippedFileEntry::getUnzippedData(IByteArray& rData) const
00231 {
00232 return getUnzippedData(rData, TString(L""));
00233 }
00234
00235
00236
00237
00238
00239
00240
00244
00245 zeusbase_class TZipDataSource : public TZObject
00246 {
00247 public:
00248 TZipDataSource();
00249 TZipDataSource(IInputStream& rInStream);
00250
00251 Retval addFileEntry(const IString& rEntryName, const IString& rFileName, const IString& rPassword, bool bOverwriteIfExists, Int iZipLevel);
00252 Retval deleteFileEntry(Int iIndex);
00253 Retval getFileEntry(Int iIndex, TZippedFileEntry*& rpEntry) const;
00254 Retval getFileEntryByName(const IString& rEntryName, TZippedFileEntry*& rpEntry) const;
00255 void getFileEntryNames(IStringList& rNames) const;
00256 Int getFileEntryCount() const;
00257 bool isValid() const;
00258 Retval removeFileEntry(const IString& rEntryName);
00259
00260 Retval generateOutput(IOutputStream& rOutStream);
00261
00262 protected:
00263 virtual ~TZipDataSource();
00264
00265 private:
00267 bool m_bValid;
00269 TSingleLinkedList<TZippedFileEntry*> m_lstFiles;
00271 TStringMap<Int> m_mapLookup;
00272
00273 bool readBuffer(IInputStream& rInStream, TArrayPtr<char>& rpBuffer, Int iSize);
00274 };
00275
00276
00277
00278
00281
00282 inline bool TZipDataSource::isValid() const
00283 {
00284 return m_bValid;
00285 }
00286
00287
00290
00291 inline Int TZipDataSource::getFileEntryCount() const
00292 {
00293 return m_lstFiles.getCount();
00294 }
00295
00296
00303
00304 inline Retval TZipDataSource::getFileEntryByName(const IString& rEntryName, TZippedFileEntry*& rpEntry) const
00305 {
00306 Int iIndex = m_mapLookup.getItemConst(rEntryName);
00307 return getFileEntry(iIndex, rpEntry);
00308 }
00309
00310
00314
00315 inline Retval TZipDataSource::removeFileEntry(const IString& rEntryName)
00316 {
00317 Int iIndex = m_mapLookup.getItemConst(rEntryName);
00318 return deleteFileEntry(iIndex);
00319 }
00320
00321
00322 END_NAMESPACE_Zeus
00323
00324 #endif