Compress and decompress the GZIP file in Symbian.

Source: Internet
Author: User
Tags gz file

Development partner platform:
S60 3rd edition, FP1

Detailed description
The following code snippet demonstrates how to compress and decompress the GZIP file. The cezfiletogzip class and cezgziptofi class are used here. The code can be executed through self-signature.

MPs File
The following link library is required

Code:
LIBRARY efsrv.lib
LIBRARY ezlib.lib

Source File

Code:
#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
}

Use the compressgzipfilel () and decompressgzipfilel () Methods
The following example demonstrates how to use the command line to compress or decompress the. GZ file.

Option:
*-C = Compress
*-D = decompress
*-B n = buffer size
* Filename = GZIP file

Code:
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
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.