19 const std::string& destPath) {
21 zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &error);
25 zip_error_init_with_code(&ziperror, error);
26 std::string msg =
"Failed to open ZIP: " + std::string(zip_error_strerror(&ziperror));
27 zip_error_fini(&ziperror);
32 zip_int64_t numEntries = zip_get_num_entries(archive, 0);
35 return Result(
false,
"Failed to get entry count");
39 if (!CreateDirectories(destPath)) {
41 return Result(
false,
"Failed to create destination directory: " + destPath);
45 for (zip_int64_t i = 0; i < numEntries; i++) {
49 if (zip_stat_index(archive, i, 0, &st) != 0) {
51 return Result(
false,
"Failed to stat file at index " + std::to_string(i));
54 std::string filename = st.name;
57 if (filename.back() ==
'/') {
60 CreateDirectories(dirPath);
68 size_t lastSlash = destFile.find_last_of(
"/\\");
69 if (lastSlash != std::string::npos) {
70 std::string dirPath = destFile.substr(0, lastSlash);
71 if (!CreateDirectories(dirPath)) {
73 return Result(
false,
"Failed to create directory: " + dirPath);
78 zip_file_t* file = zip_fopen_index(archive, i, 0);
81 return Result(
false,
"Failed to open file in ZIP: " + filename);
85 std::vector<char> buffer(st.size);
86 zip_int64_t bytesRead = zip_fread(file, buffer.data(), st.size);
89 if (bytesRead !=
static_cast<zip_int64_t
>(st.size)) {
91 return Result(
false,
"Failed to read file: " + filename);
95 std::ofstream out(destFile, std::ios::binary);
98 return Result(
false,
"Failed to create file: " + destFile);
100 out.write(buffer.data(), bytesRead);
110 const std::string& fileInZip,
111 const std::string& destPath) {
113 zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &error);
116 return Result(
false,
"Failed to open ZIP");
120 zip_int64_t index = zip_name_locate(archive, fileInZip.c_str(), 0);
123 return Result(
false,
"File not found in ZIP: " + fileInZip);
129 if (zip_stat_index(archive, index, 0, &st) != 0) {
131 return Result(
false,
"Failed to stat file");
135 zip_file_t* file = zip_fopen_index(archive, index, 0);
138 return Result(
false,
"Failed to open file");
141 std::vector<char> buffer(st.size);
142 zip_int64_t bytesRead = zip_fread(file, buffer.data(), st.size);
146 if (bytesRead !=
static_cast<zip_int64_t
>(st.size)) {
147 return Result(
false,
"Failed to read file");
151 size_t lastSlash = destPath.find_last_of(
"/\\");
152 if (lastSlash != std::string::npos) {
153 std::string dirPath = destPath.substr(0, lastSlash);
154 if (!CreateDirectories(dirPath)) {
155 return Result(
false,
"Failed to create directory: " + dirPath);
160 std::ofstream out(destPath, std::ios::binary);
162 return Result(
false,
"Failed to create file: " + destPath);
164 out.write(buffer.data(), bytesRead);
172 const std::string& fileInZip,
173 std::string& outContents) {
175 zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &error);
178 return Result(
false,
"Failed to open ZIP");
182 zip_int64_t index = zip_name_locate(archive, fileInZip.c_str(), 0);
185 return Result(
false,
"File not found in ZIP: " + fileInZip);
191 if (zip_stat_index(archive, index, 0, &st) != 0) {
193 return Result(
false,
"Failed to stat file");
197 zip_file_t* file = zip_fopen_index(archive, index, 0);
200 return Result(
false,
"Failed to open file");
203 std::vector<char> buffer(st.size + 1);
204 zip_int64_t bytesRead = zip_fread(file, buffer.data(), st.size);
208 if (bytesRead !=
static_cast<zip_int64_t
>(st.size)) {
209 return Result(
false,
"Failed to read file");
212 buffer[st.size] =
'\0';
213 outContents = std::string(buffer.data());
220 std::vector<std::string>& outFiles) {
222 zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &error);
225 return Result(
false,
"Failed to open ZIP");
228 zip_int64_t numEntries = zip_get_num_entries(archive, 0);
229 if (numEntries < 0) {
231 return Result(
false,
"Failed to get entry count");
235 for (zip_int64_t i = 0; i < numEntries; i++) {
236 const char* name = zip_get_name(archive, i, 0);
238 outFiles.push_back(name);
265 zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY | ZIP_CHECKCONS, &error);
268 zip_error_t ziperror;
269 zip_error_init_with_code(&ziperror, error);
270 std::string msg = std::string(zip_error_strerror(&ziperror));
271 zip_error_fini(&ziperror);
272 return Result(
false,
"Invalid or corrupted ZIP: " + msg);