forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchive.cpp
More file actions
377 lines (325 loc) · 7.48 KB
/
Archive.cpp
File metadata and controls
377 lines (325 loc) · 7.48 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Archive.cpp
//
// $Id$
//
// Library: SevenZip
// Package: Archive
// Module: Archive
//
// Definition of the Archive class.
//
// Copyright (c) 2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/SevenZip/Archive.h"
#include "Poco/TextConverter.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/UTF16Encoding.h"
#include "Poco/UnicodeConverter.h"
#include "Poco/FileStream.h"
#include "Poco/File.h"
#include "Poco/Path.h"
#include "Poco/Mutex.h"
#include "7z.h"
#include "7zAlloc.h"
#include "7zCrc.h"
#include "7zFile.h"
namespace Poco {
namespace SevenZip {
class ArchiveImpl
{
public:
ArchiveImpl(const std::string& path):
_path(path)
{
initialize();
open();
}
~ArchiveImpl()
{
try
{
close();
}
catch (...)
{
poco_unexpected();
}
}
const std::string& path() const
{
return _path;
}
std::size_t size() const
{
return _entries.size();
}
Archive::ConstIterator begin() const
{
return _entries.begin();
}
Archive::ConstIterator end() const
{
return _entries.end();
}
std::string extract(const ArchiveEntry& entry, const std::string& destPath)
{
Poco::Path basePath;
if (destPath.empty())
{
basePath = Poco::Path::current();
}
else
{
basePath = destPath;
}
basePath.makeDirectory();
Poco::Path entryPath(entry.path(), Poco::Path::PATH_UNIX);
Poco::Path extractedPath(basePath);
extractedPath.append(entryPath);
extractedPath.makeAbsolute();
if (entry.isFile())
{
Poco::UInt32 blockIndex = 0;
Byte* pOutBuffer = 0;
std::size_t outBufferSize = 0;
std::size_t offset = 0;
std::size_t extractedSize = 0;
int err = SzArEx_Extract(
&_db,
&_lookStream.s,
entry.index(),
&blockIndex,
&pOutBuffer,
&outBufferSize,
&offset,
&extractedSize,
&_szAlloc,
&_szAllocTemp);
if (err == SZ_OK)
{
try
{
poco_assert (extractedSize == entry.size());
Poco::Path parent = extractedPath.parent();
Poco::File dir(parent.toString());
dir.createDirectories();
Poco::FileOutputStream ostr(extractedPath.toString());
ostr.write(reinterpret_cast<const char*>(pOutBuffer) + offset, extractedSize);
IAlloc_Free(&_szAlloc, pOutBuffer);
}
catch (...)
{
IAlloc_Free(&_szAlloc, pOutBuffer);
throw;
}
}
else
{
handleError(err, entry.path());
}
}
else
{
Poco::File dir(extractedPath.toString());
dir.createDirectories();
}
return extractedPath.toString();
}
protected:
void initialize()
{
FileInStream_CreateVTable(&_archiveStream);
LookToRead_CreateVTable(&_lookStream, False);
Poco::FastMutex::ScopedLock lock(_initMutex);
if (!_initialized)
{
CrcGenerateTable();
_initialized = true;
}
}
void open()
{
checkFile();
#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
std::wstring wpath;
Poco::UnicodeConverter::toUTF16(_path, wpath);
if (InFile_OpenW(&_archiveStream.file, wpath.c_str()) != SZ_OK)
{
throw Poco::OpenFileException(_path);
}
#else
if (InFile_Open(&_archiveStream.file, _path.c_str()) != SZ_OK)
{
throw Poco::OpenFileException(_path);
}
#endif
_lookStream.realStream = &_archiveStream.s;
LookToRead_Init(&_lookStream);
SzArEx_Init(&_db);
int err = SzArEx_Open(&_db, &_lookStream.s, &_szAlloc, &_szAllocTemp);
if (err == SZ_OK)
{
loadEntries();
}
else
{
handleError(err);
}
}
void close()
{
SzArEx_Free(&_db, &_szAlloc);
File_Close(&_archiveStream.file);
}
void loadEntries()
{
_entries.reserve(_db.db.NumFiles);
for (Poco::UInt32 i = 0; i < _db.db.NumFiles; i++)
{
const CSzFileItem *f = _db.db.Files + i;
ArchiveEntry::EntryType type = f->IsDir ? ArchiveEntry::ENTRY_DIRECTORY : ArchiveEntry::ENTRY_FILE;
Poco::UInt32 attributes = f->AttribDefined ? f->Attrib : 0;
Poco::UInt64 size = f->Size;
std::vector<Poco::UInt16> utf16Path;
std::size_t utf16PathLen = SzArEx_GetFileNameUtf16(&_db, i, 0);
utf16Path.resize(utf16PathLen, 0);
utf16PathLen--; // we don't need terminating 0 later on
SzArEx_GetFileNameUtf16(&_db, i, &utf16Path[0]);
Poco::UTF8Encoding utf8Encoding;
Poco::UTF16Encoding utf16Encoding;
Poco::TextConverter converter(utf16Encoding, utf8Encoding);
std::string utf8Path;
converter.convert(&utf16Path[0], (int) utf16PathLen*sizeof(Poco::UInt16), utf8Path);
Poco::Timestamp lastModified(0);
if (f->MTimeDefined)
{
Poco::Timestamp::TimeVal tv(0);
tv = (static_cast<Poco::UInt64>(f->MTime.High) << 32) + f->MTime.Low;
tv -= (static_cast<Poco::Int64>(0x019DB1DE) << 32) + 0xD53E8000;
tv /= 10;
lastModified = tv;
}
ArchiveEntry entry(type, utf8Path, size, lastModified, attributes, i);
_entries.push_back(entry);
}
}
void checkFile()
{
Poco::File f(_path);
if (!f.exists())
{
throw Poco::FileNotFoundException(_path);
}
if (!f.isFile())
{
throw Poco::OpenFileException("not a file", _path);
}
if (!f.canRead())
{
throw Poco::FileAccessDeniedException(_path);
}
}
void handleError(int err)
{
std::string arg;
handleError(err, arg);
}
void handleError(int err, const std::string& arg)
{
switch (err)
{
case SZ_ERROR_DATA:
throw Poco::DataFormatException("archive", _path, err);
case SZ_ERROR_MEM:
throw Poco::RuntimeException("7z library out of memory", err);
case SZ_ERROR_CRC:
throw Poco::DataException("CRC error", arg, err);
case SZ_ERROR_UNSUPPORTED:
throw Poco::DataException("unsupported archive format", arg, err);
case SZ_ERROR_PARAM:
throw Poco::InvalidArgumentException("7z library", err);
case SZ_ERROR_INPUT_EOF:
throw Poco::ReadFileException("EOF while reading archive", _path, err);
case SZ_ERROR_OUTPUT_EOF:
throw Poco::WriteFileException(arg, err);
case SZ_ERROR_READ:
throw Poco::ReadFileException(_path, err);
case SZ_ERROR_WRITE:
throw Poco::WriteFileException(arg, err);
default:
throw Poco::IOException("7z error", arg, err);
}
}
private:
std::string _path;
Archive::EntryVec _entries;
CFileInStream _archiveStream;
CLookToRead _lookStream;
CSzArEx _db;
static ISzAlloc _szAlloc;
static ISzAlloc _szAllocTemp;
static Poco::FastMutex _initMutex;
static bool _initialized;
};
ISzAlloc ArchiveImpl::_szAlloc = { SzAlloc, SzFree };
ISzAlloc ArchiveImpl::_szAllocTemp = { SzAlloc, SzFree };
Poco::FastMutex ArchiveImpl::_initMutex;
bool ArchiveImpl::_initialized(false);
Archive::Archive(const std::string& path):
_pImpl(new ArchiveImpl(path))
{
}
Archive::~Archive()
{
delete _pImpl;
}
const std::string& Archive::path() const
{
return _pImpl->path();
}
std::size_t Archive::size() const
{
return _pImpl->size();
}
Archive::ConstIterator Archive::begin() const
{
return _pImpl->begin();
}
Archive::ConstIterator Archive::end() const
{
return _pImpl->end();
}
void Archive::extract(const std::string& destPath)
{
for (ConstIterator it = begin(); it != end(); ++it)
{
ExtractedEventArgs extractedArgs;
extractedArgs.entry = *it;
bool success = true;
try
{
extractedArgs.extractedPath = _pImpl->extract(*it, destPath);
}
catch (Poco::Exception& exc)
{
success = false;
FailedEventArgs failedArgs;
failedArgs.entry = *it;
failedArgs.pException = &exc;
failed(this, failedArgs);
}
if (success)
{
extracted(this, extractedArgs);
}
}
}
std::string Archive::extract(const ArchiveEntry& entry, const std::string& destPath)
{
return _pImpl->extract(entry, destPath);
}
} } // namespace Poco::SevenZip