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