Adapting for web

This commit is contained in:
Vladislav Khorev committed 2026-02-08 22:38:24 +03:00
1 parent cf1ec1e76f
commit a59bcc0c4b
42 files changed
+843 -294

No files matched your search

+32 -31
View File
@@ -1,4 +1,4 @@
#include "utils/Utils.h"
#include "utils/Utils.h"
#include <cstring>
#include <iterator>
#include <vector>
@@ -98,44 +98,45 @@ namespace ZL
std::vector<char> readFileFromZIP(const std::string& filename, const std::string& zipfilename) {
#ifdef EMSCRIPTEN
const std::string zipPath = zipfilename;
int zipErr;
zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &zipErr);
if (!archive) {
throw std::runtime_error("?????? ???????? ZIP: " + zipPath);
}
int zipErr = 0;
zip_t* archive = zip_open(zipfilename.c_str(), ZIP_RDONLY, &zipErr);
if (!archive) {
std::cout << "Failed to open ZIP: " << zipfilename << " (error code " << zipErr << ")" << std::endl;
throw std::runtime_error("Failed to open ZIP: " + zipfilename);
}
std::string cleanFilename = filename;
if (cleanFilename.rfind("./", 0) == 0) {
cleanFilename = cleanFilename.substr(2);
}
std::string cleanFilename = filename;
if (cleanFilename.rfind("./", 0) == 0) cleanFilename = cleanFilename.substr(2);
std::cout << "???? ? ZIP: " << cleanFilename << std::endl;
zip_stat_t fileStat;
zip_stat_init(&fileStat);
if (zip_stat(archive, cleanFilename.c_str(), 0, &fileStat) != 0 || !(fileStat.valid & ZIP_STAT_SIZE)) {
zip_close(archive);
std::cout << "Failed to stat file in ZIP: " << cleanFilename << " in " << zipfilename << std::endl;
throw std::runtime_error("Can't stat file in ZIP: " + cleanFilename);
}
zip_file_t* zipFile = zip_fopen(archive, cleanFilename.c_str(), 0);
if (!zipFile) {
zip_close(archive);
throw std::runtime_error("???? ?? ?????? ? ZIP: " + cleanFilename);
}
zip_file_t* zipFile = zip_fopen(archive, cleanFilename.c_str(), 0);
if (!zipFile) {
zip_close(archive);
std::cout << "Failed to open file in ZIP: " << cleanFilename << std::endl;
throw std::runtime_error("Can't open file in ZIP: " + cleanFilename);
}
zip_stat_t fileStat;
if (zip_stat(archive, cleanFilename.c_str(), 0, &fileStat) != 0) {
zip_fclose(zipFile);
zip_close(archive);
throw std::runtime_error("?????? ?????? ZIP-??????????.");
}
std::vector<char> fileData(static_cast<size_t>(fileStat.size));
zip_int64_t bytesRead = zip_fread(zipFile, fileData.data(), fileStat.size);
std::vector<char> fileData;
fileData.resize(fileStat.size);
// Обязательно закрываем всё перед проверкой результата или throw
zip_fclose(zipFile);
zip_close(archive);
zip_fread(zipFile, fileData.data(), fileData.size());
if (bytesRead < 0 || static_cast<zip_uint64_t>(bytesRead) != fileStat.size) {
std::cout << "Failed to read file from ZIP: " << cleanFilename << " (bytes read: " << bytesRead << ", expected: " << fileStat.size << ")" << std::endl;
throw std::runtime_error("Error reading data from ZIP: " + cleanFilename);
}
zip_fclose(zipFile);
zip_close(archive);
return fileData;
return fileData;
#else
// ???????
if (zipfilename.empty()) {
std::cerr << "readFileFromZIP: zipfilename empty" << std::endl;