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 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "eustd.h"
|
sl@0
|
17 |
#include <ezcompressor.h>
|
sl@0
|
18 |
#include <ezdecompressor.h>
|
sl@0
|
19 |
#include <ezlib.h>
|
sl@0
|
20 |
#include <ezgzip.h>
|
sl@0
|
21 |
|
sl@0
|
22 |
#include <f32file.h>
|
sl@0
|
23 |
|
sl@0
|
24 |
void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname);
|
sl@0
|
25 |
|
sl@0
|
26 |
/**
|
sl@0
|
27 |
@SYMTestCaseID SYSLIB-EZLIB-CT-0829
|
sl@0
|
28 |
@SYMTestCaseDesc Gzip functionality test
|
sl@0
|
29 |
@SYMTestPriority High
|
sl@0
|
30 |
@SYMTestActions Decompress and compress a zip file read from the command line.
|
sl@0
|
31 |
@SYMTestExpectedResults Test must not fail
|
sl@0
|
32 |
@SYMREQ REQ0000
|
sl@0
|
33 |
*/
|
sl@0
|
34 |
|
sl@0
|
35 |
LOCAL_C void doExampleL()
|
sl@0
|
36 |
{
|
sl@0
|
37 |
RFs rfs;
|
sl@0
|
38 |
rfs.Connect();
|
sl@0
|
39 |
TBool compress = ETrue;
|
sl@0
|
40 |
TInt bufferSize = 0x8000;
|
sl@0
|
41 |
TBool readHeader = EFalse;
|
sl@0
|
42 |
TBool readTrailer = EFalse;
|
sl@0
|
43 |
TBool noWork = EFalse;
|
sl@0
|
44 |
|
sl@0
|
45 |
TInt cmdLineLen = User::CommandLineLength();
|
sl@0
|
46 |
|
sl@0
|
47 |
if (cmdLineLen <= 0)
|
sl@0
|
48 |
{
|
sl@0
|
49 |
_LIT(KUsage,"Usage:gzip [-dht] [-u bufferSize] filename\n");
|
sl@0
|
50 |
console->Printf(KUsage);
|
sl@0
|
51 |
User::Leave(1);
|
sl@0
|
52 |
}
|
sl@0
|
53 |
//(cmdLineLen > 0) case
|
sl@0
|
54 |
HBufC *argv = HBufC::NewLC(cmdLineLen);
|
sl@0
|
55 |
TPtr cmd(argv->Des());
|
sl@0
|
56 |
User::CommandLine(cmd);
|
sl@0
|
57 |
|
sl@0
|
58 |
TLex arguments(*argv);
|
sl@0
|
59 |
|
sl@0
|
60 |
TPtrC options(arguments.NextToken());
|
sl@0
|
61 |
TBool expectBufferSize = EFalse;
|
sl@0
|
62 |
_LIT(KBadBufferSize,"Bad buffersize specified\n");
|
sl@0
|
63 |
_LIT(KUnknownOption,"Unknown Options %S\n");
|
sl@0
|
64 |
|
sl@0
|
65 |
|
sl@0
|
66 |
while (options[0]=='-' || expectBufferSize)
|
sl@0
|
67 |
{
|
sl@0
|
68 |
TInt i = 1;
|
sl@0
|
69 |
|
sl@0
|
70 |
if (expectBufferSize)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
expectBufferSize = EFalse;
|
sl@0
|
73 |
if (options.Length() == 0)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
console->Printf(KBadBufferSize);
|
sl@0
|
76 |
User::Leave(1);
|
sl@0
|
77 |
}
|
sl@0
|
78 |
else
|
sl@0
|
79 |
{
|
sl@0
|
80 |
TLex bufLex(options);
|
sl@0
|
81 |
if (bufLex.Val(bufferSize) != KErrNone)
|
sl@0
|
82 |
{
|
sl@0
|
83 |
console->Printf(KBadBufferSize);
|
sl@0
|
84 |
User::Leave(1);
|
sl@0
|
85 |
}
|
sl@0
|
86 |
}
|
sl@0
|
87 |
}
|
sl@0
|
88 |
else
|
sl@0
|
89 |
{
|
sl@0
|
90 |
|
sl@0
|
91 |
while (i < options.Length())
|
sl@0
|
92 |
{
|
sl@0
|
93 |
if (options[i] == 'd')
|
sl@0
|
94 |
compress = EFalse;
|
sl@0
|
95 |
else if (options[i] == 'b')
|
sl@0
|
96 |
{
|
sl@0
|
97 |
if (i + 1 < options.Length())
|
sl@0
|
98 |
{
|
sl@0
|
99 |
TLex bufLex(options.Right(options.Length() - (i + 1)));
|
sl@0
|
100 |
if (bufLex.Val(bufferSize) != KErrNone)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
console->Printf(KBadBufferSize);
|
sl@0
|
103 |
User::Leave(1);
|
sl@0
|
104 |
}
|
sl@0
|
105 |
}
|
sl@0
|
106 |
else
|
sl@0
|
107 |
expectBufferSize = ETrue;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
else if (options[i] == 'h')
|
sl@0
|
110 |
readHeader = noWork = ETrue;
|
sl@0
|
111 |
else if (options[i] == 't')
|
sl@0
|
112 |
readTrailer = noWork = ETrue;
|
sl@0
|
113 |
else
|
sl@0
|
114 |
{
|
sl@0
|
115 |
console->Printf(KUnknownOption,&options);
|
sl@0
|
116 |
i = options.Length();
|
sl@0
|
117 |
}
|
sl@0
|
118 |
i++;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
if (i == 1)
|
sl@0
|
122 |
{
|
sl@0
|
123 |
_LIT(KNoOption,"No option specified\n");
|
sl@0
|
124 |
console->Printf(KNoOption);
|
sl@0
|
125 |
User::Leave(1);
|
sl@0
|
126 |
}
|
sl@0
|
127 |
}
|
sl@0
|
128 |
options.Set(arguments.NextToken());
|
sl@0
|
129 |
}
|
sl@0
|
130 |
|
sl@0
|
131 |
console->Printf(_L("Buffer Size %d\n"),bufferSize);
|
sl@0
|
132 |
|
sl@0
|
133 |
if (readHeader)
|
sl@0
|
134 |
{
|
sl@0
|
135 |
ReadAndPrintHeaderL(rfs,options);
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
if (readTrailer)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
TEZGZipTrailer trailer;
|
sl@0
|
141 |
EZGZipFile::LocateAndReadTrailerL(rfs, options, trailer);
|
sl@0
|
142 |
_LIT(KTrailer,"Crc = %d Size = %d\n");
|
sl@0
|
143 |
console->Printf(KTrailer,trailer.iCrc32,trailer.iSize);
|
sl@0
|
144 |
}
|
sl@0
|
145 |
|
sl@0
|
146 |
if (!noWork)
|
sl@0
|
147 |
{
|
sl@0
|
148 |
if (!compress)
|
sl@0
|
149 |
{
|
sl@0
|
150 |
|
sl@0
|
151 |
TPtrC inputFile(options);
|
sl@0
|
152 |
|
sl@0
|
153 |
HBufC *uncompressedFile = HBufC::NewLC(inputFile.Length()+1);
|
sl@0
|
154 |
_LIT(KUfl,"%S1");
|
sl@0
|
155 |
uncompressedFile->Des().Format(KUfl,&inputFile);
|
sl@0
|
156 |
|
sl@0
|
157 |
RFile output;
|
sl@0
|
158 |
TInt err;
|
sl@0
|
159 |
|
sl@0
|
160 |
_LIT(KInfo,"Decompressing file %S\n");
|
sl@0
|
161 |
console->Printf(KInfo,&inputFile);
|
sl@0
|
162 |
|
sl@0
|
163 |
err = output.Create(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive);
|
sl@0
|
164 |
if (err == KErrAlreadyExists)
|
sl@0
|
165 |
User::LeaveIfError(output.Open(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive));
|
sl@0
|
166 |
else
|
sl@0
|
167 |
User::LeaveIfError(err);
|
sl@0
|
168 |
CleanupClosePushL(output);
|
sl@0
|
169 |
|
sl@0
|
170 |
CEZGZipToFile *def = CEZGZipToFile::NewLC(rfs,inputFile,output,bufferSize);
|
sl@0
|
171 |
while (def->InflateL()){/*do nothing*/}
|
sl@0
|
172 |
|
sl@0
|
173 |
_LIT(KHoorah,"Hoorah");
|
sl@0
|
174 |
console->Printf(KHoorah);
|
sl@0
|
175 |
|
sl@0
|
176 |
CleanupStack::PopAndDestroy(3);
|
sl@0
|
177 |
}
|
sl@0
|
178 |
else
|
sl@0
|
179 |
{
|
sl@0
|
180 |
TPtrC inputFile(options);
|
sl@0
|
181 |
|
sl@0
|
182 |
HBufC *compressedFile = HBufC::NewLC(inputFile.Length()+3);
|
sl@0
|
183 |
_LIT(KUfl,"%S.gz");
|
sl@0
|
184 |
compressedFile->Des().Format(KUfl,&inputFile);
|
sl@0
|
185 |
|
sl@0
|
186 |
RFile input;
|
sl@0
|
187 |
|
sl@0
|
188 |
_LIT(KInfo,"Compressing file %S to %S\n");
|
sl@0
|
189 |
console->Printf(KInfo,&inputFile,compressedFile);
|
sl@0
|
190 |
|
sl@0
|
191 |
User::LeaveIfError(input.Open(rfs,inputFile,EFileStream | EFileRead | EFileShareAny));
|
sl@0
|
192 |
CleanupClosePushL(input);
|
sl@0
|
193 |
|
sl@0
|
194 |
CEZFileToGZip *com = CEZFileToGZip::NewLC(rfs,*compressedFile,input,bufferSize);
|
sl@0
|
195 |
while (com->DeflateL()){/*do nothing*/}
|
sl@0
|
196 |
|
sl@0
|
197 |
_LIT(KHoorah,"Hoorah");
|
sl@0
|
198 |
console->Printf(KHoorah);
|
sl@0
|
199 |
|
sl@0
|
200 |
CleanupStack::PopAndDestroy(3);
|
sl@0
|
201 |
}
|
sl@0
|
202 |
}
|
sl@0
|
203 |
CleanupStack::PopAndDestroy(1);
|
sl@0
|
204 |
rfs.Close();
|
sl@0
|
205 |
}
|
sl@0
|
206 |
|
sl@0
|
207 |
|
sl@0
|
208 |
void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname)
|
sl@0
|
209 |
{
|
sl@0
|
210 |
TEZGZipHeader header;
|
sl@0
|
211 |
|
sl@0
|
212 |
if (!EZGZipFile::IsGzipFileL(rfs,fname))
|
sl@0
|
213 |
{
|
sl@0
|
214 |
_LIT(KNotGzipFile,"%S is not a gzip file\n");
|
sl@0
|
215 |
console->Printf(KNotGzipFile,&fname);
|
sl@0
|
216 |
User::Leave(1);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
RFile gzipFile;
|
sl@0
|
219 |
User::LeaveIfError(gzipFile.Open(rfs,fname,EFileStream | EFileRead | EFileShareAny));
|
sl@0
|
220 |
EZGZipFile::ReadHeaderL(gzipFile,header);
|
sl@0
|
221 |
|
sl@0
|
222 |
_LIT(KFileIds,"ID1 = %d ID2 = %d\n");
|
sl@0
|
223 |
console->Printf(KFileIds,header.iId1,header.iId2);
|
sl@0
|
224 |
_LIT(KCompressionMethod,"Compression Method = %d\n");
|
sl@0
|
225 |
console->Printf(KCompressionMethod,header.iCompressionMethod);
|
sl@0
|
226 |
_LIT(KFlags,"Flags = %d\n");
|
sl@0
|
227 |
console->Printf(KFlags,header.iFlags);
|
sl@0
|
228 |
_LIT(KTime,"Time Stamp = %d\n");
|
sl@0
|
229 |
console->Printf(KTime,header.iTime);
|
sl@0
|
230 |
_LIT(KExtraFlags,"Extra Flags %d\n");
|
sl@0
|
231 |
console->Printf(KExtraFlags,header.iExtraFlags);
|
sl@0
|
232 |
_LIT(KOS,"OS %d\n");
|
sl@0
|
233 |
console->Printf(KOS,header.iOs);
|
sl@0
|
234 |
if (header.iFlags & 4)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
_LIT(KExtraLen,"Extra Length %d\n");
|
sl@0
|
237 |
console->Printf(KExtraLen,header.iXlen);
|
sl@0
|
238 |
HBufC *buf = HBufC::NewMaxLC(header.iExtra->Length());
|
sl@0
|
239 |
buf->Des().Copy(*header.iExtra);
|
sl@0
|
240 |
console->Printf(*buf);
|
sl@0
|
241 |
CleanupStack::PopAndDestroy();
|
sl@0
|
242 |
}
|
sl@0
|
243 |
|
sl@0
|
244 |
if (header.iFlags & 8)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
_LIT(KName,"Name: %S\n");
|
sl@0
|
247 |
HBufC *buf = HBufC::NewMaxLC(header.iFname->Length());
|
sl@0
|
248 |
buf->Des().Copy(*header.iFname);
|
sl@0
|
249 |
console->Printf(KName,buf);
|
sl@0
|
250 |
CleanupStack::PopAndDestroy();
|
sl@0
|
251 |
}
|
sl@0
|
252 |
|
sl@0
|
253 |
if (header.iFlags & 16)
|
sl@0
|
254 |
{
|
sl@0
|
255 |
_LIT(KComment,"Comment: %S\n");
|
sl@0
|
256 |
HBufC *buf = HBufC::NewMaxLC(header.iComment->Length());
|
sl@0
|
257 |
buf->Des().Copy(*header.iComment);
|
sl@0
|
258 |
console->Printf(KComment,buf);
|
sl@0
|
259 |
CleanupStack::PopAndDestroy();
|
sl@0
|
260 |
}
|
sl@0
|
261 |
|
sl@0
|
262 |
if (header.iFlags & 2)
|
sl@0
|
263 |
{
|
sl@0
|
264 |
_LIT(KCrc,"Crc16 = %d\n");
|
sl@0
|
265 |
console->Printf(KCrc,header.iCrc);
|
sl@0
|
266 |
}
|
sl@0
|
267 |
}
|