examples/SysLibs/EzlibExample/ezlibexample.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 This example program demonstrates the use of the Ezlib API. 
00029 The code demonstrates how to 
00030 -# Open a ZIP file and get the properties of each file
00031 -# Extract a selected member of the ZIP file and save it to a new file. 
00032 -# Save all the members of a ZIP file to a file.
00033 -# Open a GZIP file and extract the contents to a file. 
00034 -# Compress a file into a GZIP file. 
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         // Search for a file in the zip archive.
00153         // The name of the file to be searched for 
00154         // in the zipfile is case-insensitive.
00155         _LIT(KSearchFile, "EzlibZipFolder\\icon.bmp");
00156         member = zipFile->CaseInsensitiveMemberL(KSearchFile);
00157         CleanupStack::PushL(member);
00158         if(member != NULL)
00159                 {
00160                 // Extract the file that was searched for and found.
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         // Gets the iterator used for iterating through the files 
00170         // contained in the ZIP file.   
00171         fileMembers = zipFile->GetMembersL();
00172         CleanupStack::PushL(fileMembers);
00173         // Moves the entry iterator onto the next 
00174         // zip file member contained in the ZIP file
00175         member = fileMembers->NextL();
00176 
00177         iConsole->Printf(KFileZipExtract, &aFileName);
00178         iConsole->Printf(KProperties);
00179         iConsole->Getch();
00180         
00181         // Get the properties of all the files
00182         while(member != NULL)
00183                 {
00184                 CleanupStack::PushL(member);
00185                 
00186                 // Retrieve CRC(Cyclic Redundancy Check) value in a compressed file 
00187                 // contained in a CZipFile archive file.
00188                 TUint32 redundencyCode = member->CRC32();
00189                 
00190                 // Retrieve the size of uncompressed file 
00191                 // contained in a CZipFile archive file.
00192                 TUint32 UnCompSize = member->UncompressedSize();
00193                 
00194                 // Retrieve the size of compressed file 
00195                 // contained in a CZipFile archive file.
00196                 TUint32 CompSize = member->CompressedSize();
00197                 
00198                 // Print the properties
00199                 // Name of the zipped file
00200                 TFileName fileName = *member->Name();
00201                 iConsole->Printf(KZipName, &fileName);
00202                 
00203                 // CRC value which will be printed in hexadecimal
00204                 iConsole->Printf(KZipCRC32, redundencyCode);
00205                 
00206                 // Size of the compressed file
00207                 iConsole->Printf(KZipCSize, CompSize);
00208                 
00209                 // Original size of te file
00210                 iConsole->Printf(KZipUCSize,UnCompSize);
00211                 
00212                 // Extract each member in the zip file
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         // Allocate memory for the retrieved name of a 
00238         // compressed file contained in a CZipFile archive file.
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         // Create the directory to which the files need to be extracted.
00250         TFileName fn;
00251         fn.Append(aOutputPath);
00252         // Append the name of the file to the directory 
00253         // where the file would get extracted.
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         // Create and return the input stream for a file in the archive.
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); //expanded member, name
00270                 aFsSession.Delete(fn);
00271                 return;
00272                 }
00273         CleanupStack::PushL(fileStream);
00274 
00275         // Retrieve the size of uncompressed file contained 
00276         // in a CZipFile archive file.
00277         TUint16 size = aMember->UncompressedSize();
00278 
00279         RBuf8 bytes;
00280         bytes.CreateL(size);
00281         bytes.CleanupClosePushL();
00282 
00283         // Read the specified number of bytes of binary data 
00284         // from the file at the current position.
00285         User::LeaveIfError(fileStream->Read(bytes,size));
00286         // Write to the file
00287         User::LeaveIfError(extractedMember.Write(bytes));
00288         
00289         CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and 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         //TFileName gzipInputFileName(KInputGzipFile);
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         // Append the path to the directory where you want to 
00324         // store the uncompressed file.
00325         aFsSession.MkDirAll(outputFile);
00326         outputFile.Append(KCompressZipFile);
00327         
00328         // Create and open icon.bmp on 
00329         // c:\private\E80000B7\gzip\extracts\ folder.
00330         output.Replace(aFsSession, outputFile, EFileStream | EFileWrite | EFileShareExclusive);
00331         CleanupClosePushL(output);
00332         
00333         // icon.bmp.gz file is the input file
00334         TPtrC inputFile = gzipInputFileName;
00335         
00336         RBuf uncompressedFile;
00337         uncompressedFile.CreateL(outputFile.Length());
00338         uncompressedFile.CleanupClosePushL();
00339 
00340         _LIT(KUfl,"%S");
00341         // Format and copy the filename into the descriptor
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         // Decompress the gzip file
00350         while(def->InflateL())
00351                 {
00352                 // InflateL decompresses the file in stages. 
00353                 // It returns false when decompression is complete
00354                 }
00355         iConsole->Printf(KDecompress);
00356         
00357         CleanupStack::PopAndDestroy(4); // input, output, uncompressedFile, def
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         //TFileName inputFileToBeGzipped(KInputGzipPath);
00382 
00383         RFile input;
00384         
00385         User::LeaveIfError(input.Open(aFsSession, inputFileToBeGzipped, EFileStream | EFileRead | EFileShareAny));
00386         CleanupClosePushL(input);
00387         
00388         // Set the output path to the directory where you 
00389         // want to store the compressed file.
00390         TFileName outputFile(KCompressGzipPath);
00391         aFsSession.MkDirAll(outputFile);
00392         outputFile.Append(KExtractGzipFile);
00393         
00394         iConsole->Printf(KFileInfo, &inputFileToBeGzipped);
00395         
00396         // Prevent the length of the descriptor to which data is written,
00397         // from exceeding its maximum length by appending additional length.
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         // Format and copy the filename into the descriptor
00406         compressedFile.Format(KAppendExtension,&outputFile);
00407         
00408         CEZFileToGZip *def = CEZFileToGZip::NewLC(aFsSession, compressedFile, input);
00409         // Compress the file to gzip format
00410         while(def->DeflateL())
00411                 {
00412                 // DeflateL compresses the file in stages. 
00413                 // It returns false when compression is complete
00414                 }
00415         
00416         iConsole->Printf(KCompress);
00417 
00418         CleanupStack::PopAndDestroy(3); // def, compressedFile, input
00419 
00420         }
00421 
00422                         
00423 LOCAL_C void MainL()
00424         {
00425         // Create an Active Scheduler to handle asychronous calls
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         // Connect to file session
00434         User::LeaveIfError(fsSession.Connect());
00435         
00436         // Create the private directory on the writable drive
00437         // i.e. "\private\E80000B7\"
00438         // Note that the number E80000B7 is the process security id 
00439         // taken from the 2nd UID specified in the mmp file.
00440 
00441     fsSession.CreatePrivatePath(RFs::GetSystemDrive());
00442     // Set the session path to this private directory on the writable drive
00443     fsSession.SetSessionToPrivate(RFs::GetSystemDrive());
00444         
00445         // Get the properties of files in the zip file
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         // Compress a file to gzip format
00457         app->CompressToGzipFileL(fsSession);
00458         
00459         // Extract a gzip file
00460         app->ExtractGzipFileL(fsSession);
00461                 
00462         CleanupStack::PopAndDestroy(2); //app, scheduler
00463         // Close the file session
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         

Generated by  doxygen 1.6.2