-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef48a4b
commit 94be711
Showing
12 changed files
with
263 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "BinaryWrapper.h" | ||
|
||
BinaryWrapper::BinaryWrapper(const std::string& path) : mPath(path) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <string> | ||
#include <mutex> | ||
|
||
class BinaryWrapper { | ||
public: | ||
BinaryWrapper() {} | ||
BinaryWrapper(const std::string& path); | ||
virtual ~BinaryWrapper() = default; | ||
|
||
virtual int32_t CreateArchive(void) = 0; | ||
virtual bool CreateFile(const std::string& path, std::vector<char> data) = 0; | ||
virtual int32_t Close(void) = 0; | ||
protected: | ||
std::mutex mMutex; | ||
std::string mPath; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "ZWrapper.h" | ||
#include <filesystem> | ||
#include <iostream> | ||
#include <fstream> | ||
|
||
#include "spdlog/spdlog.h" | ||
#include <Companion.h> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
ZWrapper::ZWrapper(const std::string& path) { | ||
mPath = path; | ||
} | ||
|
||
int32_t ZWrapper::CreateArchive(void) { | ||
int openErr; | ||
zip_t* zip; | ||
|
||
if(fs::exists(mPath)) { | ||
fs::remove(mPath); | ||
} | ||
|
||
{ | ||
const std::lock_guard<std::mutex> lock(mMutex); | ||
zip = zip_open(mPath.c_str(), ZIP_CREATE, &openErr); | ||
} | ||
|
||
if (zip == nullptr) { | ||
zip_error_t error; | ||
zip_error_init_with_code(&error, openErr); | ||
SPDLOG_ERROR("Failed to create ZIP (O2R) file. Error: {}", zip_error_strerror(&error)); | ||
zip_error_fini(&error); | ||
return -1; | ||
} | ||
mZip = zip; | ||
SPDLOG_INFO("Loaded ZIP (O2R) archive: {}", mPath.c_str()); | ||
return 0; | ||
} | ||
|
||
bool ZWrapper::CreateFile(const std::string& path, std::vector<char> data) { | ||
char* fileData = data.data(); | ||
size_t fileSize = data.size(); | ||
zip_source_t* source = zip_source_buffer(mZip, fileData, fileSize, 0); | ||
|
||
if (source == nullptr) { | ||
zip_error_t* zipError = zip_get_error(mZip); | ||
SPDLOG_ERROR("Failed to create ZIP source. Error: {}", zip_error_strerror(zipError)); | ||
zip_source_free(source); | ||
zip_error_fini(zipError); | ||
return false; | ||
} | ||
|
||
if (zip_file_add(mZip, path.c_str(), source, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8) < 0) { | ||
zip_error_t* zipError = zip_get_error(mZip); | ||
SPDLOG_ERROR("Failed to add file to ZIP. Error: {}", zip_error_strerror(zipError)); | ||
zip_source_free(source); | ||
zip_error_fini(zipError); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int32_t ZWrapper::Close(void) { | ||
int err; | ||
{ | ||
const std::lock_guard<std::mutex> lock(mMutex); | ||
err = zip_close(mZip); | ||
} | ||
if (err < 0) { | ||
zip_error_t* zipError = zip_get_error(mZip); | ||
SPDLOG_ERROR("Failed to close ZIP (O2R) file. Error: {}", zip_error_strerror(zipError)); | ||
printf("fail\n"); | ||
zip_error_fini(zipError); | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.