compressionlibs/ziplib/inc/ezgzip.h
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef __GZIP_H__
       
    17 #define __GZIP_H__
       
    18 
       
    19 #include <ezdecompressor.h>
       
    20 #include <ezcompressor.h>
       
    21 #include <ezfilebuffer.h>
       
    22 
       
    23 /**
       
    24 The TEZGZipHeader class encapsulates a zip file header, which is written to the start of the zip
       
    25 file to store various settings of the archive
       
    26 
       
    27 @publishedAll
       
    28 @released
       
    29 */
       
    30 class TEZGZipHeader
       
    31 	{
       
    32 
       
    33 public:
       
    34 	IMPORT_C TEZGZipHeader();
       
    35 	IMPORT_C ~TEZGZipHeader();
       
    36 	
       
    37 public:
       
    38 	TUint8 iId1;
       
    39 	TUint8 iId2;
       
    40 	TUint8 iCompressionMethod;
       
    41 	TUint8 iFlags;
       
    42 	TInt32 iTime;
       
    43 	TUint8 iExtraFlags;
       
    44 	TUint8 iOs;
       
    45 	TInt16 iXlen;
       
    46 	HBufC8* iExtra;
       
    47 	HBufC8* iFname;
       
    48 	HBufC8* iComment;
       
    49 	TInt16 iCrc;
       
    50 	};
       
    51 
       
    52 /**
       
    53 The TEZGZipTrailer class encapsulates a zip file trailer, which uses a CRC (cyclic redundancy check) to 
       
    54 confirm the validity of the unpacked archive
       
    55 
       
    56 @publishedAll
       
    57 @released
       
    58 */
       
    59 class TEZGZipTrailer
       
    60 	{
       
    61 public:
       
    62 	IMPORT_C TEZGZipTrailer();
       
    63 	IMPORT_C TEZGZipTrailer(TInt32 aCrc, TInt32 aSize);
       
    64 public:
       
    65 	TInt32 iCrc32;
       
    66 	TInt32 iSize;
       
    67 	};
       
    68 
       
    69 /**
       
    70 The EZGZipFile class handles writing / reading of headers and trailers to / from zip files
       
    71 
       
    72 @publishedAll
       
    73 @released
       
    74 */
       
    75 class EZGZipFile
       
    76 	{
       
    77 public:
       
    78 	
       
    79 	/** Zip file error codes */	
       
    80 	enum { 	ENotGZipFile = KEZlibErrNotGZipFile, 					
       
    81 			EInvalidCompressionMethod = KEZlibErrInvalidCompression,
       
    82 			EBadGZipHeader= KEZlibErrBadGZipHeader, 				
       
    83 			EBadGZipTrailer = KEZlibErrBadGZipTrailer, 				
       
    84 			EBadGZipCrc = KEZlibErrBadGZipCrc };					
       
    85 	/** Flags to determine which part of the header / trailer is being written */			
       
    86 	enum { EFText = 0, EFHcrc = 1, EFExtra = 2, EFName = 3, EFComment = 4};
       
    87 
       
    88 public:
       
    89 	IMPORT_C static void ReadHeaderL(RFile &aFile, TEZGZipHeader &aHeader);
       
    90 	IMPORT_C static void WriteHeaderL(RFile &aFile, TEZGZipHeader &aHeader);
       
    91 	IMPORT_C static void ReadTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer);
       
    92 	IMPORT_C static void WriteTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer);
       
    93 	IMPORT_C static void LocateAndReadTrailerL(RFs &aRfs, const TDesC &aFname, TEZGZipTrailer &aTrailer);
       
    94 	IMPORT_C static TBool IsGzipFile(RFs &aRfs, const TDesC &aFname);
       
    95 	IMPORT_C static TBool IsGzipFileL(RFs &aRfs, const TDesC &aFname);
       
    96 
       
    97 private:
       
    98 	static void ReadStringIntoDescriptorL(RFile &aFile, HBufC8 **aDes);
       
    99 
       
   100 public:
       
   101 	/** First of the header ID pair */
       
   102 	static const TUint8 ID1;
       
   103 	/** Second of the header ID pair */	
       
   104 	static const TUint8 ID2;
       
   105 	};
       
   106 
       
   107 /**
       
   108 The CEZFileToGzipBM manages the input and output buffers for compression
       
   109 
       
   110 @publishedAll
       
   111 @released
       
   112 */
       
   113 NONSHARABLE_CLASS(CEZFileToGzipBM) : public CEZFileBufferManager
       
   114 	{
       
   115 public:
       
   116 
       
   117 	static CEZFileToGzipBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize);
       
   118 	static CEZFileToGzipBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize);
       
   119 
       
   120 	/**
       
   121 	Initialise the stream with input and output buffers and starts reading
       
   122 
       
   123 	@param aZStream the stream to initialise
       
   124 	*/
       
   125 	virtual void InitializeL(CEZZStream &aZStream);
       
   126 	
       
   127 	/**
       
   128 	Set the stream's input buffer and starts reading
       
   129 	
       
   130 	@param aZStream the steam whose input buffer to set
       
   131 	*/	
       
   132 	virtual void NeedInputL(CEZZStream &aZStream);
       
   133 	
       
   134 	/**
       
   135 	Return the CRC - used for checking validity of the archive
       
   136 
       
   137 	@return the CRC value
       
   138 	*/	
       
   139 	TInt32 Crc() const { return iCrc; } ;
       
   140 
       
   141 private:
       
   142 	CEZFileToGzipBM(RFile &aInput, RFile &aOutput);
       
   143 private:
       
   144 	TInt32 iCrc;
       
   145 	};
       
   146 
       
   147 /**
       
   148 The CEZGzipToFileBM class manages the input and output buffers for de-compression
       
   149 
       
   150 @publishedAll
       
   151 @released
       
   152 */
       
   153 NONSHARABLE_CLASS(CEZGzipToFileBM) : public CEZFileBufferManager
       
   154 	{
       
   155 public:
       
   156 
       
   157 	static CEZGzipToFileBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize);
       
   158 	static CEZGzipToFileBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize);
       
   159 
       
   160 	/**
       
   161 	Finish writing to the stream
       
   162 
       
   163 	@param aZStream the stream to complete writing to
       
   164 	*/	
       
   165 	virtual void FinalizeL(CEZZStream &aZStream);
       
   166 	
       
   167 	/**
       
   168 	Set the stream's output buffer and start writing
       
   169 
       
   170 	@param aZStream the steam whose output buffer to set
       
   171 	*/	
       
   172 	virtual void NeedOutputL(CEZZStream &aZStream);
       
   173 	
       
   174 	/**
       
   175 	Return the CRC - used for checking validity of the archive
       
   176 
       
   177 	@return the CRC value
       
   178 	*/	
       
   179 	TInt32 Crc() const { return iCrc; } ;
       
   180 
       
   181 private:
       
   182 	CEZGzipToFileBM(RFile &aInput, RFile &aOutput);
       
   183 private:
       
   184 	TInt32 iCrc;
       
   185 	};
       
   186 
       
   187 
       
   188 /**
       
   189 A CEZGZipToFile object allows de-compression of a compressed file.
       
   190 The name of the compressed file is passed into the constructor along with a target file 
       
   191 to contain the uncompressed data.  The file is uncompressed by calling the InflateL() function.
       
   192 
       
   193 @publishedAll
       
   194 @released
       
   195 */
       
   196 class CEZGZipToFile : public CBase
       
   197 	{
       
   198 public:
       
   199 	~CEZGZipToFile();
       
   200 
       
   201 	IMPORT_C static CEZGZipToFile* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
       
   202 	IMPORT_C static CEZGZipToFile* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
       
   203 
       
   204 	IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000);
       
   205 
       
   206 	IMPORT_C TBool InflateL();
       
   207 
       
   208 private:
       
   209 	CEZGZipToFile();
       
   210 	void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize);
       
   211 	void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize);
       
   212 private:
       
   213 	CEZDecompressor *iDecompressor;
       
   214 	CEZGzipToFileBM *iBufferManager;
       
   215 	TEZGZipTrailer iTrailer;
       
   216 	RFile iGZipFile;
       
   217 	TEZGZipHeader iHeader;
       
   218 	};
       
   219 
       
   220 /**
       
   221 A CEZFileToGZip object allows compression of an uncompressed file to a zip file.
       
   222 The uncompressed source file is passed into the constructor along with the name of the target
       
   223 zip file.  The file is compressed by calling the DeflateL() function.
       
   224 
       
   225 @publishedAll
       
   226 @released
       
   227 */
       
   228 class CEZFileToGZip : public CBase
       
   229 	{
       
   230 public:
       
   231 	~CEZFileToGZip();
       
   232 
       
   233 	IMPORT_C static CEZFileToGZip* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
       
   234 	IMPORT_C static CEZFileToGZip* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
       
   235 
       
   236 	IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000);
       
   237 
       
   238 	IMPORT_C TBool DeflateL();
       
   239 
       
   240 private:
       
   241 	CEZFileToGZip();
       
   242 	void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize);
       
   243 	void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize);
       
   244 private:
       
   245 	CEZCompressor *iCompressor;
       
   246 	CEZFileToGzipBM *iBufferManager;
       
   247 	TInt iUncompressedDataSize;
       
   248 	RFile iGZipFile;
       
   249 	TEZGZipHeader iHeader;
       
   250 	};
       
   251 
       
   252 
       
   253 #endif