|
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 <zipfile.h> |
|
17 |
|
18 #include "unzip.h" |
|
19 |
|
20 CUnzip::CUnzip(RFs& aFs, const TDesC& aFileName) : iZipFile(aFileName), iFs(aFs) |
|
21 { |
|
22 } |
|
23 |
|
24 CUnzip::~CUnzip() |
|
25 { |
|
26 delete iZip; |
|
27 } |
|
28 |
|
29 EXPORT_C CUnzip* CUnzip::NewLC(RFs& aFs, const TDesC& aFileName) |
|
30 { |
|
31 CUnzip* self = new(ELeave) CUnzip(aFs, aFileName); |
|
32 CleanupStack::PushL(self); |
|
33 self->iZip = CZipFile::NewL(self->iFs, self->iZipFile); |
|
34 return self; |
|
35 } |
|
36 |
|
37 |
|
38 EXPORT_C void CUnzip::ExtractL(const TDesC& aOutDir) |
|
39 { |
|
40 CZipFileMemberIterator* members = iZip->GetMembersL(); |
|
41 CleanupStack::PushL(members); |
|
42 |
|
43 CZipFileMember* member = members->NextL(); |
|
44 while (member) |
|
45 { |
|
46 CleanupStack::PushL(member); |
|
47 ExtractFileL(member, aOutDir); |
|
48 CleanupStack::PopAndDestroy(member); |
|
49 member = members->NextL(); |
|
50 } |
|
51 |
|
52 CleanupStack::PopAndDestroy(members); |
|
53 } |
|
54 |
|
55 |
|
56 void CUnzip::ExtractFileL(const CZipFileMember* aMember, const TDesC& aOutDir) |
|
57 { |
|
58 HBufC* name = aMember->Name()->AllocLC(); |
|
59 for (TInt i = 0; i<name->Length(); i++) |
|
60 { |
|
61 if ((*name)[i] == '/') |
|
62 { |
|
63 name->Des()[i] = '\\'; |
|
64 } |
|
65 } |
|
66 |
|
67 TFileName outFile; |
|
68 outFile.Append(aOutDir); |
|
69 outFile.Append('\\'); |
|
70 outFile.Append(*name); |
|
71 |
|
72 RFile out; |
|
73 CleanupClosePushL(out); |
|
74 |
|
75 iFs.MkDirAll(outFile); |
|
76 TInt retVal = out.Replace(iFs, outFile, EFileShareAny|EFileWrite); |
|
77 if(retVal != KErrNone) |
|
78 { |
|
79 //Could be a directory name which we are planning to write to. |
|
80 CleanupStack::PopAndDestroy(2,name); //out, name |
|
81 out.Close(); |
|
82 return; |
|
83 } |
|
84 |
|
85 RZipFileMemberReaderStream* fileStream; |
|
86 TInt error = iZip->GetInputStreamL(aMember, fileStream); |
|
87 if (error != KErrNone) |
|
88 { |
|
89 iFs.Delete(outFile); |
|
90 User::Leave(error); |
|
91 } |
|
92 CleanupStack::PushL(fileStream); |
|
93 |
|
94 TUint32 size = aMember->UncompressedSize(); |
|
95 HBufC8* bytes = HBufC8::New(size); |
|
96 CleanupStack::PushL(bytes); |
|
97 TPtr8 ptr = bytes->Des(); |
|
98 User::LeaveIfError(fileStream->Read(ptr,size)); |
|
99 User::LeaveIfError(out.Write(ptr)); |
|
100 CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, out and name |
|
101 } |