sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\misc\t_unzip.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include "unzip.h" sl@0: sl@0: RTest test(_L("T_UNZIP")); sl@0: sl@0: RFile TheInputFile; sl@0: TUint8* OutPtr; sl@0: sl@0: GLDEF_C void AcceptUnzippedBlock(TZipInfo& /*aInfo*/, TUint8*& aOutPtr, TInt aError) sl@0: { sl@0: if (aError==KErrNone) sl@0: OutPtr=aOutPtr; sl@0: } sl@0: sl@0: GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength) sl@0: { sl@0: TPtr8 ptr(aDest,0,aLength); sl@0: return TheInputFile.Read(ptr); sl@0: } sl@0: sl@0: GLDEF_C TInt UnzipComplete(TZipInfo& /*a*/, TUint8* /*aOutPtr*/, TInt /*aError*/) sl@0: { sl@0: return KErrNone; sl@0: } sl@0: sl@0: _LIT(KLitThreadName,"Unzip"); sl@0: TInt Initialise(TZipInfo& a) sl@0: { sl@0: TInt r=InitInfo(a); sl@0: if (r!=KErrNone) sl@0: return r; sl@0: a.iFileBufSize=4*a.iInBufSize; sl@0: TAny* pFileBuf=MALLOC(a.iFileBufSize); sl@0: if (!pFileBuf) sl@0: return KErrNoMemory; sl@0: a.iFileBuf=(TUint8*)pFileBuf; sl@0: RThread t; sl@0: r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a); sl@0: if (r!=KErrNone) sl@0: { sl@0: FREE(pFileBuf); sl@0: a.iFileBuf=NULL; sl@0: return r; sl@0: } sl@0: t.SetPriority(EPriorityLess); sl@0: t.Logon(a.iThreadStatus); sl@0: t.Resume(); sl@0: a.iThreadHandle=t.Handle(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: void ProcessHeader(TZipInfo& a) sl@0: { sl@0: test.Printf(_L("Flags=%d\n"),a.iFlags); sl@0: test.Printf(_L("Method=%d\n"),a.iMethod); sl@0: test.Printf(_L("Crc=%d\n"),a.iCrc); sl@0: test.Printf(_L("Compressed size=%d\n"),a.iCompressedSize); sl@0: test.Printf(_L("Uncompressed size=%d\n"),a.iUncompressedSize); sl@0: test.Printf(_L("File name %S\n"),&a.iName); sl@0: test.Printf(_L("Data offset %d\n\n"),a.iDataOffset); sl@0: sl@0: test.Next(_L("Allocate memory for unzipped file")); sl@0: a.iOutBuf=(TUint8*)User::Alloc(a.iUncompressedSize); sl@0: test(a.iOutBuf!=NULL); sl@0: test.Next(_L("Begin unzipping")); sl@0: a.iHeaderDone=2; sl@0: TRequestStatus* pS=&a.iProcessedHeader; sl@0: RThread t; sl@0: t.SetHandle(a.iThreadHandle); sl@0: t.RequestComplete(pS,0); sl@0: } sl@0: sl@0: void Cleanup(TZipInfo& a) sl@0: { sl@0: delete a.iFileBuf; sl@0: a.iFileBuf=NULL; sl@0: delete a.iOutBuf; sl@0: a.iOutBuf=NULL; sl@0: RThread& t=*(RThread*)&a.iThreadHandle; sl@0: t.Close(); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: TFileName inputFileName; sl@0: User::CommandLine(inputFileName); sl@0: test.Start(_L("Connect to file server")); sl@0: RFs fs; sl@0: TInt r=fs.Connect(); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Open file %S\n"),&inputFileName); sl@0: r=TheInputFile.Open(fs,inputFileName,EFileRead); sl@0: test(r==KErrNone); sl@0: TZipInfo z; sl@0: r=TheInputFile.Size(z.iRemain); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("File size %d\n"),z.iRemain); sl@0: sl@0: test.Next(_L("Initialise")); sl@0: r=Initialise(z); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Read header")); sl@0: TUint32 c=0; sl@0: RThread t; sl@0: t.SetHandle(z.iThreadHandle); sl@0: while (z.iRemain && z.iThreadStatus==KRequestPending) sl@0: { sl@0: TRequestStatus dummy; sl@0: TRequestStatus* pS=&dummy; sl@0: r=ReadBlockToBuffer(z); sl@0: test(r==KErrNone); sl@0: t.RequestComplete(pS,0); // same process sl@0: // test.Printf(_L(".")); sl@0: while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending) sl@0: DELAY(20000); sl@0: if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending) sl@0: { sl@0: // after reading first block, process the header sl@0: ProcessHeader(z); sl@0: c=User::NTickCount(); sl@0: } sl@0: } sl@0: sl@0: test.Next(_L("\nWait for thread to exit")); sl@0: User::WaitForRequest(z.iThreadStatus); sl@0: if (z.iRemain || t.ExitReason()!=KErrNone) sl@0: { sl@0: test.Printf(_L("Error %d\n"),t.ExitReason()); sl@0: test(0); sl@0: } sl@0: TUint c2=User::NTickCount(); sl@0: test.Printf(_L("Took %dms\n"),c2-c); sl@0: TheInputFile.Close(); sl@0: test.Getch(); sl@0: sl@0: TInt unc_size=OutPtr-z.iOutBuf; sl@0: test.Printf(_L("Recovered size %d\n"),unc_size); sl@0: test.Printf(_L("Writing to file\n")); sl@0: RFile file; sl@0: r=file.Replace(fs,z.iName,EFileWrite); sl@0: test(r==KErrNone); sl@0: TPtrC8 ptr(z.iOutBuf,unc_size); sl@0: r=file.Write(ptr); sl@0: file.Close(); sl@0: sl@0: fs.Close(); sl@0: Cleanup(z); sl@0: sl@0: return KErrNone; sl@0: } sl@0: