How to read ZIP file using Symbian C++
This code example shows how to read a zip file using the Symbian C++ CZipFile class.
Article Metadata
Code Example
Source file: Media:ExtractZip.zip
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Created: antonypr
(19 Apr 2007)
Last edited: hamishwillee
(24 Jan 2012)
Header:
#include <ZipFileMember.h>
#include <ZipFile.h>
Link against:
LIBRARY ezip.lib
Contents |
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;
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);
if (zipFile != NULL) {
CleanupStack::PushL(zipFile);
// 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) {
CleanupStack::PushL(members);
// 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
}
CleanupStack::PopAndDestroy(&fileSession);
}
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);
CleanupStack::PushL(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;
buffer.CreateL(KBufferSize);
CleanupClosePushL(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);
User::LeaveIfError(err);
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);
fileName.Copy(pathFinder.Path());
fileName.Append(*aMember.Name());
TInt err = 0;
RFile expandedMember;
CleanupClosePushL(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) {
expandedMember.Write(aData);
}
expandedMember.Close();
CleanupStack::PopAndDestroy(&expandedMember);
}
Example project
See also
- .ZIP File Format Specification - specification from the creator of PKZIP, the first program that supports ZIP file.
- /CZipFileClass - official documentation of class CZipFile.
- Other examples and tutorials in the Developer Library: Zip Compression Library


17 Sep
2009
Processing of the zip-files is a very useful feature. It could help you in many cases - especially when you need to process data from desktop PC, because very often such data could be packed in ZIP-format. Also you could store data in ZIP-format with password protection - sometimes such approach is very necessary. This article help you to understand how to retrieve information from ZIP-archive and how to extract files from it. The code snippet contains detailed comments that help you to understand all necessary aspects of working with ZIP-files with the help of class CZipFile.