-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7zHelper.cpp
212 lines (174 loc) · 5.35 KB
/
7zHelper.cpp
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
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "7zHelper.h"
#include "./lzma2107/CPP/Common/Common.h"
#include "./lzma2107/CPP/Common/MyWindows.h"
#include "./lzma2107/CPP/Common/Defs.h"
#include "./lzma2107/CPP/Common/MyInitGuid.h"
#include "./lzma2107/CPP/Common/IntToString.h"
#include "./lzma2107/CPP/Common/StringConvert.h"
#include "./lzma2107/CPP/Windows/DLL.h"
#include "./lzma2107/CPP/Windows/FileDir.h"
#include "./lzma2107/CPP/Windows/FileFind.h"
#include "./lzma2107/CPP/Windows/FileName.h"
#include "./lzma2107/CPP/Windows/NtCheck.h"
#include "./lzma2107/CPP/Windows/PropVariant.h"
#include "./lzma2107/CPP/Windows/PropVariantConv.h"
#include "./lzma2107/CPP/7zip/Common/FileStreams.h"
#include "./lzma2107/CPP/7zip/Archive/IArchive.h"
#include "./lzma2107/CPP/7zip/IPassword.h"
#include "./lzma2107/C/7zVersion.h"
#include "CompressUpdateCB.h"
DEFINE_GUID(CLSID_CFormat7z,
0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatXz,
0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x0C, 0x00, 0x00);
#define CLSID_Format CLSID_CFormat7z
// #define CLSID_Format CLSID_CFormatXz
using namespace NWindows;
using namespace NFile;
using namespace NDir;
#include <vector>
using namespace std;
static void Convert_UString_to_AString(const UString &s, AString &temp)
{
int codePage = CP_OEMCP;
/*
int g_CodePage = -1;
int codePage = g_CodePage;
if (codePage == -1)
codePage = CP_OEMCP;
if (codePage == CP_UTF8)
ConvertUnicodeToUTF8(s, temp);
else
*/
UnicodeStringToMultiByte2(temp, s, (UINT)codePage);
}
static FString CmdStringToFString(const char *s)
{
return us2fs(GetUnicodeString(s));
}
static void Print(const char *s)
{
fputs(s, stdout);
}
static void Print(const AString &s)
{
//Print(s.Ptr());
}
static void Print(const UString &s)
{
AString as;
Convert_UString_to_AString(s, as);
Print(as);
}
static void Print(const wchar_t *s)
{
Print(UString(s));
}
static void PrintNewLine()
{
//Print("\n");
}
static void PrintStringLn(const char *s)
{
Print(s);
PrintNewLine();
}
static void PrintError(const char *message)
{
Print("Error: ");
PrintNewLine();
Print(message);
PrintNewLine();
}
static void PrintError(const char *message, const FString &name)
{
PrintError(message);
Print(name);
}
static HRESULT IsArchiveItemProp(IInArchive *archive, UInt32 index, PROPID propID, bool &result)
{
NCOM::CPropVariant prop;
RINOK(archive->GetProperty(index, propID, &prop));
if (prop.vt == VT_BOOL)
result = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt == VT_EMPTY)
result = false;
else
return E_FAIL;
return S_OK;
}
static HRESULT IsArchiveItemFolder(IInArchive *archive, UInt32 index, bool &result)
{
return IsArchiveItemProp(archive, index, kpidIsDir, result);
}
STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject);
int Q7zComprocess(CString strArchive, CString strPassword, vector<CString> &vetFileList)
{
int iRet=0;
CObjectVector<CDirItem> dirItems;
for (auto &file : vetFileList)
{
CString strFile = file;
CDirItem di;
FString name = strFile;
NFind::CFileInfo fi;
if (!fi.Find(name))
{
printf("Can't find file[%s]", strFile);
//return 1;
continue;
}
di.Attrib = fi.Attrib;
di.Size = fi.Size;
di.CTime = fi.CTime;
di.ATime = fi.ATime;
di.MTime = fi.MTime;
//di.Name = fs2us(name);
di.Name = fi.Name; /*fs2us(name)*/;
di.FullPath = name;
dirItems.Add(di);
}
COutFileStream *outFileStreamSpec = new COutFileStream;
CMyComPtr<IOutStream> outFileStream = outFileStreamSpec;
if (!outFileStreamSpec->Create(strArchive, true))
{
//PrintError("can't create archive file");
printf("[Q7zComprocess]ÎÞ·¨´´½¨Ñ¹ËõÎļþ[%s],GetLastError=%d",strArchive,GetLastError());
return 1;
}
CMyComPtr<IOutArchive> outArchive;
if (CreateObject(&CLSID_Format, &IID_IOutArchive, (void **)&outArchive) != S_OK)
{
//PrintError("Can not get class object");
printf("[Q7zComprocess]CreateObject[CLSID_Format]ʧ°Ü[%s],GetLastError=%d",strArchive,GetLastError());
return 2;
}
CArchiveUpdateCallback *updateCallbackSpec = new CArchiveUpdateCallback;
#if 1
updateCallbackSpec->PasswordIsDefined = true;
// updateCallbackSpec->AskPassword = false;
updateCallbackSpec->Password = strPassword;
#endif
CMyComPtr<IArchiveUpdateCallback2> updateCallback(updateCallbackSpec);
updateCallbackSpec->Init(&dirItems);
HRESULT result = outArchive->UpdateItems(outFileStream, dirItems.Size(), updateCallback);
updateCallbackSpec->Finilize();
E_FAIL;
if (result != S_OK)
{
printf("[Q7zComprocess]Update Error(%S),UpdateItems return %d,GetLastError=%d",strArchive.GetBuffer(), result, GetLastError());
return 3;
}
FOR_VECTOR (i, updateCallbackSpec->FailedFiles)
{
//PrintNewLine();
//PrintError("Error for file", updateCallbackSpec->FailedFiles[i]);
}
if (updateCallbackSpec->FailedFiles.Size() != 0){
printf("[[Q7zComprocess]updateCallbackSpec->FailedFiles.Size() != 0[%s],GetLastError=%d",strArchive,GetLastError());
return 4;
}
return iRet;
}