Qt has basic support for compressing and uncompressing data with its qCompress and qUncompress methods. Although those methods use the same compression algorithms that are used in zip files, they do not produce proper archives. Those methods can be useful if you want to exchange some date between two Qt applications, or maybe store some compressed data that will be read only by Qt applications. In all other situations, you have to look for some library that can handle proper zip files.

In case you are working on an application targeted for Symbian phones, one of the easiest ways to work with zip files is to simply use existing APIs available on Symbian OS. This is a short blog post that will show you how to use those APIs from your Qt application.

If you decide to use Symbian C++ APIs from Qt, probably the first thing you will need is a way to convert data from Qt types to Symbian types. When working with zip files you usually have to specify a path to a zip file, or a destination directory where to unpack a file. In order to do that, you have to convert QString to TPtrC16, here is how to do that:

TPtrC16 aZipFilePath(reinterpret_cast<const TUint16*>(zipFilePath.utf16()));

TPtrC16 extends TDesC16, so you can use it if you want to call a method that takes TDesC as a parameter. If you need to convert a descriptor into a QString, you can do it like this:

QString zipFilePath((QChar *) aZipFilePath.Ptr(), aZipFilePath.Length());

To read the contents of a zip file you will need an instance of CZipFile class. Here is an example of how to create a new CZipFile instance from a file name:

RFs fileSession;
fileSession.Connect();
CleanupClosePushL(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);

After you have created a CZipFile object, you can get a list of files that are compressed inside a zip file by using GetMembersL method of that object:

CZipFileMemberIterator* members = zipFile->GetMembersL();

CZipFileMember* member;
while ((member = members->NextL()) != 0) {
  // Do something with a member
  delete member;
}

To extract a file from zip file we need to get RZipFileMemberReaderStream from the member object:

RZipFileMemberReaderStream* stream;
aZipFile.GetInputStreamL(member, stream);

Finally, in order to build Symbian code successfully we need to add required Symbian libraries to our .pro file using LIBS directive:

LIBS += -lezip -lbafl -lefsrv -leuser

Besides that, you may also get an error about an extra qualification ‘RZipFileMemberReaderStream::` on member ‘ConstructL` in file zipfilememberinputstream.h. This can be easily resolved by removing ‘RZipFileMemberReaderStream::` prefix from ConstructL method in zipfilememberinputstream.h.

I showed examples of some basic operations that can be done on zip files using CZipFile, a more complete and detailed example that uses CZipFile can be found here.

One important thing to consider before using Symbian C++ APIs is portability. Those APIs may not be the best choice if you plan to port your application to some other platform that runs Qt applications. If that is the case, a better approach would be to use a Qt library to handle zip files. On the other hand, if you are building an application only for Symbian, this is probably the easiest way to work with zip files.

0 comments