00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00028 #include "ezlibexample.h"
00029 #include <e32cons.h>
00030 #include <ezgzip.h>
00031
00032 _LIT(KTitle, "Ezlib example");
00033 _LIT(KTextPressAKey, "\n\nPress any key to step through the example\n");
00034 _LIT(KExit,"\nPress any key to exit the application");
00035 _LIT(KPressAKey,"\nPress any key to continue \n");
00036 _LIT(KProperties,"Press any key to have a look at the properties of the zipped files \n");
00037 _LIT(KDecompress,"Gzip file successfully decompressed\n");
00038 _LIT(KCompress,"\nFile successfully compressed to gzip format and saved\n");
00039 _LIT(KNoFilesFound,"No appropriate files located in this folder.\r\n");
00040 _LIT(KLineSpace, "\r\n");
00041 _LIT(KFileInfo,"\nCompressing file %S");
00042 _LIT(KFileOpen,"\nOpening file: %S\r\n");
00043 _LIT(KFileExtract,"\nExtracting the file %S\n");
00044 _LIT(KFileZipExtract,"\nExtracting files from the zip file %S\n");
00045
00046 #if defined(__WINS__)
00047 _LIT(KCInputZipFile, "\\private\\E80000B7\\zip\\input\\EzlibZipFolder.zip");
00048 _LIT(KCInputGzipPath, "\\private\\E80000B7\\gzip\\error.wav");
00049 _LIT(KCInputGzipFile, "\\private\\E80000B7\\gzip\\icon.bmp.gz");
00050 #endif
00051
00052 #if defined(__EPOC32__)
00053 _LIT(KZInputZipFile, "z:\\private\\E80000B7\\zip\\input\\EzlibZipFolder.zip");
00054 _LIT(KZInputGzipPath, "z:\\private\\E80000B7\\gzip\\error.wav");
00055 _LIT(KZInputGzipFile, "z:\\private\\E80000B7\\gzip\\icon.bmp.gz");
00056 #endif
00057
00058 _LIT(KZipName,"\nName: %S ");
00059 _LIT(KZipCRC32,"\nCRC32: %X ");
00060 _LIT(KZipCSize,"\nCompressed Size: %d");
00061 _LIT(KZipUCSize,"\nUncompressed Size: %d");
00062 _LIT(KOpenedSuccessfully, "%S opened successfully.\n");
00063
00064 _LIT(KExtractZipPathMulti, "\\private\\E80000B7\\zip\\extracts\\");
00065 _LIT(KExtractZipPathSingle, "\\private\\E80000B7\\zip\\extracts\\singlefile\\");
00066 _LIT(KCompressGzipPath, "\\private\\E80000B7\\gzip\\extracts\\");
00067 _LIT(KCompressZipFile, "icon.bmp");
00068 _LIT(KExtractGzipFile, "error.wav");
00069
00075 CEzlibExample * CEzlibExample::NewLC()
00076 {
00077 CEzlibExample * rep = new(ELeave) CEzlibExample();
00078 CleanupStack::PushL(rep);
00079 rep->ConstructL();
00080 return rep;
00081 }
00082
00086 CEzlibExample::CEzlibExample()
00087 {
00088 }
00089
00090 void CEzlibExample::ConstructL()
00091 {
00092 iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00093 iConsole->Printf(KTextPressAKey);
00094 iConsole->Getch();
00095 }
00096
00100 CEzlibExample::~CEzlibExample()
00101 {
00102 iConsole->Printf(KExit);
00103 iConsole->Getch();
00104
00105 delete iConsole;
00106 }
00107
00108
00123 void CEzlibExample::GetPropertiesL(TFileName& aFileName, RFs& aFsSession)
00124 {
00125 CZipFile* zipFile = 0;
00126 CZipFileMember* member = 0;
00127 CZipFileMemberIterator* fileMembers;
00128
00129 iConsole->Printf(KFileOpen, &aFileName);
00130
00131 zipFile = CZipFile::NewL(aFsSession, aFileName);
00132 CleanupStack::PushL(zipFile);
00133
00134 iConsole->Printf(KOpenedSuccessfully, &aFileName);
00135
00136
00137
00138
00139 _LIT(KSearchFile, "EzlibZipFolder\\icon.bmp");
00140 member = zipFile->CaseInsensitiveMemberL(KSearchFile);
00141 CleanupStack::PushL(member);
00142 if(member != NULL)
00143 {
00144
00145 ExtractFilesL(member, zipFile, aFsSession, KExtractZipPathSingle);
00146 iConsole->Printf(KFileExtract, &KSearchFile);
00147 }
00148 else
00149 {
00150 iConsole->Printf(KNoFilesFound);
00151 }
00152 CleanupStack::PopAndDestroy(member);
00153
00154
00155 fileMembers = zipFile->GetMembersL();
00156 CleanupStack::PushL(fileMembers);
00157
00158
00159 member = fileMembers->NextL();
00160
00161 iConsole->Printf(KFileZipExtract, &aFileName);
00162 iConsole->Printf(KProperties);
00163 iConsole->Getch();
00164
00165
00166 while(member != NULL)
00167 {
00168 CleanupStack::PushL(member);
00169
00170
00171
00172 TUint32 redundencyCode = member->CRC32();
00173
00174
00175
00176 TUint32 UnCompSize = member->UncompressedSize();
00177
00178
00179
00180 TUint32 CompSize = member->CompressedSize();
00181
00182
00183
00184 TFileName fileName = *member->Name();
00185 iConsole->Printf(KZipName, &fileName);
00186
00187
00188 iConsole->Printf(KZipCRC32, redundencyCode);
00189
00190
00191 iConsole->Printf(KZipCSize, CompSize);
00192
00193
00194 iConsole->Printf(KZipUCSize,UnCompSize);
00195
00196
00197 ExtractFilesL(member, zipFile, aFsSession, KExtractZipPathMulti);
00198
00199 CleanupStack::PopAndDestroy(member);
00200 member = fileMembers->NextL();
00201 }
00202
00203 CleanupStack::PopAndDestroy(fileMembers);
00204 CleanupStack::PopAndDestroy(zipFile);
00205 }
00206
00218 void CEzlibExample::ExtractFilesL(const CZipFileMember* aMember, CZipFile* aZipFile, RFs& aFsSession, const TDesC& aOutputPath)
00219 {
00220 TInt loop = 0;
00221
00222
00223 HBufC* name = aMember->Name()->AllocLC();
00224 while(loop < name->Length())
00225 {
00226 if((*name)[loop] == '/')
00227 {
00228 name->Des()[loop] = '\\';
00229 }
00230 loop++;
00231 }
00232
00233
00234 TFileName fn;
00235 fn.Append(aOutputPath);
00236
00237
00238 fn.Append(*name);
00239 aFsSession.MkDirAll(fn);
00240
00241 RFile extractedMember;
00242 extractedMember.Replace(aFsSession,fn, EFileShareAny|EFileWrite);
00243 CleanupClosePushL(extractedMember);
00244
00245 RZipFileMemberReaderStream* fileStream;
00246
00247 TInt error = aZipFile->GetInputStreamL(aMember, fileStream);
00248 if(error != KErrNone)
00249 {
00250 _LIT(KCompressionNotSupported, "Error! Compression Method Not Supported");
00251 iConsole->Printf(KCompressionNotSupported);
00252 iConsole->Printf(KLineSpace);
00253 CleanupStack::PopAndDestroy(2,name);
00254 aFsSession.Delete(fn);
00255 return;
00256 }
00257 CleanupStack::PushL(fileStream);
00258
00259
00260
00261 TUint16 size = aMember->UncompressedSize();
00262
00263 RBuf8 bytes;
00264 bytes.CreateL(size);
00265 bytes.CleanupClosePushL();
00266
00267
00268
00269 User::LeaveIfError(fileStream->Read(bytes,size));
00270
00271 User::LeaveIfError(extractedMember.Write(bytes));
00272
00273 CleanupStack::PopAndDestroy(4,name);
00274
00275 }
00276
00285 void CEzlibExample::ExtractGzipFileL(RFs& aFsSession)
00286 {
00287 iConsole->Printf(KPressAKey);
00288 iConsole->Getch();
00289
00290 #if defined(__WINS__)
00291 TFileName gzipInputFileName(KCInputGzipFile);
00292 #endif
00293
00294 #if defined(__EPOC32__)
00295 TFileName gzipInputFileName(KZInputGzipFile);
00296 #endif
00297
00298
00299
00300 RFile input;
00301 RFile output;
00302
00303 User::LeaveIfError(input.Open(aFsSession, gzipInputFileName, EFileStream | EFileRead | EFileShareAny));
00304 CleanupClosePushL(input);
00305
00306 TFileName outputFile(KCompressGzipPath);
00307
00308
00309 aFsSession.MkDirAll(outputFile);
00310 outputFile.Append(KCompressZipFile);
00311
00312
00313
00314 output.Replace(aFsSession, outputFile, EFileStream | EFileWrite | EFileShareExclusive);
00315 CleanupClosePushL(output);
00316
00317
00318 TPtrC inputFile = gzipInputFileName;
00319
00320 RBuf uncompressedFile;
00321 uncompressedFile.CreateL(outputFile.Length());
00322 uncompressedFile.CleanupClosePushL();
00323
00324 _LIT(KUfl,"%S");
00325
00326 uncompressedFile.Format(KUfl,&inputFile);
00327
00328 _LIT(KInfo,"\nDecompressing file %S\n");
00329 iConsole->Printf(KInfo,&inputFile);
00330
00331 CEZGZipToFile *def = CEZGZipToFile::NewLC(aFsSession,uncompressedFile,output);
00332
00333
00334 while(def->InflateL())
00335 {
00336
00337
00338 }
00339 iConsole->Printf(KDecompress);
00340
00341 CleanupStack::PopAndDestroy(4);
00342
00343 }
00344
00353 void CEzlibExample::CompressToGzipFileL(RFs& aFsSession)
00354 {
00355 iConsole->Printf(KPressAKey);
00356 iConsole->Getch();
00357
00358 #if defined(__WINS__)
00359 TFileName inputFileToBeGzipped(KCInputGzipPath);
00360 #endif
00361
00362 #if defined(__EPOC32__)
00363 TFileName inputFileToBeGzipped(KZInputGzipPath);
00364 #endif
00365
00366
00367 RFile input;
00368
00369 User::LeaveIfError(input.Open(aFsSession, inputFileToBeGzipped, EFileStream | EFileRead | EFileShareAny));
00370 CleanupClosePushL(input);
00371
00372
00373
00374 TFileName outputFile(KCompressGzipPath);
00375 aFsSession.MkDirAll(outputFile);
00376 outputFile.Append(KExtractGzipFile);
00377
00378 iConsole->Printf(KFileInfo, &inputFileToBeGzipped);
00379
00380
00381
00382 const TInt extensionLength = 3;
00383
00384 RBuf compressedFile;
00385 compressedFile.CreateL(outputFile.Length() + extensionLength);
00386 compressedFile.CleanupClosePushL();
00387
00388 _LIT(KAppendExtension,"%S.gz");
00389
00390 compressedFile.Format(KAppendExtension,&outputFile);
00391
00392 CEZFileToGZip *def = CEZFileToGZip::NewLC(aFsSession, compressedFile, input);
00393
00394 while(def->DeflateL())
00395 {
00396
00397
00398 }
00399
00400 iConsole->Printf(KCompress);
00401
00402 CleanupStack::PopAndDestroy(3);
00403
00404 }
00405
00406
00407 LOCAL_C void MainL()
00408 {
00409
00410 CActiveScheduler* scheduler = new(ELeave) CActiveScheduler;
00411 CleanupStack::PushL(scheduler);
00412 CActiveScheduler::Install( scheduler );
00413
00414 CEzlibExample * app = CEzlibExample::NewLC();
00415
00416 RFs fsSession;
00417
00418 User::LeaveIfError(fsSession.Connect());
00419
00420
00421
00422
00423
00424
00425 fsSession.CreatePrivatePath(RFs::GetSystemDrive());
00426
00427 fsSession.SetSessionToPrivate(RFs::GetSystemDrive());
00428
00429
00430 #if defined(__WINS__)
00431 TBuf<256> inputFileName(KCInputZipFile);
00432 #endif
00433
00434 #if defined(__EPOC32__)
00435 TBuf<256> inputFileName(KZInputZipFile);
00436 #endif
00437
00438 app->GetPropertiesL(inputFileName, fsSession);
00439
00440
00441 app->CompressToGzipFileL(fsSession);
00442
00443
00444 app->ExtractGzipFileL(fsSession);
00445
00446 CleanupStack::PopAndDestroy(2);
00447
00448 fsSession.Close();
00449
00450 }
00451
00452 GLDEF_C TInt E32Main()
00453 {
00454 __UHEAP_MARK;
00455 CTrapCleanup* cleanup = CTrapCleanup::New();
00456 if(cleanup == NULL)
00457 {
00458 return KErrNoMemory;
00459 }
00460 TRAPD(err, MainL());
00461 if(err != KErrNone)
00462 {
00463 User::Panic(_L("Failed to complete"),err);
00464 }
00465
00466 delete cleanup;
00467 __UHEAP_MARKEND;
00468 return KErrNone;
00469 }
00470
00471