-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path7zip_explore.h
More file actions
317 lines (272 loc) · 6.45 KB
/
7zip_explore.h
File metadata and controls
317 lines (272 loc) · 6.45 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#ifndef _7ZIP_EXPLORE_H_
#define _7ZIP_EXPLORE_H_
#include "lib7zip/lib7zip.h"
//#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
class TestInStream : public C7ZipInStream
{
private:
FILE * m_pFile;
wstring m_strFileName;
wstring m_strFileExt;
int m_nFileSize;
public:
TestInStream(wstring fileName) :
m_strFileName(fileName),
m_strFileExt(L"7z")
{
wprintf(L"fileName.c_str(): %s\n", fileName.c_str());
m_pFile = _wfopen(fileName.c_str(), L"rb");
if (m_pFile) {
fseek(m_pFile, 0, SEEK_END);
m_nFileSize = ftell(m_pFile);
fseek(m_pFile, 0, SEEK_SET);
int pos = m_strFileName.find_last_of(L".");
if (pos != m_strFileName.npos) {
#ifdef _WIN32
wstring tmp = m_strFileName.substr(pos + 1);
// int nLen = MultiByteToWideChar(CP_ACP, 0, tmp.c_str(), -1, NULL, NULL);
// LPWSTR lpszW = new WCHAR[nLen];
// MultiByteToWideChar(CP_ACP, 0,
// tmp.c_str(), -1, lpszW, nLen);
m_strFileExt = tmp;
#else
m_strFileExt = L"7z";
#endif
}
wprintf(L"Ext:%ls\n", m_strFileExt.c_str());
}
else {
wprintf(L"fileName.c_str(): %s cant open\n", fileName.c_str());
}
}
virtual ~TestInStream()
{
fclose(m_pFile);
}
public:
virtual wstring GetExt() const
{
wprintf(L"GetExt:%ls\n", m_strFileExt.c_str());
return m_strFileExt;
}
virtual int Read(void *data, unsigned int size, unsigned int *processedSize)
{
if (!m_pFile)
return 1;
int count = fread(data, 1, size, m_pFile);
wprintf(L"Read:%d %d\n", size, count);
if (count >= 0) {
if (processedSize != NULL)
*processedSize = count;
return 0;
}
return 1;
}
virtual int Seek(__int64 offset, unsigned int seekOrigin, unsigned __int64 *newPosition)
{
if (!m_pFile)
return 1;
int result = fseek(m_pFile, (long)offset, seekOrigin);
if (!result) {
if (newPosition)
*newPosition = ftell(m_pFile);
return 0;
}
return result;
}
virtual int GetSize(unsigned __int64 * size)
{
if (size)
*size = m_nFileSize;
return 0;
}
};
class TestOutStream : public C7ZipOutStream
{
private:
FILE * m_pFile;
wstring m_strFileName;
wstring m_strFileExt;
int m_nFileSize;
public:
TestOutStream(wstring fileName) :
m_strFileName(fileName),
m_strFileExt(L"7z")
{
wprintf(L"OutStream ctor\n");
m_nFileSize=0;
return;
m_pFile = _wfopen(fileName.c_str(), L"wb");
m_nFileSize = 0;
int pos = m_strFileName.find_last_of(L".");
if (pos != m_strFileName.npos)
{
#ifdef _WIN32
wstring tmp = m_strFileName.substr(pos + 1);
// int nLen = MultiByteToWideChar(CP_ACP, 0, tmp.c_str(), -1, NULL, NULL);
// LPWSTR lpszW = new WCHAR[nLen];
// MultiByteToWideChar(CP_ACP, 0,
// tmp.c_str(), -1, lpszW, nLen);
m_strFileExt = tmp;
#else
m_strFileExt = L"7z";
#endif
}
wprintf(L"Ext:%ls\n", m_strFileExt.c_str());
}
virtual ~TestOutStream()
{
wprintf(L"OutStream dtor\n");
return;
fclose(m_pFile);
}
public:
int GetFileSize() const
{
wprintf(L"OutStream GetFileSize %d\n",m_nFileSize);
return m_nFileSize;
}
virtual int Write(const void *data, unsigned int size, unsigned int *processedSize)
{
wprintf(L"Write:%d\n", size);
m_nFileSize+=size;
return 0;
int count = fwrite(data, 1, size, m_pFile);
if (count >= 0)
{
if (processedSize != NULL)
*processedSize = count;
m_nFileSize += count;
return 0;
}
return 1;
}
virtual int Seek(__int64 offset, unsigned int seekOrigin, unsigned __int64 *newPosition)
{
wprintf(L"OutStream Seek:%ld %u\n", offset,seekOrigin);
return 0;
int result = fseek(m_pFile, (long)offset, seekOrigin);
if (!result)
{
if (newPosition)
*newPosition = ftell(m_pFile);
return 0;
}
return result;
}
virtual int SetSize(unsigned __int64 size)
{
wprintf(L"OutStream SetFileSize:%ld\n", size);
return 0;
}
};
// can also serve as instream
class MemOutStream : public C7ZipOutStream
{
private:
char * m_pBuf;
char * m_p;
int m_nFileSize;
int m_nBufSize;
public:
MemOutStream():m_pBuf(0),m_p(0),m_nBufSize(0),m_nFileSize(0){}
virtual ~MemOutStream()
{
if(m_pBuf) delete[] m_pBuf;
}
public:
void SetBufferSize(unsigned __int64 size)
{
if(m_pBuf) delete[] m_pBuf;
m_pBuf=new char[size];
m_nBufSize=size;
m_p=m_pBuf;
//m_nFileSize=size;
}
int GetFileSize() const
{
wprintf(L"OutStream GetFileSize %d\n",m_nFileSize);
return m_nFileSize;
}
void Read(LPVOID Buffer,DWORD BufferLength,LPDWORD ReadLength,LONGLONG Offset)
{
if(Offset>=m_nBufSize) {*ReadLength=0;return;}
*ReadLength=min(BufferLength,m_nBufSize-Offset);
memcpy(Buffer,m_pBuf+Offset,*ReadLength);
}
virtual int Write(const void *data, unsigned int size, unsigned int *processedSize)
{
wprintf(L"Write:%d\n", size);
memcpy(m_p,data,size);
*processedSize=size;
m_nFileSize+=size;
m_p+=size;
return 0;
}
virtual int Seek(__int64 offset, unsigned int seekOrigin, unsigned __int64 *newPosition)
{
wprintf(L"OutStream Seek:%ld %u\n", offset,seekOrigin);
switch(seekOrigin)
{
case SEEK_CUR:
m_p+=offset;
break;
case SEEK_END:
m_p=m_pBuf+m_nBufSize+offset;
break;
case SEEK_SET:
m_p=m_pBuf+offset;
break;
}
*newPosition=m_p-m_pBuf;
return 0;
}
virtual int SetSize(unsigned __int64 size)
{
wprintf(L"OutStream SetFileSize:%ld\n", size);
return 0;
}
};
enum PropTypeEnum {
kpUInt64,
kpBool,
kpString,
kpTime,
kpUnknown
};
struct VariantProp
{
PropTypeEnum type;
union{
//std::wstring sVal;
unsigned __int64 iVal;
bool bVal;
unsigned __int64 tVal;
};
};
void print_archive_prop(int index,VariantProp& val,wstring& sVal);
bool get_archive_prop(C7ZipArchiveItem* pArchiveItem,lib7zip::PropertyIndexEnum index,VariantProp& val,wstring& sVal);
bool get_archive_prop(C7ZipArchive* pArchiveItem,lib7zip::PropertyIndexEnum index,VariantProp& val,wstring& sVal);
wstring& to_lower(wstring& src);
wstring& to_upper(wstring& src);
wstring get_file_ext(wstring& path);
typedef C7ZipArchiveItem* A7ZIP_ITEM_HANDLE;
typedef C7ZipArchive* A7ZIP_ARCHIVE_HANDLE;
class C7ZipExplore
{
public:
C7ZipExplore();
~C7ZipExplore();
A7ZIP_ITEM_HANDLE find(A7ZIP_ARCHIVE_HANDLE pArchive,const wchar_t* fname);
A7ZIP_ARCHIVE_HANDLE open_archive(TestInStream* stream);
bool is_supported_archive(wstring& path);
private:
A7ZIP_ITEM_HANDLE find_file_recur(const wchar_t* fname,int namelen,A7ZIP_ITEM_HANDLE file);
C7ZipLibrary lib;
WStringArray supported_exts;
};
#endif