symbian壓縮和解壓縮GZIP檔案

來源:互聯網
上載者:User

開發夥伴平台:
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
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.