diff -r 89d6a7a84779 -r 25a17d01db0c Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/ezlibexample_8cpp-source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/ezlibexample_8cpp-source.html Fri Jan 22 18:26:19 2010 +0000 @@ -0,0 +1,428 @@ + +
+00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). +00002 // All rights reserved. +00003 // This component and the accompanying materials are made available +00004 // under the terms of "Eclipse Public License v1.0" +00005 // which accompanies this distribution, and is available +00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". +00007 // +00008 // Initial Contributors: +00009 // Nokia Corporation - initial contribution. +00010 // +00011 // Contributors: +00012 // +00013 // Description: +00014 // This example program demonstrates the use of the Ezlib API. +00015 // The code demonstrates how to +00016 // -# Open a ZIP file and get the properties of each file +00017 // -# Extract a selected member of the ZIP file and save it to a new file. +00018 // -# Save all the members of a ZIP file to a file. +00019 // -# Open a GZIP file and extract the contents to a file. +00020 // -# Compress a file into a GZIP file. +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 // Search for a file in the zip archive. +00137 // The name of the file to be searched for +00138 // in the zipfile is case-insensitive. +00139 _LIT(KSearchFile, "EzlibZipFolder\\icon.bmp"); +00140 member = zipFile->CaseInsensitiveMemberL(KSearchFile); +00141 CleanupStack::PushL(member); +00142 if(member != NULL) +00143 { +00144 // Extract the file that was searched for and found. +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 // Gets the iterator used for iterating through the files +00154 // contained in the ZIP file. +00155 fileMembers = zipFile->GetMembersL(); +00156 CleanupStack::PushL(fileMembers); +00157 // Moves the entry iterator onto the next +00158 // zip file member contained in the ZIP file +00159 member = fileMembers->NextL(); +00160 +00161 iConsole->Printf(KFileZipExtract, &aFileName); +00162 iConsole->Printf(KProperties); +00163 iConsole->Getch(); +00164 +00165 // Get the properties of all the files +00166 while(member != NULL) +00167 { +00168 CleanupStack::PushL(member); +00169 +00170 // Retrieve CRC(Cyclic Redundancy Check) value in a compressed file +00171 // contained in a CZipFile archive file. +00172 TUint32 redundencyCode = member->CRC32(); +00173 +00174 // Retrieve the size of uncompressed file +00175 // contained in a CZipFile archive file. +00176 TUint32 UnCompSize = member->UncompressedSize(); +00177 +00178 // Retrieve the size of compressed file +00179 // contained in a CZipFile archive file. +00180 TUint32 CompSize = member->CompressedSize(); +00181 +00182 // Print the properties +00183 // Name of the zipped file +00184 TFileName fileName = *member->Name(); +00185 iConsole->Printf(KZipName, &fileName); +00186 +00187 // CRC value which will be printed in hexadecimal +00188 iConsole->Printf(KZipCRC32, redundencyCode); +00189 +00190 // Size of the compressed file +00191 iConsole->Printf(KZipCSize, CompSize); +00192 +00193 // Original size of te file +00194 iConsole->Printf(KZipUCSize,UnCompSize); +00195 +00196 // Extract each member in the zip file +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 // Allocate memory for the retrieved name of a +00222 // compressed file contained in a CZipFile archive file. +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 // Create the directory to which the files need to be extracted. +00234 TFileName fn; +00235 fn.Append(aOutputPath); +00236 // Append the name of the file to the directory +00237 // where the file would get extracted. +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 // Create and return the input stream for a file in the archive. +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); //expanded member, name +00254 aFsSession.Delete(fn); +00255 return; +00256 } +00257 CleanupStack::PushL(fileStream); +00258 +00259 // Retrieve the size of uncompressed file contained +00260 // in a CZipFile archive file. +00261 TUint16 size = aMember->UncompressedSize(); +00262 +00263 RBuf8 bytes; +00264 bytes.CreateL(size); +00265 bytes.CleanupClosePushL(); +00266 +00267 // Read the specified number of bytes of binary data +00268 // from the file at the current position. +00269 User::LeaveIfError(fileStream->Read(bytes,size)); +00270 // Write to the file +00271 User::LeaveIfError(extractedMember.Write(bytes)); +00272 +00273 CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and 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 //TFileName gzipInputFileName(KInputGzipFile); +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 // Append the path to the directory where you want to +00308 // store the uncompressed file. +00309 aFsSession.MkDirAll(outputFile); +00310 outputFile.Append(KCompressZipFile); +00311 +00312 // Create and open icon.bmp on +00313 // c:\private\E80000B7\gzip\extracts\ folder. +00314 output.Replace(aFsSession, outputFile, EFileStream | EFileWrite | EFileShareExclusive); +00315 CleanupClosePushL(output); +00316 +00317 // icon.bmp.gz file is the input file +00318 TPtrC inputFile = gzipInputFileName; +00319 +00320 RBuf uncompressedFile; +00321 uncompressedFile.CreateL(outputFile.Length()); +00322 uncompressedFile.CleanupClosePushL(); +00323 +00324 _LIT(KUfl,"%S"); +00325 // Format and copy the filename into the descriptor +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 // Decompress the gzip file +00334 while(def->InflateL()) +00335 { +00336 // InflateL decompresses the file in stages. +00337 // It returns false when decompression is complete +00338 } +00339 iConsole->Printf(KDecompress); +00340 +00341 CleanupStack::PopAndDestroy(4); // input, output, uncompressedFile, def +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 //TFileName inputFileToBeGzipped(KInputGzipPath); +00366 +00367 RFile input; +00368 +00369 User::LeaveIfError(input.Open(aFsSession, inputFileToBeGzipped, EFileStream | EFileRead | EFileShareAny)); +00370 CleanupClosePushL(input); +00371 +00372 // Set the output path to the directory where you +00373 // want to store the compressed file. +00374 TFileName outputFile(KCompressGzipPath); +00375 aFsSession.MkDirAll(outputFile); +00376 outputFile.Append(KExtractGzipFile); +00377 +00378 iConsole->Printf(KFileInfo, &inputFileToBeGzipped); +00379 +00380 // Prevent the length of the descriptor to which data is written, +00381 // from exceeding its maximum length by appending additional length. +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 // Format and copy the filename into the descriptor +00390 compressedFile.Format(KAppendExtension,&outputFile); +00391 +00392 CEZFileToGZip *def = CEZFileToGZip::NewLC(aFsSession, compressedFile, input); +00393 // Compress the file to gzip format +00394 while(def->DeflateL()) +00395 { +00396 // DeflateL compresses the file in stages. +00397 // It returns false when compression is complete +00398 } +00399 +00400 iConsole->Printf(KCompress); +00401 +00402 CleanupStack::PopAndDestroy(3); // def, compressedFile, input +00403 +00404 } +00405 +00406 +00407 LOCAL_C void MainL() +00408 { +00409 // Create an Active Scheduler to handle asychronous calls +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 // Connect to file session +00418 User::LeaveIfError(fsSession.Connect()); +00419 +00420 // Create the private directory on the writable drive +00421 // i.e. "\private\E80000B7\" +00422 // Note that the number E80000B7 is the process security id +00423 // taken from the 2nd UID specified in the mmp file. +00424 +00425 fsSession.CreatePrivatePath(RFs::GetSystemDrive()); +00426 // Set the session path to this private directory on the writable drive +00427 fsSession.SetSessionToPrivate(RFs::GetSystemDrive()); +00428 +00429 // Get the properties of files in the zip file +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 // Compress a file to gzip format +00441 app->CompressToGzipFileL(fsSession); +00442 +00443 // Extract a gzip file +00444 app->ExtractGzipFileL(fsSession); +00445 +00446 CleanupStack::PopAndDestroy(2); //app, scheduler +00447 // Close the file session +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 +