-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.cpp
288 lines (270 loc) · 7.3 KB
/
main.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
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
#include "zarchive/zarchivewriter.h"
#include "zarchive/zarchivereader.h"
#include <vector>
#include <fstream>
#include <filesystem>
#include <cassert>
#include <optional>
#include <stdio.h>
namespace fs = std::filesystem;
void PrintHelp()
{
puts("Usage:\n");
puts("zarchive.exe input_path [output_path]");
puts("If input_path is a directory, then output_path will be the ZArchive output file path");
puts("If input_path is a ZArchive file path, then output_path will be the output directory");
puts("output_path is optional");
}
bool ExtractFile(ZArchiveReader* reader, std::string_view srcPath, const fs::path& path)
{
ZArchiveNodeHandle fileHandle = reader->LookUp(srcPath, true, false);
if (fileHandle == ZARCHIVE_INVALID_NODE)
{
puts("Unable to extract file:");
puts(std::string(srcPath).c_str());
return false;
}
std::vector<uint8_t> buffer;
buffer.resize(64 * 1024);
std::ofstream fileOut(path, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
if (!fileOut.is_open())
{
puts("Unable to write file:");
puts(path.generic_string().c_str());
}
uint64_t readOffset = 0;
while (true)
{
uint64_t bytesRead = reader->ReadFromFile(fileHandle, readOffset, buffer.size(), buffer.data());
if (bytesRead == 0)
break;
fileOut.write((const char*)buffer.data(), bytesRead);
readOffset += bytesRead;
}
if (readOffset != reader->GetFileSize(fileHandle))
return false;
return true;
}
bool ExtractRecursive(ZArchiveReader* reader, std::string srcPath, fs::path outputDirectory)
{
ZArchiveNodeHandle dirHandle = reader->LookUp(srcPath, false, true);
if (dirHandle == ZARCHIVE_INVALID_NODE)
return false;
std::error_code ec;
fs::create_directories(outputDirectory);
uint32_t numEntries = reader->GetDirEntryCount(dirHandle);
for (uint32_t i = 0; i < numEntries; i++)
{
ZArchiveReader::DirEntry dirEntry;
if (!reader->GetDirEntry(dirHandle, i, dirEntry))
{
puts("Directory contains invalid node");
return false;
}
puts(std::string(srcPath).append("/").append(dirEntry.name).c_str());
if (dirEntry.isDirectory)
{
ExtractRecursive(reader, std::string(srcPath).append("/").append(dirEntry.name), outputDirectory / dirEntry.name);
}
else
{
// extract file
if (!ExtractFile(reader, std::string(srcPath).append("/").append(dirEntry.name), outputDirectory / dirEntry.name))
return false;
}
}
return true;
}
int Extract(fs::path inputFile, fs::path outputDirectory)
{
std::error_code ec;
if (!fs::exists(inputFile, ec))
{
puts("Unable to find archive file");
return -10;
}
ZArchiveReader* reader = ZArchiveReader::OpenFromFile(inputFile);
if (!reader)
{
puts("Failed to open ZArchive");
return -11;
}
bool r = ExtractRecursive(reader, "", outputDirectory);
if (!r)
{
puts("Extraction failed");
delete reader;
return -12;
}
delete reader;
return 0;
}
struct PackContext
{
fs::path outputFilePath;
std::ofstream currentOutputFile;
bool hasError{false};
};
void _pack_NewOutputFile(const int32_t partIndex, void* ctx)
{
PackContext* packContext = (PackContext*)ctx;
packContext->currentOutputFile = std::ofstream(packContext->outputFilePath, std::ios::binary);
if (!packContext->currentOutputFile.is_open())
{
printf("Failed to create output file: %s\n", packContext->outputFilePath.string().c_str());
packContext->hasError = true;
}
}
void _pack_WriteOutputData(const void* data, size_t length, void* ctx)
{
PackContext* packContext = (PackContext*)ctx;
packContext->currentOutputFile.write((const char*)data, length);
}
int Pack(fs::path inputDirectory, fs::path outputFile)
{
std::vector<uint8_t> buffer;
buffer.resize(64 * 1024);
std::error_code ec;
PackContext packContext;
packContext.outputFilePath = outputFile;
ZArchiveWriter zWriter(_pack_NewOutputFile, _pack_WriteOutputData, &packContext);
if (packContext.hasError)
return -16;
for (auto const& dirEntry : fs::recursive_directory_iterator(inputDirectory))
{
fs::path pathEntry = fs::relative(dirEntry.path(), inputDirectory, ec);
if (dirEntry.is_directory())
{
if (!zWriter.MakeDir(pathEntry.generic_string().c_str(), false))
{
printf("Failed to create directory %s\n", pathEntry.string().c_str());
return -13;
}
}
else if (dirEntry.is_regular_file())
{
if (dirEntry == outputFile) {
continue;
}
printf("Adding %s\n", pathEntry.string().c_str());
if (!zWriter.StartNewFile(pathEntry.generic_string().c_str()))
{
printf("Failed to create archive file %s\n", pathEntry.string().c_str());
return -14;
}
std::ifstream inputFile(inputDirectory / pathEntry, std::ios::binary);
if (!inputFile.is_open())
{
printf("Failed to open input file %s\n", pathEntry.string().c_str());
return -15;
}
while( true )
{
inputFile.read((char*)buffer.data(), buffer.size());
int32_t readBytes = (int32_t)inputFile.gcount();
if (readBytes <= 0)
break;
zWriter.AppendData(buffer.data(), readBytes);
}
}
if (packContext.hasError)
return -16;
}
zWriter.Finalize();
return 0;
}
int main(int argc, char* argv[])
{
if (argc <= 1)
{
PrintHelp();
return 0;
}
std::optional<std::string> strInput;
std::optional<std::string> strOutput;
for (int i = 1; i < argc; i++)
{
if (strInput)
{
if (strOutput)
{
puts("Too many paths specified");
return -1;
}
else
{
strOutput = argv[i];
}
}
else
{
strInput = argv[i];
}
}
if (strInput)
{
std::error_code ec;
fs::path p(*strInput);
if (fs::is_regular_file(p, ec))
{
// extract
fs::path outputDirectory;
if (!strOutput)
{
fs::path defaultOutputPath = p.parent_path() / (p.stem().filename().string().append("_extracted"));
outputDirectory = defaultOutputPath;
printf("Extracting to: %s\n", outputDirectory.generic_string().c_str());
}
else
outputDirectory = *strOutput;
if (fs::exists(outputDirectory, ec) && !fs::is_directory(outputDirectory, ec))
{
puts("The specified output path is not a valid directory");
return -3;
}
fs::create_directories(outputDirectory, ec);
if (!fs::exists(outputDirectory, ec))
{
puts("Failed to create output directory");
return -4;
}
return Extract(p, outputDirectory);
}
else if(fs::is_directory(p, ec))
{
// pack
fs::path outputFile;
if (!strOutput)
{
fs::path defaultOutputPath = p.parent_path() / (p.stem().filename().string().append(".zar"));
outputFile = defaultOutputPath;
printf("Outputting to: %s\n", outputFile.generic_string().c_str());
}
else
outputFile = *strOutput;
if ((fs::exists(outputFile, ec) && !fs::is_regular_file(outputFile, ec)))
{
puts("The specified output path is not a valid file");
return -10;
}
if ((fs::exists(outputFile, ec) && fs::is_regular_file(outputFile, ec)))
{
puts("The output file already exists");
return -11;
}
int r = Pack(p, outputFile);
if (r != 0)
{
// delete incomplete output file
fs::remove(outputFile, ec);
}
return r;
}
else
{
puts("Input path is not a valid file or directory");
return -1;
}
}
return 0;
}