⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipexample.cpp

📁 压缩文件的读取示例源码
💻 CPP
字号:
// --------------------------------------------------------------------------
// ZipExample.cpp
//
// Copyright 2005, Antony Pranata
// http://www.antonypranata.com
//
// Example how to read ZIP file in Symbian OS.
// --------------------------------------------------------------------------

#include <e32base.h>
#include <e32std.h>
#include <e32cons.h> // Console
#include <zipfile.h> // CZipFile

//  Global Variables
LOCAL_D CConsoleBase* console;  // write all messages to this

// Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");

_LIT(KExampleZipFile,   "c:\\data\\example.zip");
_LIT(KExtractedPath,    "c:\\data\\"); // make sure to add \\ at the end
_LIT(KExtractedFile,    "example.txt");
_LIT(KInfoTitle,        "File name - Compressed size - Uncompressed Size\n");
_LIT(KInfoMessage,      "%S - %d - %d\n");
_LIT(KExtractedMessage, "%S has been extracted to %S\n");


LOCAL_C void IteratorExampleL(const TDesC& aCompressedFile)
	{
	// Connect to the file server.
	RFs fileSession;
	User::LeaveIfError(fileSession.Connect());
	
	// Create an instance of CZipFile.
	CZipFile* zipFile = CZipFile::NewL(fileSession, aCompressedFile);
	CleanupStack::PushL(zipFile);
	
	// Iterate all the files inside the .zip file and then
	// print the file name on the screen.
	CZipFileMemberIterator* members = zipFile->GetMembersL();
	CleanupStack::PushL(members);
	CZipFileMember* member;
	console->Printf(KInfoTitle);
	while ((member = members->NextL()) != 0)
		{
		console->Printf(
			KInfoMessage,
			member->Name(),
			member->CompressedSize(), member->UncompressedSize());	
		delete member;
		}

	CleanupStack::PopAndDestroy(); // members
	CleanupStack::PopAndDestroy(); // zipFile
	fileSession.Close();
	}
	
LOCAL_C void ExtractionExampleL(const TDesC& aCompressedFile, const TDesC& aPath,
								const TDesC& aFileName)
	{
	// Connect to the file server.
	RFs fileSession;
	User::LeaveIfError(fileSession.Connect());
	
	// Create an instance of CZipFile.
	CZipFile* zipFile = CZipFile::NewL(fileSession, aCompressedFile);
	CleanupStack::PushL(zipFile);
	
	// Get the input stream of aFileName.
	CZipFileMember* member = zipFile->CaseInsensitiveMemberL(aFileName);
	CleanupStack::PushL(member);
	RZipFileMemberReaderStream* stream;
	zipFile->GetInputStreamL(member, stream);
	CleanupStack::PushL(stream);
	
	// Extracts aFileName to a buffer.
	// If the file is quite huge, then read the file in streaming mode.
	// For example, use 4KB buffer and read it in an active object.
	HBufC8* buffer = HBufC8::NewLC(member->UncompressedSize());
	TPtr8 bufferPtr(buffer->Des());
	User::LeaveIfError(stream->Read(bufferPtr, member->UncompressedSize()));

	// Store the buffer to a file.
	// It saves the file to KExtractedPath directory, the file name is the same
	// as the one in the .zip file.
	TFileName fileName;
	fileName.Append(aPath);
	fileName.Append(aFileName);
	RFile file;
	User::LeaveIfError(file.Replace(fileSession, fileName, EFileWrite));
	CleanupClosePushL(file);
	User::LeaveIfError(file.Write(*buffer));
	
	// Print message that aFileName has been extracted.
	console->Printf(KExtractedMessage, member->Name(), &fileName);

	// Release all the resources.	
	CleanupStack::PopAndDestroy(5); // file, buffer, stream, member, zipFile
	fileSession.Close();
	}

//  Local Functions
LOCAL_C void MainL(const TDesC& /*aArgs*/)
	{
	IteratorExampleL(KExampleZipFile);
	ExtractionExampleL(KExampleZipFile, KExtractedPath, KExtractedFile);
	}

LOCAL_C void DoStartL()
	{
	// Create active scheduler (to run active objects)
	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
	CleanupStack::PushL(scheduler);
	CActiveScheduler::Install(scheduler);

	// Call main function with command line
	TBuf<256> cmdLine;
	RProcess().CommandLine(cmdLine);
	MainL(cmdLine);

	// Delete active scheduler
	CleanupStack::PopAndDestroy(scheduler);
	}


//  Global Functions
GLDEF_C TInt E32Main()
	{
	// Create cleanup stack
	__UHEAP_MARK;
	CTrapCleanup* cleanup = CTrapCleanup::New();

	// Create output console
	TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
	if (createError)
		return createError;

	// Run application code inside TRAP harness, wait keypress when terminated
	TRAPD(mainError, DoStartL());
	if (mainError)
		console->Printf(KTextFailed, mainError);
	console->Printf(KTextPressAnyKey);
	console->Getch();
    
	delete console;
	delete cleanup;
	__UHEAP_MARKEND;
	return KErrNone;
	}

// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -