williamr@2: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). williamr@2: // All rights reserved. williamr@2: // This component and the accompanying materials are made available williamr@4: // under the terms of "Eclipse Public License v1.0" williamr@2: // which accompanies this distribution, and is available williamr@4: // at the URL "http://www.eclipse.org/legal/epl-v10.html". williamr@2: // williamr@2: // Initial Contributors: williamr@2: // Nokia Corporation - initial contribution. williamr@2: // williamr@2: // Contributors: williamr@2: // williamr@2: // Description: williamr@2: // williamr@2: williamr@2: #ifndef __GZIP_H__ williamr@2: #define __GZIP_H__ williamr@2: williamr@2: #include williamr@2: #include williamr@2: #include williamr@2: williamr@2: /** williamr@2: The TEZGZipHeader class encapsulates a zip file header, which is written to the start of the zip williamr@2: file to store various settings of the archive williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: class TEZGZipHeader williamr@2: { williamr@2: williamr@2: public: williamr@2: IMPORT_C TEZGZipHeader(); williamr@2: IMPORT_C ~TEZGZipHeader(); williamr@2: williamr@2: public: williamr@2: TUint8 iId1; williamr@2: TUint8 iId2; williamr@2: TUint8 iCompressionMethod; williamr@2: TUint8 iFlags; williamr@2: TInt32 iTime; williamr@2: TUint8 iExtraFlags; williamr@2: TUint8 iOs; williamr@2: TInt16 iXlen; williamr@2: HBufC8* iExtra; williamr@2: HBufC8* iFname; williamr@2: HBufC8* iComment; williamr@2: TInt16 iCrc; williamr@2: }; williamr@2: williamr@2: /** williamr@2: The TEZGZipTrailer class encapsulates a zip file trailer, which uses a CRC (cyclic redundancy check) to williamr@2: confirm the validity of the unpacked archive williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: class TEZGZipTrailer williamr@2: { williamr@2: public: williamr@2: IMPORT_C TEZGZipTrailer(); williamr@2: IMPORT_C TEZGZipTrailer(TInt32 aCrc, TInt32 aSize); williamr@2: public: williamr@2: TInt32 iCrc32; williamr@2: TInt32 iSize; williamr@2: }; williamr@2: williamr@2: /** williamr@2: The EZGZipFile class handles writing / reading of headers and trailers to / from zip files williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: class EZGZipFile williamr@2: { williamr@2: public: williamr@2: williamr@2: /** Zip file error codes */ williamr@2: enum { ENotGZipFile = KEZlibErrNotGZipFile, williamr@2: EInvalidCompressionMethod = KEZlibErrInvalidCompression, williamr@2: EBadGZipHeader= KEZlibErrBadGZipHeader, williamr@2: EBadGZipTrailer = KEZlibErrBadGZipTrailer, williamr@2: EBadGZipCrc = KEZlibErrBadGZipCrc }; williamr@2: /** Flags to determine which part of the header / trailer is being written */ williamr@2: enum { EFText = 0, EFHcrc = 1, EFExtra = 2, EFName = 3, EFComment = 4}; williamr@2: williamr@2: public: williamr@2: IMPORT_C static void ReadHeaderL(RFile &aFile, TEZGZipHeader &aHeader); williamr@2: IMPORT_C static void WriteHeaderL(RFile &aFile, TEZGZipHeader &aHeader); williamr@2: IMPORT_C static void ReadTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer); williamr@2: IMPORT_C static void WriteTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer); williamr@2: IMPORT_C static void LocateAndReadTrailerL(RFs &aRfs, const TDesC &aFname, TEZGZipTrailer &aTrailer); williamr@2: IMPORT_C static TBool IsGzipFile(RFs &aRfs, const TDesC &aFname); williamr@2: IMPORT_C static TBool IsGzipFileL(RFs &aRfs, const TDesC &aFname); williamr@2: williamr@2: private: williamr@2: static void ReadStringIntoDescriptorL(RFile &aFile, HBufC8 **aDes); williamr@2: williamr@2: public: williamr@2: /** First of the header ID pair */ williamr@2: static const TUint8 ID1; williamr@2: /** Second of the header ID pair */ williamr@2: static const TUint8 ID2; williamr@2: }; williamr@2: williamr@2: /** williamr@2: The CEZFileToGzipBM manages the input and output buffers for compression williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: NONSHARABLE_CLASS(CEZFileToGzipBM) : public CEZFileBufferManager williamr@2: { williamr@2: public: williamr@2: williamr@2: static CEZFileToGzipBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize); williamr@2: static CEZFileToGzipBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize); williamr@2: williamr@2: /** williamr@2: Initialise the stream with input and output buffers and starts reading williamr@2: williamr@2: @param aZStream the stream to initialise williamr@2: */ williamr@2: virtual void InitializeL(CEZZStream &aZStream); williamr@2: williamr@2: /** williamr@2: Set the stream's input buffer and starts reading williamr@2: williamr@2: @param aZStream the steam whose input buffer to set williamr@2: */ williamr@2: virtual void NeedInputL(CEZZStream &aZStream); williamr@2: williamr@2: /** williamr@2: Return the CRC - used for checking validity of the archive williamr@2: williamr@2: @return the CRC value williamr@2: */ williamr@2: TInt32 Crc() const { return iCrc; } ; williamr@2: williamr@2: private: williamr@2: CEZFileToGzipBM(RFile &aInput, RFile &aOutput); williamr@2: private: williamr@2: TInt32 iCrc; williamr@2: }; williamr@2: williamr@2: /** williamr@2: The CEZGzipToFileBM class manages the input and output buffers for de-compression williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: NONSHARABLE_CLASS(CEZGzipToFileBM) : public CEZFileBufferManager williamr@2: { williamr@2: public: williamr@2: williamr@2: static CEZGzipToFileBM* NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize); williamr@2: static CEZGzipToFileBM* NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize); williamr@2: williamr@2: /** williamr@2: Finish writing to the stream williamr@2: williamr@2: @param aZStream the stream to complete writing to williamr@2: */ williamr@2: virtual void FinalizeL(CEZZStream &aZStream); williamr@2: williamr@2: /** williamr@2: Set the stream's output buffer and start writing williamr@2: williamr@2: @param aZStream the steam whose output buffer to set williamr@2: */ williamr@2: virtual void NeedOutputL(CEZZStream &aZStream); williamr@2: williamr@2: /** williamr@2: Return the CRC - used for checking validity of the archive williamr@2: williamr@2: @return the CRC value williamr@2: */ williamr@2: TInt32 Crc() const { return iCrc; } ; williamr@2: williamr@2: private: williamr@2: CEZGzipToFileBM(RFile &aInput, RFile &aOutput); williamr@2: private: williamr@2: TInt32 iCrc; williamr@2: }; williamr@2: williamr@2: williamr@2: /** williamr@2: A CEZGZipToFile object allows de-compression of a compressed file. williamr@2: The name of the compressed file is passed into the constructor along with a target file williamr@2: to contain the uncompressed data. The file is uncompressed by calling the InflateL() function. williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: class CEZGZipToFile : public CBase williamr@2: { williamr@2: public: williamr@2: ~CEZGZipToFile(); williamr@2: williamr@2: IMPORT_C static CEZGZipToFile* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000); williamr@2: IMPORT_C static CEZGZipToFile* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000); williamr@2: williamr@2: IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize = 0x8000); williamr@2: williamr@2: IMPORT_C TBool InflateL(); williamr@2: williamr@2: private: williamr@2: CEZGZipToFile(); williamr@2: void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize); williamr@2: void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize); williamr@2: private: williamr@2: CEZDecompressor *iDecompressor; williamr@2: CEZGzipToFileBM *iBufferManager; williamr@2: TEZGZipTrailer iTrailer; williamr@2: RFile iGZipFile; williamr@2: TEZGZipHeader iHeader; williamr@2: }; williamr@2: williamr@2: /** williamr@2: A CEZFileToGZip object allows compression of an uncompressed file to a zip file. williamr@2: The uncompressed source file is passed into the constructor along with the name of the target williamr@2: zip file. The file is compressed by calling the DeflateL() function. williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: class CEZFileToGZip : public CBase williamr@2: { williamr@2: public: williamr@2: ~CEZFileToGZip(); williamr@2: williamr@2: IMPORT_C static CEZFileToGZip* NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000); williamr@2: IMPORT_C static CEZFileToGZip* NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000); williamr@2: williamr@2: IMPORT_C void ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize = 0x8000); williamr@2: williamr@2: IMPORT_C TBool DeflateL(); williamr@2: williamr@2: private: williamr@2: CEZFileToGZip(); williamr@2: void ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize); williamr@2: void InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize); williamr@2: private: williamr@2: CEZCompressor *iCompressor; williamr@2: CEZFileToGzipBM *iBufferManager; williamr@2: TInt iUncompressedDataSize; williamr@2: RFile iGZipFile; williamr@2: TEZGZipHeader iHeader; williamr@2: }; williamr@2: williamr@2: williamr@2: #endif