os/ossrv/compressionlibs/ziplib/test/rtest/ziptest/ziptestmanual.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/ziptest/ziptestmanual.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,355 @@
     1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// ZIPTEST.CPP
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "zipfile.h"
    1.22 +
    1.23 +#include <e32cons.h>
    1.24 +#include <s32file.h>
    1.25 +#include <s32strm.h> 
    1.26 +#include <utf.h>
    1.27 +#include <e32test.h>
    1.28 +
    1.29 +/*
    1.30 +	class CZipTest - defines text UI
    1.31 +*/
    1.32 +LOCAL_D RTest test(_L("ZIPTEST"));
    1.33 +
    1.34 +class CZipTest : public CBase
    1.35 +	{
    1.36 +public:
    1.37 +	// execute UI until exit command processed
    1.38 +	void ExecuteL();
    1.39 +private:
    1.40 +	// destructor
    1.41 +	~CZipTest();
    1.42 +	void DoTestL();
    1.43 +	void ExtractFileL(RFs&,  const CZipFileMember*, CZipFile*, const TFileName*, RFileWriteStream&);
    1.44 +	void TheZipTestL(RFs&, TFileName*, RFileWriteStream&);
    1.45 +	
    1.46 +private:
    1.47 +	CConsoleBase*	iConsole;
    1.48 +	RFile			iZipOut;
    1.49 +	TBool			NoFiles;
    1.50 +	};
    1.51 +
    1.52 +_LIT(KNoFilesFound,"No appropriate files located in this folder.\r\n");
    1.53 +_LIT(KOpeningFile,"Opening file ");
    1.54 +_LIT(KDots,"...");
    1.55 +_LIT(KOutputFileName,"C:\\test\\zip\\extracts\\zip_out.txt");
    1.56 +_LIT(KErrFileNotOpened, "File could not be opened.");
    1.57 +_LIT(KOk, "Ok");
    1.58 +_LIT(KZipName,"Name:		");
    1.59 +_LIT(KZipCRC32,"CRC32:		");
    1.60 +_LIT(KZipCSize,"Compressed Size:	");
    1.61 +_LIT(KZipUCSize,"Uncompressed Size:	");
    1.62 +_LIT(KOpenedSuccessfully, "%S opened sucessfully.\n");
    1.63 +_LIT(KLineSpace, "\r\n");
    1.64 +
    1.65 +_LIT(KExtractZipPath, "C:\\test\\zip\\extracts\\");
    1.66 +
    1.67 +#if !defined(__WINS__)
    1.68 +_LIT(KPath, "Z:\\test\\zip\\");
    1.69 +#else
    1.70 +_LIT(KPath, "C:\\test\\zip\\");
    1.71 +#endif
    1.72 +
    1.73 +void CZipTest::ExecuteL()
    1.74 +	{
    1.75 +	//text constant declarations
    1.76 +	_LIT(KTxtZipTest,"Zip Test");
    1.77 +		
    1.78 +	iConsole=Console::NewL(KTxtZipTest,
    1.79 +		TSize(KConsFullScreen,KConsFullScreen));
    1.80 +	DoTestL();
    1.81 +	}
    1.82 +
    1.83 +CZipTest::~CZipTest()
    1.84 +	{
    1.85 +	delete iConsole;
    1.86 +	}
    1.87 +
    1.88 +
    1.89 +void CZipTest::DoTestL()
    1.90 +	{
    1.91 +	
    1.92 +	TFileName*				FileName;
    1.93 +	RFs						Fs;
    1.94 +	
    1.95 +	NoFiles=ETrue;
    1.96 +	
    1.97 +	User::LeaveIfError(Fs.Connect()); //Connect to file session
    1.98 +	User::LeaveIfError(Fs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
    1.99 +	CleanupClosePushL(Fs);
   1.100 +
   1.101 +	Fs.MkDirAll(KOutputFileName);
   1.102 +	iZipOut.Replace(Fs, KOutputFileName, EFileShareAny|EFileWrite);
   1.103 +	RFileWriteStream stream(iZipOut);
   1.104 +	CDir* fileList;
   1.105 +	
   1.106 +	Fs.GetDir(KPath, KEntryAttNormal, ESortByName, fileList);
   1.107 +	CleanupStack::PushL(fileList);
   1.108 +	TInt count = fileList->Count(); 
   1.109 +	for (TInt y=0; y<count; y++)
   1.110 +		{
   1.111 +
   1.112 +		NoFiles=EFalse;
   1.113 +		
   1.114 +		TBuf<256> dirFileName((*fileList)[y].iName);
   1.115 +
   1.116 +		FileName= &dirFileName;
   1.117 +		stream.WriteL(KLineSpace);
   1.118 +		stream.WriteL(KOpeningFile);
   1.119 +		stream.WriteL(*FileName);
   1.120 +		stream.WriteL(KDots);
   1.121 +		TheZipTestL(Fs, FileName, stream);
   1.122 +		}
   1.123 +	if (NoFiles) 
   1.124 +		{
   1.125 +		iConsole->Printf(KNoFilesFound);
   1.126 +		stream.WriteL(KNoFilesFound);
   1.127 +		}
   1.128 +	stream.Close();
   1.129 +	CleanupStack::PopAndDestroy(2);
   1.130 +	}
   1.131 +
   1.132 +#if defined(__GCC32__)
   1.133 +typedef wchar_t __TText;
   1.134 +#elif defined(__VC32__)
   1.135 +typedef TUint16 __TText;
   1.136 +#elif defined(__CW32__)
   1.137 +typedef TUint16 __TText;
   1.138 +#elif !defined(__TText_defined)
   1.139 +#error  no typedef for __TText
   1.140 +#endif
   1.141 +const __TText* KShortData1252  = L"\x20AC\u00FC\u00E9";
   1.142 +const __TText* KShortData850   = L"\u00C7\u00FC\u00E9";
   1.143 +const __TText* KLongData1252   = L"\u00E7\u00F8d\u00EB\u00DE\u00E3g\u00EA1252";
   1.144 +const __TText* KLongData850    = L"\u00E7\u00F6d\u00EBp\u00E4g\u00EA850";
   1.145 +const __TText* KShortWin2K1252 = L"CP1252Win2kShort.zip"; // created on win2k with winzip
   1.146 +const __TText* KShortWinXP1252 = L"CP1252WinXPShort.zip"; // created on XP with winzip
   1.147 +const __TText* KShortWin2K850  = L"CP850Win2kShort.zip";
   1.148 +const __TText* KShortWinXPMS850= L"CP850WinXPMSShort.zip";// created on XP with Compressed Folders
   1.149 +const __TText* KShortWinXP850  = L"CP850WinXPShort.zip";
   1.150 +const __TText* KLongWin2K      = L"CPMixWin2kLong.zip";
   1.151 +const __TText* KLongWinXP      = L"CPMixWinXPLong.zip";
   1.152 +const __TText* KLongWinXPMS    = L"CPMixWinXPMSLong.zip";
   1.153 +struct TUnzipResultsArray
   1.154 +	{
   1.155 +	const __TText*  iZipFileName;
   1.156 +	TInt iZipFileMemberIndex;
   1.157 +	const __TText*  iZipFileMemberExpectedName;
   1.158 +	};
   1.159 +const TUnzipResultsArray KUnzipResults[] =
   1.160 +	{
   1.161 +	{KShortWin2K1252, 1, KShortData1252},
   1.162 +	{KShortWinXP1252, 1, KShortData1252},
   1.163 +	{KShortWin2K850,  1, KShortData850},
   1.164 +	{KShortWinXPMS850,1, KShortData850},
   1.165 +	{KShortWinXP850,  1, KShortData850},
   1.166 +	{KLongWin2K,      1, KLongData1252},
   1.167 +	{KLongWin2K,      2, KLongData850},
   1.168 +	{KLongWinXP,      1, KLongData850},
   1.169 +	{KLongWinXP,      2, KLongData1252},
   1.170 +	{KLongWinXPMS,    1, KLongData850},
   1.171 +	{KLongWinXPMS,    2, KLongData1252}
   1.172 +	};
   1.173 +void TestEntry(const TDesC16& aFileName, TInt aIndex, const TDesC16& aEntryName)
   1.174 +// Test for correct decoding of filenames containing extended characters (DEF083102)	
   1.175 +// Cycle through data defined above looking for matching filenames and data
   1.176 +	{
   1.177 + 	for (TInt i = 0;
   1.178 + 		i != sizeof(KUnzipResults)/sizeof(KUnzipResults[0]); ++i)
   1.179 + 		{
   1.180 + 		TPtrC filename(reinterpret_cast<const TText16*>(
   1.181 + 			KUnzipResults[i].iZipFileName));
   1.182 + 		if(aFileName==filename && aIndex==KUnzipResults[i].iZipFileMemberIndex)
   1.183 + 			{
   1.184 +	 		TPtrC entryname(reinterpret_cast<const TText16*>(
   1.185 +	 			KUnzipResults[i].iZipFileMemberExpectedName));
   1.186 + 			test.Next(_L("DEF083102"));
   1.187 + 			test(aEntryName==entryname);
   1.188 + 			}
   1.189 + 		}
   1.190 +	}
   1.191 +	
   1.192 +void CZipTest::TheZipTestL(RFs& aFs, 
   1.193 +						  TFileName* aFileName, 
   1.194 +						  RFileWriteStream& aStream)
   1.195 +	{
   1.196 +	CZipFile*				ZipFile=0;
   1.197 +	CZipFileMember*			Member=0;
   1.198 +	CZipFileMemberIterator*	FileMembers;
   1.199 +
   1.200 +	iConsole->Printf(_L("Test file: %S\r\n"), aFileName);
   1.201 +	TRAPD(err, ZipFile = CZipFile::NewL(aFs,*aFileName));
   1.202 +	
   1.203 +	if(err==KErrNoMemory) 
   1.204 +		User::Leave(err); // leave here if error is no memory
   1.205 +	
   1.206 +	if (err!=KErrNone)
   1.207 +		{
   1.208 +		aStream.WriteL(KErrFileNotOpened);
   1.209 +		aStream.WriteL(KLineSpace);
   1.210 +	
   1.211 +		if (err == CZipArchive::KZipArchiveError)
   1.212 +			aStream.WriteL(_L("Error: Zip Archive Error.\r\n"));
   1.213 +		else if (err == CZipArchive::KCentralDirectoryTrailerNotFound)
   1.214 +			aStream.WriteL(_L("Error: Central Directory Trailer Not Found.\r\n"));
   1.215 +		else if (err == CZipArchive::KCentralDirectoryTrailerInvalid)
   1.216 +			aStream.WriteL(_L("Error: KCentral Directory Trailer Invalid.\r\n"));
   1.217 +		else if (err == CZipArchive::KCompressionMethodNotSupported)
   1.218 +			aStream.WriteL(_L("Error: Compression Method Not Supported.\r\n"));
   1.219 +		else if (err == CZipArchive::KMultiDiskArchivesNotSupported)
   1.220 +			aStream.WriteL(_L("Error: Multi Disk Archives Not Supported.\r\n"));
   1.221 +		else if (err == CZipArchive::KMemberNotFound)
   1.222 +			aStream.WriteL(_L("Error: Member Not Found.\r\n"));
   1.223 +		else if (err == CZipArchive::KZipArchiveMinError)
   1.224 +			aStream.WriteL(_L("Error: Zip Archive Min Error.\r\n"));
   1.225 +
   1.226 +		User::Leave(err);
   1.227 +		}
   1.228 +	else
   1.229 +		{
   1.230 +		CleanupStack::PushL(ZipFile);
   1.231 +		aStream.WriteL(KOk);
   1.232 +		aStream.WriteL(KLineSpace);
   1.233 +			aStream.WriteL(KLineSpace);
   1.234 +		iConsole->Printf(KOpenedSuccessfully, aFileName);
   1.235 +	
   1.236 +		NoFiles=EFalse;
   1.237 +			
   1.238 +		FileMembers = ZipFile->GetMembersL();
   1.239 +		CleanupStack::PushL(FileMembers);
   1.240 +		Member = FileMembers->NextL();
   1.241 +		TInt index=1;// have to provide this - the one in the iterator is private
   1.242 +		while (Member != NULL)
   1.243 +			{
   1.244 +			CleanupStack::PushL(Member);
   1.245 +			TBuf<16> CRC32;
   1.246 +			CRC32.NumUC((TUint)Member->CRC32(), EHex);
   1.247 +			TBuf<16> UnComp;
   1.248 +			UnComp.NumUC((TUint)Member->UncompressedSize());
   1.249 +			TBuf<16> Comp;
   1.250 +			Comp.NumUC((TUint)Member->CompressedSize());
   1.251 +			aStream.WriteL(KZipName);
   1.252 +			aStream.WriteL(*Member->Name());
   1.253 +			aStream.WriteL(KLineSpace);
   1.254 +			aStream.WriteL(KZipCRC32);
   1.255 +			aStream.WriteL(CRC32);
   1.256 +			aStream.WriteL(KLineSpace);
   1.257 +			aStream.WriteL(KZipCSize);
   1.258 +			aStream.WriteL(Comp);
   1.259 +			aStream.WriteL(KLineSpace);
   1.260 +			aStream.WriteL(KZipUCSize);
   1.261 +			aStream.WriteL(UnComp);
   1.262 +			aStream.WriteL(KLineSpace);
   1.263 +			aStream.CommitL();
   1.264 +			ExtractFileL(aFs, Member, ZipFile, aFileName, aStream);
   1.265 +			TestEntry(*aFileName,index++,*Member->Name());
   1.266 +			aFs.SetSessionPath(KPath);
   1.267 +			CleanupStack::PopAndDestroy(Member); 
   1.268 +			Member = FileMembers->NextL();
   1.269 +			}
   1.270 +		CleanupStack::PopAndDestroy(FileMembers); 
   1.271 +		}
   1.272 +	CleanupStack::PopAndDestroy(ZipFile);
   1.273 +}
   1.274 +
   1.275 +void CZipTest::ExtractFileL(RFs& aFs, 
   1.276 +							const CZipFileMember* aMember, 
   1.277 +							CZipFile* aZipFile, 
   1.278 +							const TFileName* aFileName,
   1.279 +							RFileWriteStream& aStream)
   1.280 +	{
   1.281 +	TInt loop=0;
   1.282 +	HBufC* name = aMember->Name()->AllocLC();
   1.283 +	while (loop<name->Length())
   1.284 +		{
   1.285 +		if ((*name)[loop] == '/')
   1.286 +			{
   1.287 +			name->Des()[loop] = '\\';
   1.288 +			}
   1.289 +		loop++;
   1.290 +		}
   1.291 +	
   1.292 +	TFileName fn;
   1.293 +	fn.Append(KExtractZipPath);
   1.294 +	fn.Append(*aFileName);
   1.295 +	fn.Append('\\');
   1.296 +	fn.Append(*name);
   1.297 +//	
   1.298 +	RFile expandedMember;
   1.299 +	CleanupClosePushL(expandedMember);
   1.300 +//
   1.301 +	aFs.SetSessionPath(KExtractZipPath);
   1.302 +	aFs.MkDirAll(fn);
   1.303 +	TInt retVal = expandedMember.Replace(aFs,fn, EFileShareAny|EFileWrite);
   1.304 +	if(retVal != KErrNone)
   1.305 +		{
   1.306 +		//Could be a directory name which we are planning to write to.
   1.307 +		CleanupStack::PopAndDestroy(2,name); //expanded member, name
   1.308 +		expandedMember.Close();
   1.309 +		return;
   1.310 +		}
   1.311 +
   1.312 +//
   1.313 +	RZipFileMemberReaderStream* fileStream;
   1.314 +	TInt error = aZipFile->GetInputStreamL(aMember, fileStream);
   1.315 +	if (error != KErrNone)
   1.316 +		{
   1.317 +		_LIT(KCompressionNotSupported, "Error: Compression Method Not Supported");
   1.318 +		aStream.WriteL(KCompressionNotSupported);
   1.319 +		aStream.WriteL(KLineSpace);
   1.320 +		CleanupStack::PopAndDestroy(2,name); //expanded member and name
   1.321 +		aFs.Delete(fn);
   1.322 +		return;
   1.323 +		}
   1.324 +	CleanupStack::PushL(fileStream);
   1.325 +//
   1.326 +	TUint32 size = aMember->UncompressedSize();
   1.327 +	HBufC8* bytes = HBufC8::New(size);
   1.328 +	CleanupStack::PushL(bytes);
   1.329 +	TPtr8 ptr = bytes->Des();
   1.330 +	User::LeaveIfError(fileStream->Read(ptr,size));
   1.331 +	User::LeaveIfError(expandedMember.Write(ptr));
   1.332 +	CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and name
   1.333 +	expandedMember.Close();	
   1.334 +	}
   1.335 +	
   1.336 +LOCAL_C void executeUiL()
   1.337 +	{
   1.338 +	CZipTest* ziptest=new (ELeave) CZipTest;
   1.339 +	CleanupStack::PushL(ziptest);
   1.340 +	ziptest->ExecuteL();
   1.341 +	CleanupStack::PopAndDestroy();
   1.342 +	}
   1.343 +
   1.344 +GLDEF_C TInt E32Main() // main function called by E32
   1.345 +    {
   1.346 +	test.Title();
   1.347 +	__UHEAP_MARK; // mark heap state
   1.348 +	test.Start(_L("Test ZIP"));
   1.349 +	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
   1.350 +	TRAPD(error, executeUiL()); // do most stuff under cleanup stack
   1.351 +	test(error==KErrNone);
   1.352 +	test.End();
   1.353 +	delete cleanup; // destroy clean-up stack
   1.354 +	__UHEAP_MARKEND; // check no memory leak
   1.355 +	test.Close();
   1.356 +	return 0; // and return
   1.357 +    }
   1.358 +