Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompressCall2.cpp « Common « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b3421e5a05e5761502026afb6924def596f9cc44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// CompressCall.cpp

#include "StdAfx.h"

#include "../../../Common/MyException.h"

#include "../../UI/common/ArchiveCommandLine.h"

#include "../../UI/GUI/BenchmarkDialog.h"
#include "../../UI/GUI/ExtractGUI.h"
#include "../../UI/GUI/UpdateGUI.h"
#include "../../UI/GUI/HashGUI.h"

#include "../../UI/GUI/ExtractRes.h"

#include "CompressCall.h"

extern HWND g_HWND;

#define MY_TRY_BEGIN  HRESULT result; try {
#define MY_TRY_FINISH } \
  catch(CSystemException &e) { result = e.ErrorCode; } \
  catch(...) { result = E_FAIL; } \
  if (result != S_OK && result != E_ABORT) \
    ErrorMessageHRESULT(result);

static void ThrowException_if_Error(HRESULT res)
{
  if (res != S_OK)
    throw CSystemException(res);
}

#ifdef EXTERNAL_CODECS

#define CREATE_CODECS \
  CCodecs *codecs = new CCodecs; \
  CMyComPtr<ICompressCodecsInfo> compressCodecsInfo = codecs; \
  ThrowException_if_Error(codecs->Load());

#define LOAD_EXTERNAL_CODECS \
    CExternalCodecs __externalCodecs; \
    __externalCodecs.GetCodecs = codecs; \
    __externalCodecs.GetHashers = codecs; \
    ThrowException_if_Error(__externalCodecs.Load());

#else

#define CREATE_CODECS \
  CCodecs *codecs = new CCodecs; \
  CMyComPtr<IUnknown> compressCodecsInfo = codecs; \
  ThrowException_if_Error(codecs->Load());

#define LOAD_EXTERNAL_CODECS

#endif



 
UString GetQuotedString(const UString &s)
{
  UString s2 = L'\"';
  s2 += s;
  s2 += L'\"';
  return s2;
}

static void ErrorMessage(LPCWSTR message)
{
  MessageBoxW(g_HWND, message, L"7-Zip", MB_ICONERROR);
}

static void ErrorMessageHRESULT(HRESULT res)
{
  ErrorMessage(HResultToMessage(res));
}

static void ErrorLangMessage(UINT resourceID)
{
  ErrorMessage(LangString(resourceID));
}

HRESULT CompressFiles(
    const UString &arcPathPrefix,
    const UString &arcName,
    const UString &arcType,
    bool addExtension,
    const UStringVector &names,
    bool email, bool showDialog, bool /* waitFinish */)
{
  MY_TRY_BEGIN
  
  CREATE_CODECS

  CUpdateCallbackGUI callback;
  
  callback.Init();

  CUpdateOptions uo;
  uo.EMailMode = email;
  uo.SetActionCommand_Add();

  uo.ArcNameMode = (addExtension ? k_ArcNameMode_Add : k_ArcNameMode_Exact);

  CObjectVector<COpenType> formatIndices;
  if (!ParseOpenTypes(*codecs, arcType, formatIndices))
  {
    ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE);
    return E_FAIL;
  }
  const UString arcPath = arcPathPrefix + arcName;
  if (!uo.InitFormatIndex(codecs, formatIndices, arcPath) ||
      !uo.SetArcPath(codecs, arcPath))
  {
    ErrorLangMessage(IDS_UPDATE_NOT_SUPPORTED);
    return E_FAIL;
  }

  NWildcard::CCensor censor;
  FOR_VECTOR (i, names)
  {
    censor.AddPreItem(names[i]);
  }

  bool messageWasDisplayed = false;

  result = UpdateGUI(codecs,
      formatIndices, arcPath,
      censor, uo, showDialog, messageWasDisplayed, &callback, g_HWND);
  
  if (result != S_OK)
  {
    if (result != E_ABORT && messageWasDisplayed)
      return E_FAIL;
    throw CSystemException(result);
  }
  if (callback.FailedFiles.Size() > 0)
  {
    if (!messageWasDisplayed)
      throw CSystemException(E_FAIL);
    return E_FAIL;
  }
  
  MY_TRY_FINISH
  return S_OK;
}

static HRESULT ExtractGroupCommand(const UStringVector &arcPaths,
    bool showDialog, const UString &outFolder, bool testMode, bool elimDup = false)
{
  MY_TRY_BEGIN
  
  CREATE_CODECS

  CExtractOptions eo;
  eo.OutputDir = us2fs(outFolder);
  eo.TestMode = testMode;
  eo.ElimDup.Val = elimDup;
  eo.ElimDup.Def = elimDup;

  CExtractCallbackImp *ecs = new CExtractCallbackImp;
  CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
  
  ecs->Init();
  
  // eo.CalcCrc = options.CalcCrc;
  
  UStringVector arcPathsSorted;
  UStringVector arcFullPathsSorted;
  {
    NWildcard::CCensor arcCensor;
    FOR_VECTOR (i, arcPaths)
    {
      arcCensor.AddPreItem(arcPaths[i]);
    }
    arcCensor.AddPathsToCensor(NWildcard::k_RelatPath);
    CDirItemsStat st;
    EnumerateDirItemsAndSort(arcCensor, NWildcard::k_RelatPath, UString(),
        arcPathsSorted, arcFullPathsSorted,
        st,
        NULL // &scan: change it!!!!
        );
  }
  
  CObjectVector<COpenType> formatIndices;
  
  NWildcard::CCensor censor;
  {
    censor.AddPreItem_Wildcard();
  }
  
  censor.AddPathsToCensor(NWildcard::k_RelatPath);

  bool messageWasDisplayed = false;
  result = ExtractGUI(codecs,
      formatIndices, CIntVector(),
      arcPathsSorted, arcFullPathsSorted,
      censor.Pairs.Front().Head, eo, NULL, showDialog, messageWasDisplayed, ecs, g_HWND);
  if (result != S_OK)
  {
    if (result != E_ABORT && messageWasDisplayed)
      return E_FAIL;
    throw CSystemException(result);
  }
  return ecs->IsOK() ? S_OK : E_FAIL;
  
  MY_TRY_FINISH
  return result;
}

void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup)
{
  ExtractGroupCommand(arcPaths, showDialog, outFolder, false, elimDup);
}

void TestArchives(const UStringVector &arcPaths)
{
  ExtractGroupCommand(arcPaths, true, UString(), true);
}

void CalcChecksum(const UStringVector &paths, const UString &methodName)
{
  MY_TRY_BEGIN
  
  CREATE_CODECS
  LOAD_EXTERNAL_CODECS

  NWildcard::CCensor censor;
  FOR_VECTOR (i, paths)
  {
    censor.AddPreItem(paths[i]);
  }

  censor.AddPathsToCensor(NWildcard::k_RelatPath);
  bool messageWasDisplayed = false;

  CHashOptions options;
  options.Methods.Add(methodName);

  result = HashCalcGUI(EXTERNAL_CODECS_VARS_L censor, options, messageWasDisplayed);
  if (result != S_OK)
  {
    if (result != E_ABORT && messageWasDisplayed)
      return; //  E_FAIL;
    throw CSystemException(result);
  }
  
  MY_TRY_FINISH
  return; //  result;
}

void Benchmark(bool totalMode)
{
  MY_TRY_BEGIN
  
  CREATE_CODECS
  LOAD_EXTERNAL_CODECS
  
  CObjectVector<CProperty> props;
  if (totalMode)
  {
    CProperty prop;
    prop.Name = L"m";
    prop.Value = L"*";
    props.Add(prop);
  }
  result = Benchmark(EXTERNAL_CODECS_VARS_L props, g_HWND);
  
  MY_TRY_FINISH
}