|
1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Uncompress the zipped vpn policies. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "zipfiledecompressor.h" |
|
21 |
|
22 #include <zipfile.h> |
|
23 #include <bautils.h> |
|
24 |
|
25 CZipFileDecompressor* CZipFileDecompressor::NewL() |
|
26 { |
|
27 CZipFileDecompressor* self = new (ELeave) CZipFileDecompressor; |
|
28 CleanupStack::PushL(self); |
|
29 self->ConstructL(); |
|
30 CleanupStack::Pop(self); |
|
31 return self; |
|
32 } |
|
33 |
|
34 |
|
35 CZipFileDecompressor::CZipFileDecompressor() |
|
36 : CActive(EPriorityStandard) |
|
37 { |
|
38 CActiveScheduler::Add(this); |
|
39 } |
|
40 |
|
41 |
|
42 void CZipFileDecompressor::ConstructL() |
|
43 { |
|
44 User::LeaveIfError(iFileServer.Connect()); |
|
45 } |
|
46 |
|
47 |
|
48 CZipFileDecompressor::~CZipFileDecompressor() |
|
49 { |
|
50 Cancel(); |
|
51 |
|
52 iUncompressedFile.Close(); |
|
53 delete iUncompressedData; |
|
54 |
|
55 delete iZipFileMemberIterator; |
|
56 delete iZipFile; |
|
57 |
|
58 if (iCloseZippedFile) |
|
59 { |
|
60 iZippedFile.Close(); |
|
61 iCloseZippedFile = EFalse; |
|
62 } |
|
63 |
|
64 iFileServer.Close(); |
|
65 } |
|
66 |
|
67 |
|
68 void CZipFileDecompressor::DecompressZipFile(const TDesC& aSourceFile, |
|
69 const TDesC& aTargetDir, |
|
70 TRequestStatus& aClientStatus) |
|
71 { |
|
72 __ASSERT_ALWAYS(!IsActive(), User::Invariant()); |
|
73 |
|
74 iClientStatus = &aClientStatus; |
|
75 *iClientStatus = KRequestPending; |
|
76 |
|
77 RFile zippedFile; |
|
78 TInt err = zippedFile.Open(iFileServer, aSourceFile, EFileRead); |
|
79 if (err == KErrNone) |
|
80 { |
|
81 iCloseZippedFile = ETrue; |
|
82 DecompressZipFile(zippedFile, |
|
83 aTargetDir, |
|
84 aClientStatus); |
|
85 } |
|
86 else |
|
87 { |
|
88 iStatus = KRequestPending; |
|
89 SetActive(); |
|
90 |
|
91 TRequestStatus* ownStatus = &iStatus; |
|
92 User::RequestComplete(ownStatus, err); |
|
93 } |
|
94 } |
|
95 |
|
96 |
|
97 |
|
98 void CZipFileDecompressor::DecompressZipFile(RFile& aSourceFile, |
|
99 const TDesC& aTargetDir, |
|
100 TRequestStatus& aClientStatus) |
|
101 { |
|
102 __ASSERT_ALWAYS(!IsActive(), User::Invariant()); |
|
103 iClientStatus = &aClientStatus; |
|
104 *iClientStatus = KRequestPending; |
|
105 |
|
106 TRAPD(err, DecompressZipFileL(aSourceFile, aTargetDir)); |
|
107 if (err != KErrNone) |
|
108 { |
|
109 iStatus = KRequestPending; |
|
110 SetActive(); |
|
111 |
|
112 TRequestStatus* ownStatus = &iStatus; |
|
113 User::RequestComplete(ownStatus, err); |
|
114 } |
|
115 } |
|
116 |
|
117 |
|
118 void CZipFileDecompressor::DecompressZipFileL(RFile& aSourceFile, |
|
119 const TDesC& aTargetDir) |
|
120 { |
|
121 |
|
122 iZippedFile = aSourceFile; |
|
123 |
|
124 BaflUtils::EnsurePathExistsL(iFileServer, aTargetDir); |
|
125 User::LeaveIfError(iFileServer.SetSessionPath(aTargetDir)); |
|
126 |
|
127 iZipFile = CZipFile::NewL(iFileServer, iZippedFile); |
|
128 |
|
129 iZipFileMemberIterator = iZipFile->GetMembersL(); |
|
130 |
|
131 CZipFileMember* member = iZipFileMemberIterator->NextL(); |
|
132 if (!member) |
|
133 { |
|
134 User::Leave(KErrArgument); |
|
135 } |
|
136 CleanupStack::PushL(member); |
|
137 DecompressNextMemberL(*member); |
|
138 CleanupStack::PopAndDestroy(member); |
|
139 } |
|
140 |
|
141 |
|
142 void CZipFileDecompressor::DecompressNextMemberL(CZipFileMember& aMember) |
|
143 { |
|
144 __ASSERT_ALWAYS(!iUncompressedFile.SubSessionHandle(), |
|
145 User::Invariant()); |
|
146 __ASSERT_ALWAYS(!iUncompressedData, User::Invariant()); |
|
147 |
|
148 |
|
149 //Ignore entries that has zero uncompressed size. |
|
150 //(This includes e.g. directories) |
|
151 if (aMember.UncompressedSize() > 0) |
|
152 { |
|
153 const TChar KDirectorySeparator('\\'); |
|
154 |
|
155 TUint32 uncompressedSize = aMember.UncompressedSize(); |
|
156 HBufC8* uncompressedData = HBufC8::NewLC(uncompressedSize); |
|
157 |
|
158 RZipFileMemberReaderStream* readerStream; |
|
159 User::LeaveIfError(iZipFile->GetInputStreamL(&aMember, readerStream)); |
|
160 CleanupStack::PushL(readerStream); |
|
161 |
|
162 TPtr8 uncompressedDataPtr = uncompressedData->Des(); |
|
163 User::LeaveIfError(readerStream->Read(uncompressedDataPtr, |
|
164 uncompressedDataPtr.MaxLength())); |
|
165 |
|
166 CleanupStack::PopAndDestroy(readerStream); |
|
167 |
|
168 HBufC* fileName = aMember.Name()->AllocLC(); |
|
169 TPtr fileNamePtr= fileName->Des(); |
|
170 TInt lastDirectorySeparator = fileName->LocateReverse(KDirectorySeparator); |
|
171 if (lastDirectorySeparator >= 0) |
|
172 { |
|
173 fileNamePtr = fileName->Mid(lastDirectorySeparator+1); |
|
174 } |
|
175 |
|
176 TParsePtr fileNameParser(fileNamePtr); |
|
177 |
|
178 User::LeaveIfError(iUncompressedFile.Replace(iFileServer, |
|
179 fileNameParser.NameAndExt(), |
|
180 EFileWrite)); |
|
181 CleanupStack::PopAndDestroy(fileName); |
|
182 |
|
183 CleanupStack::Pop(uncompressedData); |
|
184 iUncompressedData = uncompressedData; |
|
185 |
|
186 iUncompressedFile.Write(*iUncompressedData, iStatus); |
|
187 SetActive(); |
|
188 } |
|
189 else |
|
190 { |
|
191 iStatus = KRequestPending; |
|
192 SetActive(); |
|
193 TRequestStatus* ownStatus = &iStatus; |
|
194 User::RequestComplete(ownStatus, KErrNone); |
|
195 } |
|
196 } |
|
197 |
|
198 |
|
199 |
|
200 void CZipFileDecompressor::RunL() |
|
201 { |
|
202 delete iUncompressedData; |
|
203 iUncompressedData = NULL; |
|
204 |
|
205 iUncompressedFile.Close(); |
|
206 |
|
207 if (iStatus.Int() == KErrNone) |
|
208 { |
|
209 CZipFileMember* member = iZipFileMemberIterator->NextL(); |
|
210 if (member) |
|
211 { |
|
212 CleanupStack::PushL(member); |
|
213 DecompressNextMemberL(*member); |
|
214 CleanupStack::PopAndDestroy(member); |
|
215 } |
|
216 else |
|
217 { |
|
218 delete iZipFileMemberIterator; |
|
219 iZipFileMemberIterator = NULL; |
|
220 |
|
221 delete iZipFile; |
|
222 iZipFile = NULL; |
|
223 |
|
224 if (iCloseZippedFile) |
|
225 { |
|
226 iZippedFile.Close(); |
|
227 iCloseZippedFile = EFalse; |
|
228 } |
|
229 |
|
230 |
|
231 User::RequestComplete(iClientStatus, KErrNone); |
|
232 } |
|
233 } |
|
234 else |
|
235 { |
|
236 delete iZipFileMemberIterator; |
|
237 iZipFileMemberIterator = NULL; |
|
238 |
|
239 delete iZipFile; |
|
240 iZipFile = NULL; |
|
241 |
|
242 if (iCloseZippedFile) |
|
243 { |
|
244 iZippedFile.Close(); |
|
245 iCloseZippedFile = EFalse; |
|
246 } |
|
247 |
|
248 |
|
249 User::RequestComplete(iClientStatus, iStatus.Int()); |
|
250 } |
|
251 } |
|
252 |
|
253 |
|
254 void CZipFileDecompressor::DoCancel() |
|
255 { |
|
256 //No Cancel for write... |
|
257 } |
|
258 |
|
259 |
|
260 void CZipFileDecompressor::Cancel() |
|
261 { |
|
262 CActive::Cancel(); |
|
263 |
|
264 delete iUncompressedData; |
|
265 iUncompressedData = NULL; |
|
266 |
|
267 iUncompressedFile.Close(); |
|
268 |
|
269 delete iZipFileMemberIterator; |
|
270 iZipFileMemberIterator = NULL; |
|
271 |
|
272 delete iZipFile; |
|
273 iZipFile = NULL; |
|
274 |
|
275 if (iCloseZippedFile) |
|
276 { |
|
277 iZippedFile.Close(); |
|
278 iCloseZippedFile = EFalse; |
|
279 } |
|
280 |
|
281 |
|
282 } |
|
283 |
|
284 TInt CZipFileDecompressor::RunError(TInt aError) |
|
285 { |
|
286 delete iUncompressedData; |
|
287 iUncompressedData = NULL; |
|
288 |
|
289 iUncompressedFile.Close(); |
|
290 |
|
291 delete iZipFileMemberIterator; |
|
292 iZipFileMemberIterator = NULL; |
|
293 |
|
294 delete iZipFile; |
|
295 iZipFile = NULL; |
|
296 |
|
297 if (iCloseZippedFile) |
|
298 { |
|
299 iZippedFile.Close(); |
|
300 iCloseZippedFile = EFalse; |
|
301 } |
|
302 |
|
303 |
|
304 User::RequestComplete(iClientStatus, aError); |
|
305 |
|
306 return KErrNone; |
|
307 } |
|
308 |