1 // 7zExtract.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../../Common/ComTry.h"
6
7 #include "../../Common/ProgressUtils.h"
8
9 #include "7zDecode.h"
10 // #include "7z1Decode.h"
11 #include "7zFolderOutStream.h"
12 #include "7zHandler.h"
13
14 namespace NArchive {
15 namespace N7z {
16
17 struct CExtractFolderInfo
18 {
19 #ifdef _7Z_VOL
20 int VolumeIndex;
21 #endif
22 CNum FileIndex;
23 CNum FolderIndex;
24 CBoolVector ExtractStatuses;
25 UInt64 UnpackSize;
CExtractFolderInfoNArchive::N7z::CExtractFolderInfo26 CExtractFolderInfo(
27 #ifdef _7Z_VOL
28 int volumeIndex,
29 #endif
30 CNum fileIndex, CNum folderIndex):
31 #ifdef _7Z_VOL
32 VolumeIndex(volumeIndex),
33 #endif
34 FileIndex(fileIndex),
35 FolderIndex(folderIndex),
36 UnpackSize(0)
37 {
38 if (fileIndex != kNumNoIndex)
39 {
40 ExtractStatuses.ClearAndSetSize(1);
41 ExtractStatuses[0] = true;
42 }
43 };
44 };
45
Extract(const UInt32 * indices,UInt32 numItems,Int32 testModeSpec,IArchiveExtractCallback * extractCallbackSpec)46 STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
47 Int32 testModeSpec, IArchiveExtractCallback *extractCallbackSpec)
48 {
49 COM_TRY_BEGIN
50 bool testMode = (testModeSpec != 0);
51 CMyComPtr<IArchiveExtractCallback> extractCallback = extractCallbackSpec;
52 UInt64 importantTotalUnpacked = 0;
53
54 bool allFilesMode = (numItems == (UInt32)(Int32)-1);
55 if (allFilesMode)
56 numItems =
57 #ifdef _7Z_VOL
58 _refs.Size();
59 #else
60 _db.Files.Size();
61 #endif
62
63 if(numItems == 0)
64 return S_OK;
65
66 /*
67 if(_volumes.Size() != 1)
68 return E_FAIL;
69 const CVolume &volume = _volumes.Front();
70 const CDbEx &_db = volume.Database;
71 IInStream *_inStream = volume.Stream;
72 */
73
74 CObjectVector<CExtractFolderInfo> extractFolderInfoVector;
75 for (UInt32 ii = 0; ii < numItems; ii++)
76 {
77 // UInt32 fileIndex = allFilesMode ? indexIndex : indices[indexIndex];
78 UInt32 ref2Index = allFilesMode ? ii : indices[ii];
79 // const CRef2 &ref2 = _refs[ref2Index];
80
81 // for (UInt32 ri = 0; ri < ref2.Refs.Size(); ri++)
82 {
83 #ifdef _7Z_VOL
84 // const CRef &ref = ref2.Refs[ri];
85 const CRef &ref = _refs[ref2Index];
86
87 int volumeIndex = ref.VolumeIndex;
88 const CVolume &volume = _volumes[volumeIndex];
89 const CDbEx &db = volume.Database;
90 UInt32 fileIndex = ref.ItemIndex;
91 #else
92 const CDbEx &db = _db;
93 UInt32 fileIndex = ref2Index;
94 #endif
95
96 CNum folderIndex = db.FileIndexToFolderIndexMap[fileIndex];
97 if (folderIndex == kNumNoIndex)
98 {
99 extractFolderInfoVector.Add(CExtractFolderInfo(
100 #ifdef _7Z_VOL
101 volumeIndex,
102 #endif
103 fileIndex, kNumNoIndex));
104 continue;
105 }
106 if (extractFolderInfoVector.IsEmpty() ||
107 folderIndex != extractFolderInfoVector.Back().FolderIndex
108 #ifdef _7Z_VOL
109 || volumeIndex != extractFolderInfoVector.Back().VolumeIndex
110 #endif
111 )
112 {
113 extractFolderInfoVector.Add(CExtractFolderInfo(
114 #ifdef _7Z_VOL
115 volumeIndex,
116 #endif
117 kNumNoIndex, folderIndex));
118 UInt64 unpackSize = db.GetFolderUnpackSize(folderIndex);
119 importantTotalUnpacked += unpackSize;
120 extractFolderInfoVector.Back().UnpackSize = unpackSize;
121 }
122
123 CExtractFolderInfo &efi = extractFolderInfoVector.Back();
124
125 // const CFolderInfo &folderInfo = m_dam_Folders[folderIndex];
126 CNum startIndex = db.FolderStartFileIndex[folderIndex];
127 for (CNum index = efi.ExtractStatuses.Size();
128 index <= fileIndex - startIndex; index++)
129 {
130 // UInt64 unpackSize = _db.Files[startIndex + index].UnpackSize;
131 // Count partial_folder_size
132 // efi.UnpackSize += unpackSize;
133 // importantTotalUnpacked += unpackSize;
134 efi.ExtractStatuses.Add(index == fileIndex - startIndex);
135 }
136 }
137 }
138
139 RINOK(extractCallback->SetTotal(importantTotalUnpacked));
140
141 CDecoder decoder(
142 #ifdef _ST_MODE
143 false
144 #else
145 true
146 #endif
147 );
148 // CDecoder1 decoder;
149
150 UInt64 totalPacked = 0;
151 UInt64 totalUnpacked = 0;
152 UInt64 curPacked, curUnpacked;
153
154 CLocalProgress *lps = new CLocalProgress;
155 CMyComPtr<ICompressProgressInfo> progress = lps;
156 lps->Init(extractCallback, false);
157
158 for (unsigned i = 0;; i++, totalUnpacked += curUnpacked, totalPacked += curPacked)
159 {
160 lps->OutSize = totalUnpacked;
161 lps->InSize = totalPacked;
162 RINOK(lps->SetCur());
163
164 if (i >= extractFolderInfoVector.Size())
165 break;
166
167 const CExtractFolderInfo &efi = extractFolderInfoVector[i];
168 curUnpacked = efi.UnpackSize;
169 curPacked = 0;
170
171 CFolderOutStream *folderOutStream = new CFolderOutStream;
172 CMyComPtr<ISequentialOutStream> outStream(folderOutStream);
173
174 #ifdef _7Z_VOL
175 const CVolume &volume = _volumes[efi.VolumeIndex];
176 const CDbEx &db = volume.Database;
177 #else
178 const CDbEx &db = _db;
179 #endif
180
181 CNum startIndex;
182 if (efi.FileIndex != kNumNoIndex)
183 startIndex = efi.FileIndex;
184 else
185 startIndex = db.FolderStartFileIndex[efi.FolderIndex];
186
187 HRESULT result = folderOutStream->Init(&db,
188 #ifdef _7Z_VOL
189 volume.StartRef2Index,
190 #else
191 0,
192 #endif
193 startIndex,
194 &efi.ExtractStatuses, extractCallback, testMode, _crcSize != 0);
195
196 RINOK(result);
197
198 if (efi.FileIndex != kNumNoIndex)
199 continue;
200
201 CNum folderIndex = efi.FolderIndex;
202 curPacked = _db.GetFolderFullPackSize(folderIndex);
203
204 #ifndef _NO_CRYPTO
205 CMyComPtr<ICryptoGetTextPassword> getTextPassword;
206 if (extractCallback)
207 extractCallback.QueryInterface(IID_ICryptoGetTextPassword, &getTextPassword);
208 #endif
209
210 try
211 {
212 #ifndef _NO_CRYPTO
213 bool isEncrypted = false;
214 bool passwordIsDefined = false;
215 #endif
216
217 HRESULT result = decoder.Decode(
218 EXTERNAL_CODECS_VARS
219 #ifdef _7Z_VOL
220 volume.Stream,
221 #else
222 _inStream,
223 #endif
224 db.ArcInfo.DataStartPosition,
225 db, folderIndex,
226 outStream,
227 progress
228 _7Z_DECODER_CRYPRO_VARS
229 #if !defined(_7ZIP_ST) && !defined(_SFX)
230 , true, _numThreads
231 #endif
232 );
233
234 if (result == S_FALSE)
235 {
236 RINOK(folderOutStream->FlushCorrupted(NExtract::NOperationResult::kDataError));
237 continue;
238 }
239 if (result == E_NOTIMPL)
240 {
241 RINOK(folderOutStream->FlushCorrupted(NExtract::NOperationResult::kUnsupportedMethod));
242 continue;
243 }
244 if (result != S_OK)
245 return result;
246 if (folderOutStream->WasWritingFinished() != S_OK)
247 {
248 RINOK(folderOutStream->FlushCorrupted(NExtract::NOperationResult::kDataError));
249 continue;
250 }
251 }
252 catch(...)
253 {
254 RINOK(folderOutStream->FlushCorrupted(NExtract::NOperationResult::kDataError));
255 continue;
256 }
257 }
258 return S_OK;
259 COM_TRY_END
260 }
261
262 }}
263