mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-03-22 01:34:13 -05:00
54 lines
2.0 KiB
C++
54 lines
2.0 KiB
C++
#include "fs/MiniZip.hpp"
|
|
|
|
#include "config.hpp"
|
|
#include "error.hpp"
|
|
|
|
#include <ctime>
|
|
|
|
fs::MiniZip::MiniZip(const fslib::Path &path) { MiniZip::open(path); }
|
|
|
|
fs::MiniZip::~MiniZip() { MiniZip::close(); }
|
|
|
|
bool fs::MiniZip::open(const fslib::Path &path)
|
|
{
|
|
MiniZip::close();
|
|
m_zip = zipOpen64(path.full_path(), APPEND_STATUS_CREATE);
|
|
if (error::is_null(m_zip)) { return false; }
|
|
m_isOpen = true;
|
|
return true;
|
|
}
|
|
|
|
void fs::MiniZip::close()
|
|
{
|
|
if (!m_isOpen) { return; }
|
|
zipClose(m_zip, nullptr) == ZIP_OK;
|
|
m_isOpen = false;
|
|
}
|
|
|
|
bool fs::MiniZip::open_new_file(std::string_view filename, bool trimPath, size_t trimPlaces)
|
|
{
|
|
const uint8_t zipLevel = config::get_by_key(config::keys::ZIP_COMPRESSION_LEVEL);
|
|
|
|
const size_t pathBegin = filename.find_first_of('/');
|
|
if (pathBegin != filename.npos) { filename = filename.substr(pathBegin + 1); }
|
|
|
|
const std::time_t currentTime = std::time(nullptr);
|
|
const std::tm *local = std::localtime(¤tTime);
|
|
const zip_fileinfo fileInfo = {.tmz_date = {.tm_sec = local->tm_sec,
|
|
.tm_min = local->tm_min,
|
|
.tm_hour = local->tm_hour,
|
|
.tm_mday = local->tm_mday,
|
|
.tm_mon = local->tm_mon,
|
|
.tm_year = local->tm_year + 1900},
|
|
.dosDate = 0,
|
|
.internal_fa = 0,
|
|
.external_fa = 0};
|
|
|
|
return zipOpenNewFileInZip64(m_zip, filename.data(), &fileInfo, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, zipLevel, 0) ==
|
|
ZIP_OK;
|
|
}
|
|
|
|
bool fs::MiniZip::close_current_file() { return zipCloseFileInZip(m_zip) == ZIP_OK; }
|
|
|
|
bool fs::MiniZip::write(const void *buffer, size_t dataSize) { return zipWriteInFileInZip(m_zip, buffer, dataSize) == ZIP_OK; }
|