Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include <ezcompressor.h>
18 #include <ezdecompressor.h>
24 void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname);
27 @SYMTestCaseID SYSLIB-EZLIB-CT-0829
28 @SYMTestCaseDesc Gzip functionality test
30 @SYMTestActions Decompress and compress a zip file read from the command line.
31 @SYMTestExpectedResults Test must not fail
35 LOCAL_C void doExampleL()
39 TBool compress = ETrue;
40 TInt bufferSize = 0x8000;
41 TBool readHeader = EFalse;
42 TBool readTrailer = EFalse;
43 TBool noWork = EFalse;
45 TInt cmdLineLen = User::CommandLineLength();
49 _LIT(KUsage,"Usage:gzip [-dht] [-u bufferSize] filename\n");
50 console->Printf(KUsage);
53 //(cmdLineLen > 0) case
54 HBufC *argv = HBufC::NewLC(cmdLineLen);
55 TPtr cmd(argv->Des());
56 User::CommandLine(cmd);
58 TLex arguments(*argv);
60 TPtrC options(arguments.NextToken());
61 TBool expectBufferSize = EFalse;
62 _LIT(KBadBufferSize,"Bad buffersize specified\n");
63 _LIT(KUnknownOption,"Unknown Options %S\n");
66 while (options[0]=='-' || expectBufferSize)
72 expectBufferSize = EFalse;
73 if (options.Length() == 0)
75 console->Printf(KBadBufferSize);
81 if (bufLex.Val(bufferSize) != KErrNone)
83 console->Printf(KBadBufferSize);
91 while (i < options.Length())
93 if (options[i] == 'd')
95 else if (options[i] == 'b')
97 if (i + 1 < options.Length())
99 TLex bufLex(options.Right(options.Length() - (i + 1)));
100 if (bufLex.Val(bufferSize) != KErrNone)
102 console->Printf(KBadBufferSize);
107 expectBufferSize = ETrue;
109 else if (options[i] == 'h')
110 readHeader = noWork = ETrue;
111 else if (options[i] == 't')
112 readTrailer = noWork = ETrue;
115 console->Printf(KUnknownOption,&options);
116 i = options.Length();
123 _LIT(KNoOption,"No option specified\n");
124 console->Printf(KNoOption);
128 options.Set(arguments.NextToken());
131 console->Printf(_L("Buffer Size %d\n"),bufferSize);
135 ReadAndPrintHeaderL(rfs,options);
140 TEZGZipTrailer trailer;
141 EZGZipFile::LocateAndReadTrailerL(rfs, options, trailer);
142 _LIT(KTrailer,"Crc = %d Size = %d\n");
143 console->Printf(KTrailer,trailer.iCrc32,trailer.iSize);
151 TPtrC inputFile(options);
153 HBufC *uncompressedFile = HBufC::NewLC(inputFile.Length()+1);
155 uncompressedFile->Des().Format(KUfl,&inputFile);
160 _LIT(KInfo,"Decompressing file %S\n");
161 console->Printf(KInfo,&inputFile);
163 err = output.Create(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive);
164 if (err == KErrAlreadyExists)
165 User::LeaveIfError(output.Open(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive));
167 User::LeaveIfError(err);
168 CleanupClosePushL(output);
170 CEZGZipToFile *def = CEZGZipToFile::NewLC(rfs,inputFile,output,bufferSize);
171 while (def->InflateL()){/*do nothing*/}
173 _LIT(KHoorah,"Hoorah");
174 console->Printf(KHoorah);
176 CleanupStack::PopAndDestroy(3);
180 TPtrC inputFile(options);
182 HBufC *compressedFile = HBufC::NewLC(inputFile.Length()+3);
184 compressedFile->Des().Format(KUfl,&inputFile);
188 _LIT(KInfo,"Compressing file %S to %S\n");
189 console->Printf(KInfo,&inputFile,compressedFile);
191 User::LeaveIfError(input.Open(rfs,inputFile,EFileStream | EFileRead | EFileShareAny));
192 CleanupClosePushL(input);
194 CEZFileToGZip *com = CEZFileToGZip::NewLC(rfs,*compressedFile,input,bufferSize);
195 while (com->DeflateL()){/*do nothing*/}
197 _LIT(KHoorah,"Hoorah");
198 console->Printf(KHoorah);
200 CleanupStack::PopAndDestroy(3);
203 CleanupStack::PopAndDestroy(1);
208 void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname)
210 TEZGZipHeader header;
212 if (!EZGZipFile::IsGzipFileL(rfs,fname))
214 _LIT(KNotGzipFile,"%S is not a gzip file\n");
215 console->Printf(KNotGzipFile,&fname);
219 User::LeaveIfError(gzipFile.Open(rfs,fname,EFileStream | EFileRead | EFileShareAny));
220 EZGZipFile::ReadHeaderL(gzipFile,header);
222 _LIT(KFileIds,"ID1 = %d ID2 = %d\n");
223 console->Printf(KFileIds,header.iId1,header.iId2);
224 _LIT(KCompressionMethod,"Compression Method = %d\n");
225 console->Printf(KCompressionMethod,header.iCompressionMethod);
226 _LIT(KFlags,"Flags = %d\n");
227 console->Printf(KFlags,header.iFlags);
228 _LIT(KTime,"Time Stamp = %d\n");
229 console->Printf(KTime,header.iTime);
230 _LIT(KExtraFlags,"Extra Flags %d\n");
231 console->Printf(KExtraFlags,header.iExtraFlags);
233 console->Printf(KOS,header.iOs);
234 if (header.iFlags & 4)
236 _LIT(KExtraLen,"Extra Length %d\n");
237 console->Printf(KExtraLen,header.iXlen);
238 HBufC *buf = HBufC::NewMaxLC(header.iExtra->Length());
239 buf->Des().Copy(*header.iExtra);
240 console->Printf(*buf);
241 CleanupStack::PopAndDestroy();
244 if (header.iFlags & 8)
246 _LIT(KName,"Name: %S\n");
247 HBufC *buf = HBufC::NewMaxLC(header.iFname->Length());
248 buf->Des().Copy(*header.iFname);
249 console->Printf(KName,buf);
250 CleanupStack::PopAndDestroy();
253 if (header.iFlags & 16)
255 _LIT(KComment,"Comment: %S\n");
256 HBufC *buf = HBufC::NewMaxLC(header.iComment->Length());
257 buf->Des().Copy(*header.iComment);
258 console->Printf(KComment,buf);
259 CleanupStack::PopAndDestroy();
262 if (header.iFlags & 2)
264 _LIT(KCrc,"Crc16 = %d\n");
265 console->Printf(KCrc,header.iCrc);