|
1 /* |
|
2 * Copyright (c) 2007 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: Helper class for unzipping KMZ file. |
|
15 * |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "EPos_PosLmUnzipUtil.h" |
|
21 |
|
22 |
|
23 const TInt KMaxDirName = 255; |
|
24 _LIT(KExtractZipPath, "extracts\\"); |
|
25 |
|
26 |
|
27 |
|
28 void PosLmUnzipUtil::DoUnzipL(const TDesC& aPath) |
|
29 { |
|
30 RFs fs; |
|
31 User::LeaveIfError(fs.Connect()); //Connect to file session |
|
32 CleanupClosePushL(fs); |
|
33 TFileName dirFileName(aPath); |
|
34 UnzipFileL(fs, &dirFileName); |
|
35 CleanupStack::PopAndDestroy(&fs); |
|
36 |
|
37 |
|
38 |
|
39 } |
|
40 |
|
41 void PosLmUnzipUtil::UnzipFileL(RFs& aFs, TFileName* aFileName) |
|
42 { |
|
43 CZipFile* zipFile = NULL; |
|
44 CZipFileMember* member = NULL; |
|
45 CZipFileMemberIterator* fileMembers = NULL; |
|
46 zipFile = CZipFile::NewL(aFs,*aFileName); |
|
47 CleanupStack::PushL(zipFile); |
|
48 User::LeaveIfError(zipFile->OpenL()); |
|
49 fileMembers = zipFile->GetMembersL(); |
|
50 CleanupStack::PushL(fileMembers); |
|
51 while (member = fileMembers->NextL()) |
|
52 { |
|
53 //You must take ownership of the member object created for you by NextL()! |
|
54 CleanupStack::PushL(member); |
|
55 ExtractFileL(aFs, member, zipFile, aFileName); |
|
56 |
|
57 CleanupStack::PopAndDestroy(); //member |
|
58 } |
|
59 zipFile->Close(); |
|
60 CleanupStack::PopAndDestroy(2); //fileMembers, zipFile |
|
61 } |
|
62 |
|
63 void PosLmUnzipUtil::ExtractFileL(RFs& aFs, CZipFileMember* aMember, CZipFile* aZipFile, TFileName* aFileName) |
|
64 { |
|
65 TInt loop=0; |
|
66 HBufC* name = aMember->Name()->AllocLC(); |
|
67 // Change any instances of '/' to '\' in zipped file paths |
|
68 while (loop < name->Length()) |
|
69 { |
|
70 if ((*name)[loop] == '/') |
|
71 { |
|
72 name->Des()[loop] = '\\'; |
|
73 } |
|
74 loop++; |
|
75 } |
|
76 //set target path |
|
77 TBuf<KMaxDirName> privatePath; |
|
78 aFs.PrivatePath(privatePath); |
|
79 TFileName fn; |
|
80 fn.Append(privatePath); |
|
81 fn.Append(KExtractZipPath); |
|
82 fn.Append(*name); |
|
83 |
|
84 //create target path if not exist. |
|
85 TInt err = aFs.MkDirAll(fn); |
|
86 if (err != KErrNone && err != KErrAlreadyExists) |
|
87 { |
|
88 User::Leave(err); |
|
89 } |
|
90 RFile expandedMember; |
|
91 User::LeaveIfError(expandedMember.Replace(aFs, fn, EFileShareAny|EFileWrite)); |
|
92 CleanupClosePushL(expandedMember); |
|
93 RZipFileMemberReaderStream* fileStream; |
|
94 |
|
95 // KCompressionMethodNotSupported is possible in decompressing file here |
|
96 User::LeaveIfError(aZipFile->GetInputStreamL(aMember, fileStream)); |
|
97 CleanupStack::PushL(fileStream); |
|
98 |
|
99 // Assume file contents are 8-bit data |
|
100 TUint32 size = aMember->UncompressedSize(); |
|
101 HBufC8* bytes = HBufC8::NewLC(size); |
|
102 TPtr8 ptr = bytes->Des(); //Obtain a modifiable descriptor |
|
103 fileStream->Read(ptr, size); |
|
104 // save the unzipped contents to file |
|
105 User::LeaveIfError(expandedMember.Write(ptr)); |
|
106 expandedMember.Close(); |
|
107 CleanupStack::PopAndDestroy(4); //bytes, fileStream, expandedMember, name |
|
108 } |
|
109 |