开发伙伴平台:
S60 3rd Edition, FP1
详细描述
下列代码片段演示了如何压缩和解压缩GZIP文件,这里使用了CEZFileToGZip类和CEZGZipToFi类。代码可以通过自签名执行。
MMP文件
需要下列链接库
Code:
LIBRARY efsrv.lib
LIBRARY ezlib.lib
源文件
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
}
使用CompressGZipFileL()和DecompressGZipFileL()方法
下面这个示例演示了通过读取命令行参数来压缩或解压缩.gz文件
选项:
* -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
}