This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathArchiveReader.cpp
More file actions
125 lines (109 loc) · 2.64 KB
/
ArchiveReader.cpp
File metadata and controls
125 lines (109 loc) · 2.64 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
#include "stdafx.h"
#include "ArchiveReader.h"
#include <string.h>
#include <sstream>
#include <algorithm>
#include "FolderUtilities.h"
#include "ZipReader.h"
#include "SZReader.h"
ArchiveReader::~ArchiveReader()
{
if(_buffer) {
delete[] _buffer;
_buffer = nullptr;
}
}
bool ArchiveReader::GetStream(string filename, std::stringstream &stream)
{
if(_initialized) {
vector<uint8_t> fileData;
if(ExtractFile(filename, fileData)) {
stream.write((char*)fileData.data(), fileData.size());
return true;
}
}
return false;
}
vector<string> ArchiveReader::GetFileList(std::initializer_list<string> extensions)
{
if(extensions.size() == 0) {
return InternalGetFileList();
}
vector<string> filenames;
for(string filename : InternalGetFileList()) {
string lcFilename = filename;
std::transform(lcFilename.begin(), lcFilename.end(), lcFilename.begin(), ::tolower);
for(string ext : extensions) {
if(lcFilename.size() >= ext.size()) {
if(lcFilename.substr(lcFilename.length() - ext.size(), ext.size()).compare(ext) == 0) {
filenames.push_back(filename);
}
}
}
}
return filenames;
}
bool ArchiveReader::CheckFile(string filename)
{
vector<string> files = InternalGetFileList();
return std::find(files.begin(), files.end(), filename) != files.end();
}
bool ArchiveReader::LoadArchive(std::istream &in)
{
in.seekg(0, std::ios::end);
std::streampos filesize = in.tellg();
in.seekg(0, std::ios::beg);
if(_buffer) {
delete[] _buffer;
_buffer = nullptr;
}
_buffer = new uint8_t[(uint32_t)filesize];
in.read((char*)_buffer, filesize);
in.seekg(0, std::ios::beg);
bool result = LoadArchive(_buffer, (size_t)filesize);
return result;
}
bool ArchiveReader::LoadArchive(vector<uint8_t> &data)
{
return LoadArchive(data.data(), data.size());
}
bool ArchiveReader::LoadArchive(void* buffer, size_t size)
{
if(InternalLoadArchive(buffer, size)) {
_initialized = true;
return true;
}
return false;
}
bool ArchiveReader::LoadArchive(string filename)
{
ifstream in(filename, std::ios::binary | std::ios::in);
if(in.good()) {
LoadArchive(in);
in.close();
}
return false;
}
shared_ptr<ArchiveReader> ArchiveReader::GetReader(std::istream &in)
{
uint8_t header[2] = { 0,0 };
in.read((char*)header, 2);
shared_ptr<ArchiveReader> reader;
if(memcmp(header, "PK", 2) == 0) {
reader.reset(new ZipReader());
} else if(memcmp(header, "7z", 2) == 0) {
reader.reset(new SZReader());
}
if(reader) {
reader->LoadArchive(in);
}
return reader;
}
shared_ptr<ArchiveReader> ArchiveReader::GetReader(string filepath)
{
ifstream in(filepath, std::ios::in | std::ios::binary);
if(in) {
return GetReader(in);
}
return nullptr;
}