转自:http://hi.baidu.com/letsherwel/blog/item/deb91bf915352652252df286.html
接上一篇,记下如何用Symbian自带的GZip工具压缩其它文件及解压缩: 用到的类:CEZFileToGZip、CEZGZipToFile。 用法很简单-_-
//压缩 void CompressFileL(const TDesC& aSrcFile) { RFs& nFs = CCoeEnv::Static()->FsSession(); TBuf<256> nDestFile; nDestFile.Zero(); nDestFile.Append(aSrcFile); nDestFile.Append(_L(".gz"));
RFile nInput; CleanupClosePushL(nInput); TInt nRet = nInput.Open(nFs,aSrcFile,EFileRead|EFileStream); if (nRet == KErrNone) { CEZFileToGZip* nFileToGz = CEZFileToGZip::NewLC(nFs,nDestFile ,nInput); while ( nFileToGz->DeflateL () ) { ; } CleanupStack::PopAndDestroy(); } CleanupStack::PopAndDestroy(); }
//解压缩 void CNewTestAppView::DeCompressFileL(const TDesC& aSrcFile) { RFs& nFs = CCoeEnv::Static()->FsSession(); TBuf<256> nDestFile; nDestFile.Zero(); nDestFile.Append(aSrcFile); nDestFile.Append(_L(".gz")); //简单地加了后缀"gz", 偷点懒......
RFile nOutput; CleanupClosePushL(nOutput); TInt nRet = nOutput.Replace(nFs,nDestFile,EFileWrite|EFileShareAny); if (nRet == KErrNone) { CEZGZipToFile* nGzToFile = CEZGZipToFile::NewLC(nFs,aSrcFile,nOutput ); while ( nGzToFile->InflateL () ) { ; } CleanupStack::PopAndDestroy(); } CleanupStack::PopAndDestroy(); }
另外,值得注意的是用"
CEZFileToGZip "压缩的文件也可以用"
CEZDecompressor "来解压缩,关键要在解压前去掉相应的头信息(通过UltraEdit编辑器对"
CEZFileToGZip "和"
CEZCompressor "压缩同一个文件后生成的文件的比较,可以看出头信息即为文件的前
10 个字节)。
void DeCompressDataL(const TDesC& aFileName) { TInt nBufferSize = 128; HBufC8* nSrc = ReadFile(aFileName); if (aFileName.Right(3).CompareF(_L(".gz")) == 0) //如果是" CEZFileToGZip " 压缩的文件 {HBufC8* nTemp = nSrc->Mid(10).Alloc(); delete nSrc; nSrc = nTemp; } CleanupStack::PushL(nSrc);
CBufFlat* nBufFlat = CBufFlat::NewL(nBufferSize); CleanupStack::PushL(nBufFlat);
CBufferManager* nBufferManager = CBufferManager::NewLC(*nSrc,*nBufFlat,nBufferSize); CEZDecompressor* decompressor = CEZDecompressor::NewLC(*nBufferManager); while (decompressor->InflateL()) {// loop here until the file is compressed }
HBufC *decompressedFile = HBufC::NewL(aFileName.Length()+2); decompressedFile->Des().Format(KDecompressedFileName,&aFileName); WriteFile(nBufFlat->Ptr(0),*decompressedFile); //将解压后的内容写文件 delete decompressedFile; CleanupStack::PopAndDestroy(4); }