如何读取ZIP文件
文章信息
可以通过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
查看
- .ZIP File Format Specification - specification from the creator of PKZIP, the first program that supports ZIP file.
- CZipFile - CZipFile reference.


(no comments yet)