×
Namespaces

Variants
Actions
(Redirected from How to read ZIP file)

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
Article
Created: antonypr (19 Apr 2007)
Last edited: hamishwillee (18 Sep 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

File:ExtractZip.zip

See also

This page was last modified on 18 September 2012, at 08:56.
102 page views in the last 30 days.
×