sl@0
|
1 |
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// ZIPTEST.CPP
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "zipfile.h"
|
sl@0
|
19 |
|
sl@0
|
20 |
#include <e32cons.h>
|
sl@0
|
21 |
#include <s32file.h>
|
sl@0
|
22 |
#include <s32strm.h>
|
sl@0
|
23 |
#include <utf.h>
|
sl@0
|
24 |
#include <e32test.h>
|
sl@0
|
25 |
|
sl@0
|
26 |
/*
|
sl@0
|
27 |
class CZipTest - defines text UI
|
sl@0
|
28 |
*/
|
sl@0
|
29 |
LOCAL_D RTest test(_L("ZIPTEST"));
|
sl@0
|
30 |
|
sl@0
|
31 |
class CZipTest : public CBase
|
sl@0
|
32 |
{
|
sl@0
|
33 |
public:
|
sl@0
|
34 |
// execute UI until exit command processed
|
sl@0
|
35 |
void ExecuteL();
|
sl@0
|
36 |
private:
|
sl@0
|
37 |
// destructor
|
sl@0
|
38 |
~CZipTest();
|
sl@0
|
39 |
void DoTestL();
|
sl@0
|
40 |
void ExtractFileL(RFs&, const CZipFileMember*, CZipFile*, const TFileName*, RFileWriteStream&);
|
sl@0
|
41 |
void TheZipTestL(RFs&, TFileName*, RFileWriteStream&);
|
sl@0
|
42 |
|
sl@0
|
43 |
private:
|
sl@0
|
44 |
CConsoleBase* iConsole;
|
sl@0
|
45 |
RFile iZipOut;
|
sl@0
|
46 |
TBool NoFiles;
|
sl@0
|
47 |
};
|
sl@0
|
48 |
|
sl@0
|
49 |
_LIT(KNoFilesFound,"No appropriate files located in this folder.\r\n");
|
sl@0
|
50 |
_LIT(KOpeningFile,"Opening file ");
|
sl@0
|
51 |
_LIT(KDots,"...");
|
sl@0
|
52 |
_LIT(KOutputFileName,"C:\\test\\zip\\extracts\\zip_out.txt");
|
sl@0
|
53 |
_LIT(KErrFileNotOpened, "File could not be opened.");
|
sl@0
|
54 |
_LIT(KOk, "Ok");
|
sl@0
|
55 |
_LIT(KZipName,"Name: ");
|
sl@0
|
56 |
_LIT(KZipCRC32,"CRC32: ");
|
sl@0
|
57 |
_LIT(KZipCSize,"Compressed Size: ");
|
sl@0
|
58 |
_LIT(KZipUCSize,"Uncompressed Size: ");
|
sl@0
|
59 |
_LIT(KOpenedSuccessfully, "%S opened sucessfully.\n");
|
sl@0
|
60 |
_LIT(KLineSpace, "\r\n");
|
sl@0
|
61 |
|
sl@0
|
62 |
_LIT(KExtractZipPath, "C:\\test\\zip\\extracts\\");
|
sl@0
|
63 |
|
sl@0
|
64 |
#if !defined(__WINS__)
|
sl@0
|
65 |
_LIT(KPath, "Z:\\test\\zip\\");
|
sl@0
|
66 |
#else
|
sl@0
|
67 |
_LIT(KPath, "C:\\test\\zip\\");
|
sl@0
|
68 |
#endif
|
sl@0
|
69 |
|
sl@0
|
70 |
void CZipTest::ExecuteL()
|
sl@0
|
71 |
{
|
sl@0
|
72 |
//text constant declarations
|
sl@0
|
73 |
_LIT(KTxtZipTest,"Zip Test");
|
sl@0
|
74 |
|
sl@0
|
75 |
iConsole=Console::NewL(KTxtZipTest,
|
sl@0
|
76 |
TSize(KConsFullScreen,KConsFullScreen));
|
sl@0
|
77 |
DoTestL();
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
CZipTest::~CZipTest()
|
sl@0
|
81 |
{
|
sl@0
|
82 |
delete iConsole;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
|
sl@0
|
86 |
void CZipTest::DoTestL()
|
sl@0
|
87 |
{
|
sl@0
|
88 |
|
sl@0
|
89 |
TFileName* FileName;
|
sl@0
|
90 |
RFs Fs;
|
sl@0
|
91 |
|
sl@0
|
92 |
NoFiles=ETrue;
|
sl@0
|
93 |
|
sl@0
|
94 |
User::LeaveIfError(Fs.Connect()); //Connect to file session
|
sl@0
|
95 |
User::LeaveIfError(Fs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
|
sl@0
|
96 |
CleanupClosePushL(Fs);
|
sl@0
|
97 |
|
sl@0
|
98 |
Fs.MkDirAll(KOutputFileName);
|
sl@0
|
99 |
iZipOut.Replace(Fs, KOutputFileName, EFileShareAny|EFileWrite);
|
sl@0
|
100 |
RFileWriteStream stream(iZipOut);
|
sl@0
|
101 |
CDir* fileList;
|
sl@0
|
102 |
|
sl@0
|
103 |
Fs.GetDir(KPath, KEntryAttNormal, ESortByName, fileList);
|
sl@0
|
104 |
CleanupStack::PushL(fileList);
|
sl@0
|
105 |
TInt count = fileList->Count();
|
sl@0
|
106 |
for (TInt y=0; y<count; y++)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
|
sl@0
|
109 |
NoFiles=EFalse;
|
sl@0
|
110 |
|
sl@0
|
111 |
TBuf<256> dirFileName((*fileList)[y].iName);
|
sl@0
|
112 |
|
sl@0
|
113 |
FileName= &dirFileName;
|
sl@0
|
114 |
stream.WriteL(KLineSpace);
|
sl@0
|
115 |
stream.WriteL(KOpeningFile);
|
sl@0
|
116 |
stream.WriteL(*FileName);
|
sl@0
|
117 |
stream.WriteL(KDots);
|
sl@0
|
118 |
TheZipTestL(Fs, FileName, stream);
|
sl@0
|
119 |
}
|
sl@0
|
120 |
if (NoFiles)
|
sl@0
|
121 |
{
|
sl@0
|
122 |
iConsole->Printf(KNoFilesFound);
|
sl@0
|
123 |
stream.WriteL(KNoFilesFound);
|
sl@0
|
124 |
}
|
sl@0
|
125 |
stream.Close();
|
sl@0
|
126 |
CleanupStack::PopAndDestroy(2);
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
#if defined(__GCC32__)
|
sl@0
|
130 |
typedef wchar_t __TText;
|
sl@0
|
131 |
#elif defined(__VC32__)
|
sl@0
|
132 |
typedef TUint16 __TText;
|
sl@0
|
133 |
#elif defined(__CW32__)
|
sl@0
|
134 |
typedef TUint16 __TText;
|
sl@0
|
135 |
#elif !defined(__TText_defined)
|
sl@0
|
136 |
#error no typedef for __TText
|
sl@0
|
137 |
#endif
|
sl@0
|
138 |
const __TText* KShortData1252 = L"\x20AC\u00FC\u00E9";
|
sl@0
|
139 |
const __TText* KShortData850 = L"\u00C7\u00FC\u00E9";
|
sl@0
|
140 |
const __TText* KLongData1252 = L"\u00E7\u00F8d\u00EB\u00DE\u00E3g\u00EA1252";
|
sl@0
|
141 |
const __TText* KLongData850 = L"\u00E7\u00F6d\u00EBp\u00E4g\u00EA850";
|
sl@0
|
142 |
const __TText* KShortWin2K1252 = L"CP1252Win2kShort.zip"; // created on win2k with winzip
|
sl@0
|
143 |
const __TText* KShortWinXP1252 = L"CP1252WinXPShort.zip"; // created on XP with winzip
|
sl@0
|
144 |
const __TText* KShortWin2K850 = L"CP850Win2kShort.zip";
|
sl@0
|
145 |
const __TText* KShortWinXPMS850= L"CP850WinXPMSShort.zip";// created on XP with Compressed Folders
|
sl@0
|
146 |
const __TText* KShortWinXP850 = L"CP850WinXPShort.zip";
|
sl@0
|
147 |
const __TText* KLongWin2K = L"CPMixWin2kLong.zip";
|
sl@0
|
148 |
const __TText* KLongWinXP = L"CPMixWinXPLong.zip";
|
sl@0
|
149 |
const __TText* KLongWinXPMS = L"CPMixWinXPMSLong.zip";
|
sl@0
|
150 |
struct TUnzipResultsArray
|
sl@0
|
151 |
{
|
sl@0
|
152 |
const __TText* iZipFileName;
|
sl@0
|
153 |
TInt iZipFileMemberIndex;
|
sl@0
|
154 |
const __TText* iZipFileMemberExpectedName;
|
sl@0
|
155 |
};
|
sl@0
|
156 |
const TUnzipResultsArray KUnzipResults[] =
|
sl@0
|
157 |
{
|
sl@0
|
158 |
{KShortWin2K1252, 1, KShortData1252},
|
sl@0
|
159 |
{KShortWinXP1252, 1, KShortData1252},
|
sl@0
|
160 |
{KShortWin2K850, 1, KShortData850},
|
sl@0
|
161 |
{KShortWinXPMS850,1, KShortData850},
|
sl@0
|
162 |
{KShortWinXP850, 1, KShortData850},
|
sl@0
|
163 |
{KLongWin2K, 1, KLongData1252},
|
sl@0
|
164 |
{KLongWin2K, 2, KLongData850},
|
sl@0
|
165 |
{KLongWinXP, 1, KLongData850},
|
sl@0
|
166 |
{KLongWinXP, 2, KLongData1252},
|
sl@0
|
167 |
{KLongWinXPMS, 1, KLongData850},
|
sl@0
|
168 |
{KLongWinXPMS, 2, KLongData1252}
|
sl@0
|
169 |
};
|
sl@0
|
170 |
void TestEntry(const TDesC16& aFileName, TInt aIndex, const TDesC16& aEntryName)
|
sl@0
|
171 |
// Test for correct decoding of filenames containing extended characters (DEF083102)
|
sl@0
|
172 |
// Cycle through data defined above looking for matching filenames and data
|
sl@0
|
173 |
{
|
sl@0
|
174 |
for (TInt i = 0;
|
sl@0
|
175 |
i != sizeof(KUnzipResults)/sizeof(KUnzipResults[0]); ++i)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
TPtrC filename(reinterpret_cast<const TText16*>(
|
sl@0
|
178 |
KUnzipResults[i].iZipFileName));
|
sl@0
|
179 |
if(aFileName==filename && aIndex==KUnzipResults[i].iZipFileMemberIndex)
|
sl@0
|
180 |
{
|
sl@0
|
181 |
TPtrC entryname(reinterpret_cast<const TText16*>(
|
sl@0
|
182 |
KUnzipResults[i].iZipFileMemberExpectedName));
|
sl@0
|
183 |
test.Next(_L("DEF083102"));
|
sl@0
|
184 |
test(aEntryName==entryname);
|
sl@0
|
185 |
}
|
sl@0
|
186 |
}
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
void CZipTest::TheZipTestL(RFs& aFs,
|
sl@0
|
190 |
TFileName* aFileName,
|
sl@0
|
191 |
RFileWriteStream& aStream)
|
sl@0
|
192 |
{
|
sl@0
|
193 |
CZipFile* ZipFile=0;
|
sl@0
|
194 |
CZipFileMember* Member=0;
|
sl@0
|
195 |
CZipFileMemberIterator* FileMembers;
|
sl@0
|
196 |
|
sl@0
|
197 |
iConsole->Printf(_L("Test file: %S\r\n"), aFileName);
|
sl@0
|
198 |
TRAPD(err, ZipFile = CZipFile::NewL(aFs,*aFileName));
|
sl@0
|
199 |
|
sl@0
|
200 |
if(err==KErrNoMemory)
|
sl@0
|
201 |
User::Leave(err); // leave here if error is no memory
|
sl@0
|
202 |
|
sl@0
|
203 |
if (err!=KErrNone)
|
sl@0
|
204 |
{
|
sl@0
|
205 |
aStream.WriteL(KErrFileNotOpened);
|
sl@0
|
206 |
aStream.WriteL(KLineSpace);
|
sl@0
|
207 |
|
sl@0
|
208 |
if (err == CZipArchive::KZipArchiveError)
|
sl@0
|
209 |
aStream.WriteL(_L("Error: Zip Archive Error.\r\n"));
|
sl@0
|
210 |
else if (err == CZipArchive::KCentralDirectoryTrailerNotFound)
|
sl@0
|
211 |
aStream.WriteL(_L("Error: Central Directory Trailer Not Found.\r\n"));
|
sl@0
|
212 |
else if (err == CZipArchive::KCentralDirectoryTrailerInvalid)
|
sl@0
|
213 |
aStream.WriteL(_L("Error: KCentral Directory Trailer Invalid.\r\n"));
|
sl@0
|
214 |
else if (err == CZipArchive::KCompressionMethodNotSupported)
|
sl@0
|
215 |
aStream.WriteL(_L("Error: Compression Method Not Supported.\r\n"));
|
sl@0
|
216 |
else if (err == CZipArchive::KMultiDiskArchivesNotSupported)
|
sl@0
|
217 |
aStream.WriteL(_L("Error: Multi Disk Archives Not Supported.\r\n"));
|
sl@0
|
218 |
else if (err == CZipArchive::KMemberNotFound)
|
sl@0
|
219 |
aStream.WriteL(_L("Error: Member Not Found.\r\n"));
|
sl@0
|
220 |
else if (err == CZipArchive::KZipArchiveMinError)
|
sl@0
|
221 |
aStream.WriteL(_L("Error: Zip Archive Min Error.\r\n"));
|
sl@0
|
222 |
|
sl@0
|
223 |
User::Leave(err);
|
sl@0
|
224 |
}
|
sl@0
|
225 |
else
|
sl@0
|
226 |
{
|
sl@0
|
227 |
CleanupStack::PushL(ZipFile);
|
sl@0
|
228 |
aStream.WriteL(KOk);
|
sl@0
|
229 |
aStream.WriteL(KLineSpace);
|
sl@0
|
230 |
aStream.WriteL(KLineSpace);
|
sl@0
|
231 |
iConsole->Printf(KOpenedSuccessfully, aFileName);
|
sl@0
|
232 |
|
sl@0
|
233 |
NoFiles=EFalse;
|
sl@0
|
234 |
|
sl@0
|
235 |
FileMembers = ZipFile->GetMembersL();
|
sl@0
|
236 |
CleanupStack::PushL(FileMembers);
|
sl@0
|
237 |
Member = FileMembers->NextL();
|
sl@0
|
238 |
TInt index=1;// have to provide this - the one in the iterator is private
|
sl@0
|
239 |
while (Member != NULL)
|
sl@0
|
240 |
{
|
sl@0
|
241 |
CleanupStack::PushL(Member);
|
sl@0
|
242 |
TBuf<16> CRC32;
|
sl@0
|
243 |
CRC32.NumUC((TUint)Member->CRC32(), EHex);
|
sl@0
|
244 |
TBuf<16> UnComp;
|
sl@0
|
245 |
UnComp.NumUC((TUint)Member->UncompressedSize());
|
sl@0
|
246 |
TBuf<16> Comp;
|
sl@0
|
247 |
Comp.NumUC((TUint)Member->CompressedSize());
|
sl@0
|
248 |
aStream.WriteL(KZipName);
|
sl@0
|
249 |
aStream.WriteL(*Member->Name());
|
sl@0
|
250 |
aStream.WriteL(KLineSpace);
|
sl@0
|
251 |
aStream.WriteL(KZipCRC32);
|
sl@0
|
252 |
aStream.WriteL(CRC32);
|
sl@0
|
253 |
aStream.WriteL(KLineSpace);
|
sl@0
|
254 |
aStream.WriteL(KZipCSize);
|
sl@0
|
255 |
aStream.WriteL(Comp);
|
sl@0
|
256 |
aStream.WriteL(KLineSpace);
|
sl@0
|
257 |
aStream.WriteL(KZipUCSize);
|
sl@0
|
258 |
aStream.WriteL(UnComp);
|
sl@0
|
259 |
aStream.WriteL(KLineSpace);
|
sl@0
|
260 |
aStream.CommitL();
|
sl@0
|
261 |
ExtractFileL(aFs, Member, ZipFile, aFileName, aStream);
|
sl@0
|
262 |
TestEntry(*aFileName,index++,*Member->Name());
|
sl@0
|
263 |
aFs.SetSessionPath(KPath);
|
sl@0
|
264 |
CleanupStack::PopAndDestroy(Member);
|
sl@0
|
265 |
Member = FileMembers->NextL();
|
sl@0
|
266 |
}
|
sl@0
|
267 |
CleanupStack::PopAndDestroy(FileMembers);
|
sl@0
|
268 |
}
|
sl@0
|
269 |
CleanupStack::PopAndDestroy(ZipFile);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
|
sl@0
|
272 |
void CZipTest::ExtractFileL(RFs& aFs,
|
sl@0
|
273 |
const CZipFileMember* aMember,
|
sl@0
|
274 |
CZipFile* aZipFile,
|
sl@0
|
275 |
const TFileName* aFileName,
|
sl@0
|
276 |
RFileWriteStream& aStream)
|
sl@0
|
277 |
{
|
sl@0
|
278 |
TInt loop=0;
|
sl@0
|
279 |
HBufC* name = aMember->Name()->AllocLC();
|
sl@0
|
280 |
while (loop<name->Length())
|
sl@0
|
281 |
{
|
sl@0
|
282 |
if ((*name)[loop] == '/')
|
sl@0
|
283 |
{
|
sl@0
|
284 |
name->Des()[loop] = '\\';
|
sl@0
|
285 |
}
|
sl@0
|
286 |
loop++;
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
TFileName fn;
|
sl@0
|
290 |
fn.Append(KExtractZipPath);
|
sl@0
|
291 |
fn.Append(*aFileName);
|
sl@0
|
292 |
fn.Append('\\');
|
sl@0
|
293 |
fn.Append(*name);
|
sl@0
|
294 |
//
|
sl@0
|
295 |
RFile expandedMember;
|
sl@0
|
296 |
CleanupClosePushL(expandedMember);
|
sl@0
|
297 |
//
|
sl@0
|
298 |
aFs.SetSessionPath(KExtractZipPath);
|
sl@0
|
299 |
aFs.MkDirAll(fn);
|
sl@0
|
300 |
TInt retVal = expandedMember.Replace(aFs,fn, EFileShareAny|EFileWrite);
|
sl@0
|
301 |
if(retVal != KErrNone)
|
sl@0
|
302 |
{
|
sl@0
|
303 |
//Could be a directory name which we are planning to write to.
|
sl@0
|
304 |
CleanupStack::PopAndDestroy(2,name); //expanded member, name
|
sl@0
|
305 |
expandedMember.Close();
|
sl@0
|
306 |
return;
|
sl@0
|
307 |
}
|
sl@0
|
308 |
|
sl@0
|
309 |
//
|
sl@0
|
310 |
RZipFileMemberReaderStream* fileStream;
|
sl@0
|
311 |
TInt error = aZipFile->GetInputStreamL(aMember, fileStream);
|
sl@0
|
312 |
if (error != KErrNone)
|
sl@0
|
313 |
{
|
sl@0
|
314 |
_LIT(KCompressionNotSupported, "Error: Compression Method Not Supported");
|
sl@0
|
315 |
aStream.WriteL(KCompressionNotSupported);
|
sl@0
|
316 |
aStream.WriteL(KLineSpace);
|
sl@0
|
317 |
CleanupStack::PopAndDestroy(2,name); //expanded member and name
|
sl@0
|
318 |
aFs.Delete(fn);
|
sl@0
|
319 |
return;
|
sl@0
|
320 |
}
|
sl@0
|
321 |
CleanupStack::PushL(fileStream);
|
sl@0
|
322 |
//
|
sl@0
|
323 |
TUint32 size = aMember->UncompressedSize();
|
sl@0
|
324 |
HBufC8* bytes = HBufC8::New(size);
|
sl@0
|
325 |
CleanupStack::PushL(bytes);
|
sl@0
|
326 |
TPtr8 ptr = bytes->Des();
|
sl@0
|
327 |
User::LeaveIfError(fileStream->Read(ptr,size));
|
sl@0
|
328 |
User::LeaveIfError(expandedMember.Write(ptr));
|
sl@0
|
329 |
CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and name
|
sl@0
|
330 |
expandedMember.Close();
|
sl@0
|
331 |
}
|
sl@0
|
332 |
|
sl@0
|
333 |
LOCAL_C void executeUiL()
|
sl@0
|
334 |
{
|
sl@0
|
335 |
CZipTest* ziptest=new (ELeave) CZipTest;
|
sl@0
|
336 |
CleanupStack::PushL(ziptest);
|
sl@0
|
337 |
ziptest->ExecuteL();
|
sl@0
|
338 |
CleanupStack::PopAndDestroy();
|
sl@0
|
339 |
}
|
sl@0
|
340 |
|
sl@0
|
341 |
GLDEF_C TInt E32Main() // main function called by E32
|
sl@0
|
342 |
{
|
sl@0
|
343 |
test.Title();
|
sl@0
|
344 |
__UHEAP_MARK; // mark heap state
|
sl@0
|
345 |
test.Start(_L("Test ZIP"));
|
sl@0
|
346 |
CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
|
sl@0
|
347 |
TRAPD(error, executeUiL()); // do most stuff under cleanup stack
|
sl@0
|
348 |
test(error==KErrNone);
|
sl@0
|
349 |
test.End();
|
sl@0
|
350 |
delete cleanup; // destroy clean-up stack
|
sl@0
|
351 |
__UHEAP_MARKEND; // check no memory leak
|
sl@0
|
352 |
test.Close();
|
sl@0
|
353 |
return 0; // and return
|
sl@0
|
354 |
}
|
sl@0
|
355 |
|