×
Namespaces

Variants
Actions

Archived:Compressing and decompressing GZIP files using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition FP1
Article
Keywords: RFile,CEZFileToGZip,CEZGZipToFile,RFile::Open(),RFile::Create()CEZFileToGZip::DeflateL(), CEZGZipToFile::InflateL()
Created: aknyman (06 May 2008)
Last edited: lpvalente (16 Sep 2012)

Contents

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

Archived:Getting trailer and header info from GZIP files using Symbian C++

The gzip format is specified in the page http://www.faqs.org/rfcs/rfc1952.html.

This page was last modified on 16 September 2012, at 01:56.
102 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×