Extraction should be working better now

This commit is contained in:
Epicpkmn11
2019-12-20 15:36:43 -06:00
parent 0a308c43e8
commit ccf19f777d
+17 -19
View File
@@ -28,41 +28,39 @@
#include <archive.h> #include <archive.h>
#include <archive_entry.h> #include <archive_entry.h>
#include <regex>
int filesExtracted = 0; int filesExtracted = 0;
std::string extractingFile = ""; std::string extractingFile = "";
Result extractArchive(std::string archivePath, std::string wantedFile, std::string outputPath) Result extractArchive(std::string archivePath, std::string wantedFile, std::string outputPath) {
{ archive_entry *entry;
int r;
struct archive_entry *entry;
struct archive *a = archive_read_new(); archive *a = archive_read_new();
archive_read_support_format_all(a); archive_read_support_format_all(a);
archive_read_support_format_raw(a); archive_read_support_format_raw(a);
r = archive_read_open_filename(a, archivePath.c_str(), 0x4000); if(archive_read_open_filename(a, archivePath.c_str(), 0x4000) != ARCHIVE_OK) {
if (r != ARCHIVE_OK) {
return EXTRACT_ERROR_ARCHIVE; return EXTRACT_ERROR_ARCHIVE;
} }
Result ret = EXTRACT_ERROR_FIND; Result ret = EXTRACT_ERROR_FIND;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { while(archive_read_next_header(a, &entry) == ARCHIVE_OK) {
std::string entryName(archive_entry_pathname(entry)); std::string entryName(archive_entry_pathname(entry));
if (wantedFile == "/") wantedFile = ""; std::smatch match;
if (matchPattern(wantedFile, entryName) || matchPattern(wantedFile, entryName.substr(0, wantedFile.size())) || wantedFile == "") { if(std::regex_search(entryName, match, std::regex(wantedFile))) {
extractingFile = (entryName.length() > wantedFile.length() ? entryName.substr(wantedFile.length()) : wantedFile);
ret = EXTRACT_ERROR_NONE; ret = EXTRACT_ERROR_NONE;
Handle fileHandle; // make directories
std::string outputPathFinal = outputPath; std::string out = (outputPath + match.suffix().str());
if(wantedFile[wantedFile.length()-1] == '/') { // Folder int substrPos = 1;
if (entryName.length() > wantedFile.length()) while(out.find("/", substrPos)) {
outputPathFinal += entryName.substr(wantedFile.length()); mkdir(out.substr(0, substrPos).c_str(), 0777);
if (outputPathFinal.substr(outputPathFinal.length()-1) == "/") continue; substrPos = out.find("/", substrPos)+1;
} }
Result res = openFile(&fileHandle, outputPathFinal.c_str(), true);
Handle fileHandle;
Result res = openFile(&fileHandle, (outputPath + match.suffix().str()).c_str(), true);
if (R_FAILED(res)) { if (R_FAILED(res)) {
ret = EXTRACT_ERROR_OPENFILE; ret = EXTRACT_ERROR_OPENFILE;
break; break;