add read from zip

This commit is contained in:
Vlad committed 2026-01-09 20:08:09 +06:00
1 parent b055faf882
commit 112684b40a
6 files changed
+124 -45

No files matched your search

+69 -18
View File
@@ -5,9 +5,11 @@
#include <iostream>
#include <algorithm>
#include <fstream>
#ifdef EMSCRIPTEN
#include <zip.h>
#endif
#include <cstdlib>
#include <filesystem>
#include <string>
#include <cctype>
namespace ZL
{
@@ -34,7 +36,7 @@ namespace ZL
file.seekg(0, std::ios::beg);
std::vector<char> vec;
vec.reserve(fileSize);
vec.reserve(static_cast<size_t>(fileSize));
vec.insert(vec.begin(),
std::istream_iterator<char>(file),
@@ -42,47 +44,96 @@ namespace ZL
return vec;
}
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);
throw std::runtime_error("?????? ???????? ZIP: " + zipPath);
}
std::string cleanFilename = filename;
if (cleanFilename.rfind("./", 0) == 0) {
cleanFilename = cleanFilename.substr(2);
}
std::cout << "Ищем в ZIP: " << cleanFilename << std::endl;
std::cout << "???? ? ZIP: " << cleanFilename << std::endl;
zip_file_t* zipFile = zip_fopen(archive, cleanFilename.c_str(), 0);
if (!zipFile) {
zip_close(archive);
throw std::runtime_error("Файл не найден в ZIP: " + cleanFilename);
throw std::runtime_error("???? ?? ?????? ? 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-статистики.");
throw std::runtime_error("?????? ?????? ZIP-??????????.");
}
std::vector<char> fileData;
fileData.resize(fileStat.size);
zip_fread(zipFile, fileData.data(), fileData.size());
zip_fclose(zipFile);
zip_close(archive);
return fileData;
#else
return {};
// ???????
if (zipfilename.empty()) {
std::cerr << "readFileFromZIP: zipfilename empty" << std::endl;
return {};
}
int zipErr = 0;
zip_t* archive = zip_open(zipfilename.c_str(), ZIP_RDONLY, &zipErr);
if (!archive) {
std::cerr << "readFileFromZIP: zip_open failed for: " << zipfilename
<< " (error code " << zipErr << ")" << std::endl;
return {};
}
std::string cleanFilename = filename;
if (cleanFilename.rfind("./", 0) == 0) {
cleanFilename = cleanFilename.substr(2);
}
zip_stat_t fileStat;
if (zip_stat(archive, cleanFilename.c_str(), 0, &fileStat) != 0) {
std::cerr << "readFileFromZIP: file not found in ZIP: " << cleanFilename
<< " in " << zipfilename << std::endl;
zip_close(archive);
return {};
}
zip_file_t* zipFile = zip_fopen(archive, cleanFilename.c_str(), 0);
if (!zipFile) {
std::cerr << "readFileFromZIP: zip_fopen failed for " << cleanFilename << std::endl;
zip_close(archive);
return {};
}
std::vector<char> fileData;
fileData.resize(static_cast<size_t>(fileStat.size));
zip_int64_t readBytes = zip_fread(zipFile, fileData.data(), static_cast<zip_uint64_t>(fileData.size()));
if (readBytes < 0) {
std::cerr << "readFileFromZIP: zip_fread failed for " << cleanFilename << std::endl;
zip_fclose(zipFile);
zip_close(archive);
return {};
}
zip_fclose(zipFile);
zip_close(archive);
return fileData;
#endif
}
@@ -102,4 +153,4 @@ namespace ZL
}
return false;
}
};
};