|
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 // |
|
15 |
|
16 #include "OldEZGzip.h" |
|
17 |
|
18 using namespace TOLDEZLIB; |
|
19 |
|
20 const TUint8 EZGZipFile::ID1 = 31; |
|
21 const TUint8 EZGZipFile::ID2 = 139; |
|
22 |
|
23 /** |
|
24 Constructor |
|
25 */ |
|
26 EXPORT_C TEZGZipHeader::TEZGZipHeader() : iId1(EZGZipFile::ID1), iId2(EZGZipFile::ID2), iCompressionMethod(8), iFlags(0), iTime(0), |
|
27 iExtraFlags(0), iOs(255), iXlen(0), iExtra(NULL), iFname(NULL), iComment(NULL), iCrc(0) |
|
28 { |
|
29 |
|
30 } |
|
31 |
|
32 /** |
|
33 Destructor |
|
34 */ |
|
35 EXPORT_C TEZGZipHeader::~TEZGZipHeader() |
|
36 { |
|
37 delete iExtra; |
|
38 delete iFname; |
|
39 delete iComment; |
|
40 } |
|
41 |
|
42 /** |
|
43 Constructor |
|
44 */ |
|
45 EXPORT_C TEZGZipTrailer::TEZGZipTrailer() : iCrc32(0), iSize(0) |
|
46 { |
|
47 |
|
48 } |
|
49 |
|
50 /** |
|
51 Constructor |
|
52 |
|
53 @param aCrc the CRC to use for archive checking |
|
54 @param aSize the size of the trailer |
|
55 */ |
|
56 EXPORT_C TEZGZipTrailer::TEZGZipTrailer(TInt32 aCrc, TInt32 aSize) : iCrc32(aCrc), iSize(aSize) |
|
57 { |
|
58 |
|
59 } |
|
60 |
|
61 //-------------------------------------------------------------------------------------------------------- |
|
62 |
|
63 /** |
|
64 Read the zip header from the specified zip file into the TEZGZipHeader object |
|
65 |
|
66 @param aFile the zip file to read from |
|
67 @param aHeader the target header object |
|
68 @leave KEZlibErrBadGZipHeader invalid zip header |
|
69 @leave ... Any of the system wide error codes |
|
70 */ |
|
71 EXPORT_C void EZGZipFile::ReadHeaderL(RFile &aFile, TEZGZipHeader &aHeader) |
|
72 { |
|
73 TInt obligatoryData = sizeof(aHeader.iId1) + sizeof(aHeader.iId2) + sizeof(aHeader.iCompressionMethod) + |
|
74 sizeof(aHeader.iFlags) + sizeof(aHeader.iTime) + sizeof(aHeader.iExtraFlags) + sizeof(aHeader.iOs); |
|
75 |
|
76 TPtr8 des(&aHeader.iId1,0,obligatoryData); |
|
77 TInt err = aFile.Read(des); |
|
78 if (err != KErrNone || (des.Size() != obligatoryData)) |
|
79 User::Leave(KEZlibErrBadGZipHeader); |
|
80 |
|
81 if (aHeader.iId1 != ID1 || aHeader.iId2 != ID2) |
|
82 User::Leave(KEZlibErrBadGZipHeader); |
|
83 |
|
84 if (aHeader.iFlags & (1 << EFExtra)) // then the extra bit is set |
|
85 { |
|
86 des.Set(REINTERPRET_CAST(TUint8 *,&aHeader.iXlen),0,sizeof(aHeader.iXlen)); |
|
87 err = aFile.Read(des); |
|
88 if (err != KErrNone || des.Size() != sizeof(aHeader.iXlen) || aHeader.iXlen < 0) |
|
89 User::Leave(KEZlibErrBadGZipHeader); |
|
90 |
|
91 aHeader.iExtra = HBufC8::NewMaxL(aHeader.iXlen); |
|
92 TPtr8 des = aHeader.iExtra->Des(); |
|
93 err = aFile.Read(des); |
|
94 if (err != KErrNone || des.Size() != aHeader.iXlen) |
|
95 User::Leave(KEZlibErrBadGZipHeader); |
|
96 } |
|
97 |
|
98 if (aHeader.iFlags & (1 << EFName)) // then read in filename |
|
99 ReadStringIntoDescriptorL(aFile,&aHeader.iFname); |
|
100 |
|
101 if (aHeader.iFlags & (1 << EFComment)) // then read in comment |
|
102 ReadStringIntoDescriptorL(aFile,&aHeader.iComment); |
|
103 |
|
104 if (aHeader.iFlags & (1 << EFHcrc)) |
|
105 { |
|
106 des.Set(REINTERPRET_CAST(TUint8*,&aHeader.iCrc),0,sizeof(aHeader.iCrc)); |
|
107 err = aFile.Read(des); |
|
108 if (err != KErrNone || des.Size() != sizeof(aHeader.iCrc)) |
|
109 User::Leave(KEZlibErrBadGZipHeader); |
|
110 } |
|
111 } |
|
112 |
|
113 /** |
|
114 Write the zip header to the specified file |
|
115 |
|
116 @param aFile the file to write to |
|
117 @param aHeader the header object to write to the file |
|
118 @leave ... Any of the system wide error codes |
|
119 */ |
|
120 EXPORT_C void EZGZipFile::WriteHeaderL(RFile &aFile, TEZGZipHeader &aHeader) |
|
121 { |
|
122 TInt obligatoryData = sizeof(aHeader.iId1) + sizeof(aHeader.iId2) + sizeof(aHeader.iCompressionMethod) + |
|
123 sizeof(aHeader.iFlags) + sizeof(aHeader.iTime) + sizeof(aHeader.iExtraFlags) + sizeof(aHeader.iOs); |
|
124 |
|
125 TPtrC8 des(&aHeader.iId1,obligatoryData); |
|
126 User::LeaveIfError(aFile.Write(des)); |
|
127 TBuf8<1> null(1); |
|
128 null[0] = '\0'; |
|
129 |
|
130 if (aHeader.iFlags & (1 << EFExtra)) // then the extra bit is set |
|
131 { |
|
132 des.Set(REINTERPRET_CAST(TUint8 *,&aHeader.iXlen),sizeof(aHeader.iXlen)); |
|
133 User::LeaveIfError(aFile.Write(des)); |
|
134 |
|
135 User::LeaveIfError(aFile.Write(*aHeader.iExtra)); |
|
136 } |
|
137 |
|
138 if (aHeader.iFlags & (1 << EFName)) // then read in filename |
|
139 { |
|
140 User::LeaveIfError(aFile.Write(*aHeader.iFname)); |
|
141 User::LeaveIfError(aFile.Write(null)); |
|
142 } |
|
143 |
|
144 if (aHeader.iFlags & (1 << EFComment)) // then read in comment |
|
145 { |
|
146 User::LeaveIfError(aFile.Write(*aHeader.iComment)); |
|
147 User::LeaveIfError(aFile.Write(null)); |
|
148 } |
|
149 |
|
150 if (aHeader.iFlags & (1 << EFHcrc)) |
|
151 { |
|
152 des.Set(REINTERPRET_CAST(TUint8*,&aHeader.iCrc),sizeof(aHeader.iCrc)); |
|
153 User::LeaveIfError(aFile.Write(des)); |
|
154 } |
|
155 } |
|
156 |
|
157 void EZGZipFile::ReadStringIntoDescriptorL(RFile &aFile, HBufC8 **aDes) |
|
158 { |
|
159 TInt i, err; |
|
160 CArrayFixFlat<TUint8> *bytes = new (ELeave) CArrayFixFlat<TUint8>(16); |
|
161 CleanupStack::PushL(bytes); |
|
162 TBuf8<1> ch; |
|
163 while (((err = aFile.Read(ch)) == KErrNone) && ch[0] != '\0') |
|
164 bytes->AppendL(*ch.Ptr()); |
|
165 |
|
166 if (err != KErrNone) |
|
167 User::Leave(KEZlibErrBadGZipHeader); |
|
168 |
|
169 *aDes = HBufC8::NewMaxL(bytes->Count()); |
|
170 |
|
171 for (i = 0; i < bytes->Count(); i++) |
|
172 (*aDes)->Des()[i] = (*bytes)[i]; |
|
173 |
|
174 CleanupStack::PopAndDestroy(); // delete bytes |
|
175 } |
|
176 |
|
177 /** |
|
178 Read the zip trailer from the specified zip file into the TEZGZipTrailer object |
|
179 |
|
180 @param aFile the zip file to read from |
|
181 @param aTrailer the target trailer object |
|
182 @leave KEZlibErrBadGZipTrailer invalid zip trailer |
|
183 @leave ... Any of the system wide error codes |
|
184 */ |
|
185 EXPORT_C void EZGZipFile::ReadTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer) |
|
186 { |
|
187 TPtr8 des(REINTERPRET_CAST(TUint8*,&aTrailer.iCrc32),0,sizeof(TEZGZipTrailer)); |
|
188 |
|
189 TInt err = aFile.Read(des); |
|
190 if (err != KErrNone || des.Size() != sizeof(TEZGZipTrailer)) |
|
191 User::Leave(KEZlibErrBadGZipTrailer); |
|
192 } |
|
193 |
|
194 /** |
|
195 Write the zip trailer to the specified file |
|
196 |
|
197 @param aFile the file to write to |
|
198 @param aTrailer the trailer object to write to the file |
|
199 @leave ... Any of the system wide error codes |
|
200 */ |
|
201 EXPORT_C void EZGZipFile::WriteTrailerL(RFile &aFile, TEZGZipTrailer &aTrailer) |
|
202 { |
|
203 TPtrC8 des(REINTERPRET_CAST(TUint8*,&aTrailer.iCrc32),sizeof(TEZGZipTrailer)); |
|
204 User::LeaveIfError(aFile.Write(des)); |
|
205 } |
|
206 |
|
207 /** |
|
208 Find the zip trailer within the specified file, and read it into the TEZGZipTrailer object |
|
209 |
|
210 @param aRfs file server session |
|
211 @param aFname the file to read from |
|
212 @param aTrailer the target trailer object |
|
213 @leave KEZlibErrBadGZipHeader Invalid zip header |
|
214 @leave ... Any of the system wide error codes |
|
215 */ |
|
216 EXPORT_C void EZGZipFile::LocateAndReadTrailerL(RFs &aRfs, const TDesC &aFname, TEZGZipTrailer &aTrailer) |
|
217 { |
|
218 TInt fileSize; |
|
219 |
|
220 RFile file; |
|
221 |
|
222 User::LeaveIfError(file.Open(aRfs,aFname,EFileStream | EFileRead | EFileShareAny)); |
|
223 CleanupClosePushL(file); |
|
224 |
|
225 TUint8 magic[2]; |
|
226 TPtr8 des(magic,0,sizeof(TUint8) * 2); |
|
227 User::LeaveIfError(file.Read(des)); |
|
228 if (magic[0] != ID1 || magic[1] != ID2) |
|
229 User::Leave(KEZlibErrBadGZipHeader); |
|
230 |
|
231 User::LeaveIfError(file.Size(fileSize)); |
|
232 TInt sizePos = fileSize - (sizeof (TInt32) * 2); |
|
233 User::LeaveIfError(file.Seek(ESeekStart,sizePos)); |
|
234 |
|
235 des.Set(REINTERPRET_CAST(TUint8 *,&aTrailer),0,sizeof(TInt32)*2); |
|
236 |
|
237 User::LeaveIfError(file.Read(des)); |
|
238 |
|
239 CleanupStack::PopAndDestroy(); |
|
240 } |
|
241 |
|
242 /** |
|
243 @deprecated Interface is deprecated because it is unsafe as it may leave. It is available for backward compatibility reasons only. |
|
244 @see EZGZipFile::IsGzipFileL |
|
245 */ |
|
246 EXPORT_C TBool EZGZipFile::IsGzipFile(RFs &aRfs, const TDesC &aFname) |
|
247 { |
|
248 TBool retBool = true; |
|
249 TRAPD(errCode, retBool = IsGzipFileL(aRfs, aFname)); |
|
250 if(errCode != KErrNone) |
|
251 { |
|
252 retBool = false; |
|
253 } |
|
254 return retBool; |
|
255 } |
|
256 |
|
257 /** |
|
258 Determine if the given file is a valid zip file |
|
259 |
|
260 @param aRfs file server session |
|
261 @param aFname name of the file to check |
|
262 @leave ... Any of the system wide error codes |
|
263 @return ETrue if the file is valid zip file, EFalse otherwise |
|
264 */ |
|
265 EXPORT_C TBool EZGZipFile::IsGzipFileL(RFs &aRfs, const TDesC &aFname) |
|
266 { |
|
267 TUint8 ids[2]; |
|
268 RFile file; |
|
269 |
|
270 User::LeaveIfError(file.Open(aRfs,aFname,EFileStream | EFileRead | EFileShareAny)); |
|
271 CleanupClosePushL(file); |
|
272 |
|
273 TPtr8 des(ids,0,sizeof(TUint8) * 2); |
|
274 |
|
275 User::LeaveIfError(file.Read(des)); |
|
276 CleanupStack::PopAndDestroy(); |
|
277 return (ids[0] == ID1 && ids[1] == ID2); |
|
278 } |
|
279 |
|
280 //-------------------------------------------------------------------------------------------------------- |
|
281 |
|
282 |
|
283 CEZFileToGzipBM::CEZFileToGzipBM(RFile &aInput, RFile &aOutput) : CEZFileBufferManager(aInput,aOutput) |
|
284 { |
|
285 iCrc = crc32(iCrc,NULL,0); |
|
286 } |
|
287 |
|
288 |
|
289 CEZFileToGzipBM* CEZFileToGzipBM::NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize) |
|
290 { |
|
291 CEZFileToGzipBM *bm = new (ELeave) CEZFileToGzipBM(aInput,aOutput); |
|
292 CleanupStack::PushL(bm); |
|
293 bm->ConstructL(aBufferSize); |
|
294 return bm; |
|
295 } |
|
296 |
|
297 CEZFileToGzipBM* CEZFileToGzipBM::NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize) |
|
298 { |
|
299 CEZFileToGzipBM *bm = new (ELeave) CEZFileToGzipBM(aInput,aOutput); |
|
300 CleanupStack::PushL(bm); |
|
301 bm->ConstructL(aBufferSize); |
|
302 CleanupStack::Pop(); |
|
303 return bm; |
|
304 } |
|
305 |
|
306 void CEZFileToGzipBM::NeedInputL(CEZZStream &aZStream) |
|
307 { |
|
308 CEZFileBufferManager::NeedInputL(aZStream); |
|
309 iCrc = crc32(iCrc,iInputDescriptor.Ptr(),iInputDescriptor.Size()); |
|
310 } |
|
311 |
|
312 void CEZFileToGzipBM::InitializeL(CEZZStream &aZStream) |
|
313 { |
|
314 CEZFileBufferManager::InitializeL(aZStream); |
|
315 iCrc = crc32(iCrc,iInputDescriptor.Ptr(),iInputDescriptor.Size()); |
|
316 } |
|
317 |
|
318 //-------------------------------------------------------------------------------------------------------- |
|
319 |
|
320 |
|
321 CEZGzipToFileBM::CEZGzipToFileBM(RFile &aInput, RFile &aOutput) : CEZFileBufferManager(aInput,aOutput) |
|
322 { |
|
323 iCrc = crc32(iCrc,NULL,0); |
|
324 } |
|
325 |
|
326 CEZGzipToFileBM* CEZGzipToFileBM::NewLC(RFile &aInput, RFile &aOutput, TInt aBufferSize) |
|
327 { |
|
328 CEZGzipToFileBM *bm = new (ELeave) CEZGzipToFileBM(aInput,aOutput); |
|
329 CleanupStack::PushL(bm); |
|
330 bm->ConstructL(aBufferSize); |
|
331 return bm; |
|
332 } |
|
333 |
|
334 CEZGzipToFileBM* CEZGzipToFileBM::NewL(RFile &aInput, RFile &aOutput, TInt aBufferSize) |
|
335 { |
|
336 CEZGzipToFileBM *bm = new (ELeave) CEZGzipToFileBM(aInput,aOutput); |
|
337 CleanupStack::PushL(bm); |
|
338 bm->ConstructL(aBufferSize); |
|
339 CleanupStack::Pop(); |
|
340 return bm; |
|
341 } |
|
342 |
|
343 void CEZGzipToFileBM::NeedOutputL(CEZZStream &aZStream) |
|
344 { |
|
345 TPtrC8 od = aZStream.OutputDescriptor(); |
|
346 iCrc = crc32(iCrc,od.Ptr(),od.Size()); |
|
347 CEZFileBufferManager::NeedOutputL(aZStream); |
|
348 } |
|
349 |
|
350 void CEZGzipToFileBM::FinalizeL(CEZZStream &aZStream) |
|
351 { |
|
352 TPtrC8 od = aZStream.OutputDescriptor(); |
|
353 iCrc = crc32(iCrc,od.Ptr(),od.Size()); |
|
354 CEZFileBufferManager::FinalizeL(aZStream); |
|
355 } |
|
356 |
|
357 |
|
358 //-------------------------------------------------------------------------------------------------------- |
|
359 CEZGZipToFile::CEZGZipToFile() : iDecompressor(NULL), iBufferManager(NULL) |
|
360 { |
|
361 |
|
362 } |
|
363 |
|
364 CEZGZipToFile::~CEZGZipToFile() |
|
365 { |
|
366 delete iDecompressor; |
|
367 delete iBufferManager; |
|
368 iGZipFile.Close(); |
|
369 } |
|
370 |
|
371 /** |
|
372 Creates a new CEZGZipToFile object and leaves it on the CleanupStack |
|
373 |
|
374 @param aRfs open file server session |
|
375 @param aGzFileName name of the file to be de-compressed |
|
376 @param aOutput the target file to hold the un-compressed data |
|
377 @param aBufferSize required size of buffers |
|
378 @return a pointer to the new CEZGZipToFile object, left on the CleanupStack |
|
379 */ |
|
380 EXPORT_C CEZGZipToFile* CEZGZipToFile::NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize) |
|
381 { |
|
382 CEZGZipToFile* dec = new (ELeave) CEZGZipToFile; |
|
383 CleanupStack::PushL(dec); |
|
384 dec->ConstructL(aRfs,aGzFileName,aOutput,aBufferSize); |
|
385 return dec; |
|
386 } |
|
387 |
|
388 /** |
|
389 Creates a new CEZGZipToFile object |
|
390 |
|
391 @param aRfs open file server session |
|
392 @param aGzFileName name of the file to be de-compressed |
|
393 @param aOutput the target file to hold the un-compressed data |
|
394 @param aBufferSize required size of buffers |
|
395 @return a pointer to the new CEZGZipToFile object |
|
396 */ |
|
397 EXPORT_C CEZGZipToFile* CEZGZipToFile::NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize) |
|
398 { |
|
399 CEZGZipToFile* dec = new (ELeave) CEZGZipToFile; |
|
400 CleanupStack::PushL(dec); |
|
401 dec->ConstructL(aRfs,aGzFileName,aOutput,aBufferSize); |
|
402 CleanupStack::Pop(); |
|
403 return dec; |
|
404 } |
|
405 |
|
406 /** |
|
407 Quits the current de-compression operation and restarts with the specified arguments |
|
408 |
|
409 @param aRfs open file server session |
|
410 @param aGzFileName name of the file to be de-compressed |
|
411 @param aOutput the target file to hold the un-compressed data |
|
412 @param aBufferSize required size of buffers |
|
413 @leave ... Any of the system wide error codes |
|
414 */ |
|
415 EXPORT_C void CEZGZipToFile::ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize) |
|
416 { |
|
417 delete iBufferManager; |
|
418 InitialiseBufManL(aRfs,aGzFileName,aOutput,aBufferSize); |
|
419 iDecompressor->ResetL(*iBufferManager); |
|
420 } |
|
421 |
|
422 /** |
|
423 De-compresses the current zip file in stages. The function needs to called again until the de-compression |
|
424 is finalised, in which case it will return EFalse - for example... |
|
425 |
|
426 @code |
|
427 while ( decompressor->InflateL() ) |
|
428 { |
|
429 // No action required |
|
430 } |
|
431 @endcode |
|
432 |
|
433 @leave KEZlibErrBadGZipCrc Invalid CRC check |
|
434 @leave ... Any of the system wide error codes |
|
435 @return ETrue if the de-compression is not complete, and function must be called again |
|
436 @return EFalse if the de-compression is finalised |
|
437 */ |
|
438 EXPORT_C TBool CEZGZipToFile::InflateL() |
|
439 { |
|
440 TBool keepGoing = iDecompressor->InflateL(); |
|
441 |
|
442 if (!keepGoing) |
|
443 { |
|
444 if (iBufferManager->Crc() != iTrailer.iCrc32) |
|
445 User::Leave(KEZlibErrBadGZipCrc); |
|
446 iGZipFile.Close(); |
|
447 } |
|
448 |
|
449 return keepGoing; |
|
450 } |
|
451 |
|
452 void CEZGZipToFile::InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize) |
|
453 { |
|
454 EZGZipFile::LocateAndReadTrailerL(aRfs,aGzFileName,iTrailer); |
|
455 User::LeaveIfError(iGZipFile.Open(aRfs,aGzFileName,EFileStream | EFileRead | EFileShareAny)); |
|
456 EZGZipFile::ReadHeaderL(iGZipFile,iHeader); |
|
457 iBufferManager = CEZGzipToFileBM::NewL(iGZipFile,aOutput,aBufferSize); |
|
458 } |
|
459 |
|
460 void CEZGZipToFile::ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aOutput, TInt aBufferSize) |
|
461 { |
|
462 InitialiseBufManL(aRfs,aGzFileName,aOutput,aBufferSize); |
|
463 |
|
464 // this is a special zlib modification to stop it choking when it can't find the normal zlib stream header. |
|
465 |
|
466 iDecompressor = CEZDecompressor::NewL(*iBufferManager,-CEZDecompressor::EMaxWBits); |
|
467 } |
|
468 |
|
469 |
|
470 //-------------------------------------------------------------------------------------------------------- |
|
471 |
|
472 |
|
473 CEZFileToGZip::CEZFileToGZip() : iCompressor(NULL), iBufferManager(NULL) |
|
474 { |
|
475 |
|
476 } |
|
477 |
|
478 CEZFileToGZip::~CEZFileToGZip() |
|
479 { |
|
480 delete iCompressor; |
|
481 delete iBufferManager; |
|
482 iGZipFile.Close(); |
|
483 } |
|
484 |
|
485 /** |
|
486 Creates a new CEZFileToGZip object and leaves it on the CleanupStack |
|
487 |
|
488 @param aRfs open file server session |
|
489 @param aGzFileName the name of the target zip file |
|
490 @param aInput the file to compress |
|
491 @param aBufferSize required size of buffers |
|
492 @return a pointer to the new CEZFileToGZip object, left on the CleanupStack |
|
493 */ |
|
494 EXPORT_C CEZFileToGZip* CEZFileToGZip::NewLC(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize) |
|
495 { |
|
496 CEZFileToGZip* com = new (ELeave) CEZFileToGZip; |
|
497 CleanupStack::PushL(com); |
|
498 com->ConstructL(aRfs,aGzFileName,aInput,aBufferSize); |
|
499 return com; |
|
500 } |
|
501 |
|
502 /** |
|
503 Creates a new CEZFileToGZip object and leaves it on the CleanupStack |
|
504 |
|
505 @param aRfs open file server session |
|
506 @param aGzFileName the name of the target zip file |
|
507 @param aInput the file to compress |
|
508 @param aBufferSize required size of buffers |
|
509 @return a pointer to the new CEZFileToGZip object, left on the CleanupStack |
|
510 */ |
|
511 EXPORT_C CEZFileToGZip* CEZFileToGZip::NewL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize) |
|
512 { |
|
513 CEZFileToGZip* com = new (ELeave) CEZFileToGZip; |
|
514 CleanupStack::PushL(com); |
|
515 com->ConstructL(aRfs,aGzFileName,aInput,aBufferSize); |
|
516 CleanupStack::Pop(); |
|
517 return com; |
|
518 } |
|
519 |
|
520 /** |
|
521 Quits the current compression operation and restarts with the specified arguments |
|
522 |
|
523 @param aRfs open file server session |
|
524 @param aGzFileName the name of the target zip file |
|
525 @param aInput the file to compress |
|
526 @param aBufferSize required size of buffers |
|
527 @leave ... Any of the system wide error codes |
|
528 */ |
|
529 EXPORT_C void CEZFileToGZip::ResetL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize) |
|
530 { |
|
531 delete iBufferManager; |
|
532 InitialiseBufManL(aRfs,aGzFileName,aInput,aBufferSize); |
|
533 iCompressor->ResetL(*iBufferManager); |
|
534 } |
|
535 |
|
536 /** |
|
537 Compresses the current file in stages. The function needs to called again until the compression |
|
538 is finalised, in which case it will return EFalse - for example... |
|
539 |
|
540 @code |
|
541 while ( compressor->DeflateL() ) |
|
542 { |
|
543 // No action required |
|
544 } |
|
545 @endcode |
|
546 |
|
547 @leave ... Any of the system wide error codes |
|
548 @return ETrue if the compression is not complete, and function must be called again |
|
549 @return EFalse if the compression is finalised |
|
550 */ |
|
551 EXPORT_C TBool CEZFileToGZip::DeflateL() |
|
552 { |
|
553 TBool keepgoing = iCompressor->DeflateL(); |
|
554 if (!keepgoing) |
|
555 { |
|
556 TEZGZipTrailer trailer(iBufferManager->Crc(), iUncompressedDataSize); |
|
557 EZGZipFile::WriteTrailerL(iGZipFile, trailer); |
|
558 iGZipFile.Close(); |
|
559 } |
|
560 |
|
561 return keepgoing; |
|
562 } |
|
563 |
|
564 void CEZFileToGZip::InitialiseBufManL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize) |
|
565 { |
|
566 User::LeaveIfError(aInput.Size(iUncompressedDataSize)); |
|
567 TInt err; |
|
568 |
|
569 err = iGZipFile.Create(aRfs, aGzFileName,EFileStream | EFileWrite | EFileShareExclusive); |
|
570 if (err == KErrAlreadyExists) |
|
571 User::LeaveIfError(iGZipFile.Open(aRfs,aGzFileName,EFileStream | EFileWrite | EFileShareExclusive)); |
|
572 else |
|
573 User::LeaveIfError(err); |
|
574 |
|
575 EZGZipFile::WriteHeaderL(iGZipFile,iHeader); |
|
576 iBufferManager = CEZFileToGzipBM::NewL(aInput,iGZipFile,aBufferSize); |
|
577 } |
|
578 |
|
579 void CEZFileToGZip::ConstructL(RFs &aRfs, const TDesC &aGzFileName, RFile &aInput, TInt aBufferSize) |
|
580 { |
|
581 InitialiseBufManL(aRfs,aGzFileName,aInput,aBufferSize); |
|
582 |
|
583 // this is a special zlib modification to stop zlib writing out its normal zlib stream header. |
|
584 |
|
585 iCompressor = CEZCompressor::NewL(*iBufferManager,CEZCompressor::EDefaultCompression,-CEZCompressor::EMaxWBits); |
|
586 } |