×
Namespaces

Variants
Actions

如何读取ZIP文件

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
hoolee
最后由 hamishwillee 在 06 Aug 2012 编辑


可以通过CZipFile在Symbian OS C++中读取ZIP文件

头文件:

#include <zipfile.h>

链接库:

LIBRARY ezip.lib


在ZIP文件中获取文件信息

下列示例显示了如何遍历ZIP中所有的文件。它显示了提取的文件名,压缩的文件尺寸和解压后的文件尺寸。

// 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);
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();
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.
console->Printf(
KInfoMessage,
member->Name(),
member->CompressedSize(), member->UncompressedSize());
delete member;
}
 
// Finally, don't forget to release all the resources that we have allocated.
CleanupStack::PopAndDestroy(); // members
CleanupStack::PopAndDestroy(); // zipFile


从ZIP文件中解压文件

下列示例演示了如何从ZIP文件中解压制定文件

// Create an instance of CZipFile.
CZipFile* zipFile = CZipFile::NewL(fileSession, aCompressedFile);
CleanupStack::PushL(zipFile);
 
// Extract aFileName from the ZIP file.
CZipFileMember* member = zipFile->CaseInsensitiveMemberL(aFileName);
CleanupStack::PushL(member);
 
// 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;
zipFile->GetInputStreamL(member, 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.
HBufC8* buffer = HBufC8::NewLC(member->UncompressedSize());
TPtr8 bufferPtr(buffer->Des());
User::LeaveIfError(stream->Read(bufferPtr, member->UncompressedSize()));
 
//
// Do whatever we want with the buffer.
//
 
// Finally, do not forget to release all the allocated resources.
CleanupStack::PopAndDestroy(4); // buffer, stream, member, zipFile


查看

This page was last modified on 6 August 2012, at 09:33.
115 page views in the last 30 days.