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