This page was last modified 11:26, 14 May 2008.
CS000949 - Compressing and decompressing GZIP files
From Forum Nokia Wiki
| ID | CS000949 | Creation date | May 14, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Symbian C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): RFile,CEZFileToGZip,CEZGZipToFile,RFile::Open(),RFile::Create()CEZFileToGZip::DeflateL(),CEZGZipToFile::InflateL() |
Overview
This snippet shows how to compress and decompress GZIP files using classes CEZFileToGZip and CEZGZipToFile.
This snippet can be self-signed.
MMP file
The following libraries are required:
LIBRARY efsrv.lib LIBRARY ezlib.lib
Header file
Source file
#include <ezgzip.h> #include <f32file.h> void CompressGZipFileL(RFs &aFs, TInt aBufferSize, const TDesC& aFileName) { RFile input; HBufC *compressedFile = HBufC::NewLC(aFileName.Length()+3); _LIT(KCompressedGZipFileName,"%S.gz"); compressedFile->Des().Format(KCompressedGZipFileName,&aFileName); User::LeaveIfError(input.Open(aFs,aFileName,EFileStream | EFileRead | EFileShareAny)); CleanupClosePushL(input); CEZFileToGZip *fileToGZip = CEZFileToGZip::NewLC(aFs,*compressedFile,input,aBufferSize); _LIT(KCompressingToGZipFile,"Compressing file %S to gzip file %S\n"); console->Printf(KCompressingToGZipFile,&aFileName,compressedFile); while (fileToGZip->DeflateL()) { // loop here until the gzip file is created } CleanupStack::PopAndDestroy(3); //fileToGZip, input, compressedFile } void DecompressGZipFileL(RFs &aFs, TInt aBufferSize, const TDesC& aFileName) { TInt err(KErrNone); RFile output; HBufC *decompressedFile = HBufC::NewLC(aFileName.Length()+1); _LIT(KDecompressedGZipFileName,"%S_"); decompressedFile->Des().Format(KDecompressedGZipFileName,&aFileName); err = output.Create(aFs, *decompressedFile,EFileStream | EFileWrite | EFileShareExclusive); if (err == KErrAlreadyExists) User::LeaveIfError(output.Open(aFs, *decompressedFile,EFileStream | EFileWrite | EFileShareExclusive)); else User::LeaveIfError(err); CleanupClosePushL(output); CEZGZipToFile *gZipToFile = CEZGZipToFile::NewLC(aFs,aFileName,output,aBufferSize); _LIT(KDecompressingFromGZipFile,"Decompressing file %S from gzip file %S\n"); console->Printf(KDecompressingFromGZipFile, decompressedFile, &aFileName); while (gZipToFile->InflateL()) { // loop here until the gzip file is decompressed } CleanupStack::PopAndDestroy(3); //gZipToFile, output, decompressedFile }
Using CompressGZipFileL() and DecompressGZipFileL() methods
This simple example method reads command line arguments and compresses or decompresses .gz files.
options:
- -c = compress
- -d = decompress
- -b n = buffer size
- filename = gzip file
void doGZipCompressionAndDecompressionL() { RFs fs; User::LeaveIfError(fs.Connect()); CleanupClosePushL(fs); TBool doCompress = ETrue; TInt bufferSize = 32768; TInt cmdLineLen = User::CommandLineLength(); if (cmdLineLen <= 0) { _LIT(KUsage,"Usage: program.exe [-cd] [-u buffer] filename\n"); console->Printf(KUsage); User::Leave(KErrGeneral); } HBufC *argv = HBufC::NewLC(cmdLineLen); TPtr argPtr=argv->Des(); User::CommandLine(argPtr); TLex arguments(*argv); TPtrC options(arguments.NextToken()); TBool bufferSizeSpecified = EFalse; _LIT(KInvalidOption,"Invalid option %S\n"); _LIT(KUnknownOption,"Unknown option %S\n"); _LIT(KNoOptionSpecified,"No option specified\n"); while (options[0]=='-' || bufferSizeSpecified) { TInt index = 1; if (bufferSizeSpecified) { if (options.Length() == 0) { console->Printf(KNoOptionSpecified); console->Getch(); User::Leave(KErrGeneral); } else { TLex lex(options); TInt ret(KErrNone); ret = lex.Val(bufferSize); bufferSizeSpecified = EFalse; if (ret != KErrNone) { console->Printf(KInvalidOption,&options); console->Getch(); User::Leave(KErrGeneral); } } } else { while (index < options.Length()) { if (options[index] == 'd') doCompress = EFalse; else if (options[index] == 'c') doCompress = ETrue; else if (options[index] == 'b' ) bufferSizeSpecified = ETrue; else { console->Printf(KUnknownOption,&options); console->Getch(); User::Leave(KErrGeneral); } index++; } if (index == 1) { console->Printf(KNoOptionSpecified); console->Getch(); User::Leave(KErrGeneral); } } options.Set(arguments.NextToken()); } TPtrC fileNamePtr(options); HBufC *fileNameBuf = HBufC::NewLC(fileNamePtr.Length()); *fileNameBuf = fileNamePtr; if(doCompress) CompressGZipFileL(fs,bufferSize,*fileNameBuf); else DecompressGZipFileL(fs,bufferSize,*fileNameBuf); CleanupStack::PopAndDestroy(3); //fileNameBuf,argv,fs }
Postconditions
Depending on given parameters and the used method, GZIP files are compressed or decompressed.
See also
CS000948 - Getting trailer and header info from GZIP files
The gzip format is specified in the page http://www.faqs.org/rfcs/rfc1952.html.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Carbide cannot find my include files help!! | burakalakus | Carbide.c++ and CodeWarrior Tools | 4 | 2006-06-14 23:16 |
| Help pleace MIDlet Nokia 3100 | jcaicedo | Mobile Java General | 1 | 2005-05-07 07:22 |
| where is the path of the rms files in nokia mobile | ravibabu | Mobile Java General | 2 | 2007-11-02 22:15 |
| Can carbide handle included files in mmp files | svdwal | Carbide.c++ and CodeWarrior Tools | 4 | 2006-12-04 17:14 |
| Build with source files having the same name? | pailes | Symbian Tools & SDKs | 3 | 2008-03-14 08:02 |

