How to read ZIP file using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This code example shows how to read a zip file using the Symbian C++ CZipFile class.

Article Metadata
Code ExamplePlatform Security
Signing Required: Self-Signed
Capabilities: None
Created: antonypr (19 Apr 2007)
Last edited: hamishwillee (18 Sep 2012)


#include <ZipFileMember.h>
#include <ZipFile.h>

Link against:

LIBRARY ezip.lib


Retrieving files information in a ZIP file

The following example shows how to iterate all files inside a ZIP file. It demonstrates how to read the extracted file name, compressed file size, uncompressed file size and extracts zip file.

void ExtractZip(const TDesC& aCompressedFile) {
RFs fileSession;
// Creating a new instance of CZipFile.
// The first parameter of the two-phase constructor is a session of File Server.
// The second parameter is the file name of the ZIP file.
CZipFile* zipFile = CZipFile::NewL(fileSession, aCompressedFile);
if (zipFile != NULL) {
// Getting the list of all files inside the ZIP.
// The ownership of CZipFileMemberIterator will be passed to the caller,
// thus we have to delete it after we are done reading the ZIP.
CZipFileMemberIterator* members = zipFile->GetMembersL();
if (members != NULL) {
// Iterating through all the files.
// We have to call CZipFileMemberIterator::NextL() over and over
// until it returns 0.
CZipFileMember* member;
while ((member = members->NextL()) != 0) {
// Print the name of the extracted file, the compressed file size
// and the uncompressed file size.
const TDesC* fileName = member->Name();
TInt compressedSize = member->CompressedSize();
TInt uncompressedSize = member->UncompressedSize();
if (fileName != NULL) {
ExtractFileFromZip(fileSession, aCompressedFile, *zipFile, *member);
delete member;
CleanupStack::PopAndDestroy(); // members
CleanupStack::PopAndDestroy(); // zipFile

Extracting a file from a ZIP file

The following example shows how to extract a specific file from a ZIP file.

void ExtractFileFromZip(RFs& aFs, const TDesC& aZipFileName, CZipFile& aZipFile, 
CZipFileMember& aMember) {
// Use input stream to extract the file.
// The input stream of a file inside ZIP file is RZipFileMemberReaderStream.
// The method used to get the input stream is CZipFile::GetInputStreamL().
RZipFileMemberReaderStream* stream;
aZipFile.GetInputStreamL(&aMember, stream);
// Read the file using input stream.
// Before reading the file, the code allocates a buffer to store with
// the size of member->UncompressesedSize().
// If the file is quite huge, do not use "one-shot" Read().
// Instead, read using a small block of buffer and do it inside an
// active object.
const TInt KBufferSize = 1024;
TInt uncompressedSize = aMember.UncompressedSize();
RBuf8 buffer;
TInt readBytes = 0;
TBool createNewFile;
while (readBytes < uncompressedSize) {
TInt toRead = (uncompressedSize - readBytes) > buffer.MaxSize() ? buffer.MaxSize() : (uncompressedSize - readBytes);
TInt err = stream->Read(buffer, toRead);
if (readBytes == 0) {
createNewFile = ETrue;
else {
createNewFile = EFalse;
WriteToFile(aFs, aZipFileName, createNewFile, buffer, aMember);
readBytes += buffer.Length();
// Finally, do not forget to release all the allocated resources.
CleanupStack::PopAndDestroy(2, stream); // stream, expandedMember
void WriteToFile(RFs& aFs, const TDesC& aZipFileName, TBool aCreateNewFile,
const TDesC8& aData, CZipFileMember& aMember) {
TFileName fileName;
TParsePtrC pathFinder(aZipFileName);
TInt err = 0;
RFile expandedMember;
if (!BaflUtils::FileExists(aFs, fileName)) {
err = expandedMember.Replace(aFs, fileName, EFileShareAny|EFileWrite);
else if (aCreateNewFile) {
err = expandedMember.Replace(aFs, fileName, EFileShareAny|EFileWrite);
else {
TInt pos = 0;
err = expandedMember.Open(aFs, fileName, EFileShareAny|EFileWrite);
err = expandedMember.Seek(ESeekEnd, pos);
if (err == KErrNone) {

Example project


See also

This page was last modified on 18 September 2012, at 05:56.
57 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.