| 516 | } |
| 517 | |
| 518 | void |
| 519 | ZipArchive::AddResourceFile(std::string const& resFileName, bool isManifest) |
| 520 | { |
| 521 | std::string archiveName = resFileName; |
| 522 | |
| 523 | bool pathToResIsAbsolute = false; |
| 524 | #ifndef __MINGW32__ |
| 525 | // Issue 161.3: check to see if resFileName is relative or not, and exit early if it is not. |
| 526 | std::filesystem::path pathToResFile { resFileName }; |
| 527 | pathToResIsAbsolute = pathToResFile.is_absolute(); |
| 528 | #else |
| 529 | pathToResIsAbsolute = !PathIsRelativeA(resFileName.c_str()); |
| 530 | #endif |
| 531 | |
| 532 | if (pathToResIsAbsolute) |
| 533 | { |
| 534 | throw std::runtime_error("Relatvie path to resource file required. " + resFileName + " is absolute"); |
| 535 | } |
| 536 | |
| 537 | // This check exists solely to maintain a deprecated way of adding manifest.json |
| 538 | // through the --res-add option. |
| 539 | if (isManifest || resFileName == std::string("manifest.json")) |
| 540 | { |
| 541 | rapidjson::Document root; |
| 542 | parseAndValidateJsonFromFile(resFileName, root); |
| 543 | } |
| 544 | |
| 545 | // if it is a manifest file, we ignore the parent directory path because the |
| 546 | // manifest file is always placed at the root of the bundle name directory |
| 547 | if (isManifest && resFileName.find_last_of(PATH_SEPARATOR) != std::string::npos) |
| 548 | { |
| 549 | archiveName = resFileName.substr(resFileName.find_last_of(PATH_SEPARATOR) + 1); |
| 550 | } |
| 551 | |
| 552 | std::string archiveEntry = bundleName + "/" + archiveName; |
| 553 | CheckAndAddToArchivedNames(archiveEntry); |
| 554 | |
| 555 | if (!mz_zip_writer_add_file(writeArchive.get(), |
| 556 | archiveEntry.c_str(), |
| 557 | resFileName.c_str(), |
| 558 | NULL, |
| 559 | 0, |
| 560 | compressionLevel)) |
| 561 | { |
| 562 | throw std::runtime_error("Error writing file to archive"); |
| 563 | } |
| 564 | // add a directory entries for the file path |
| 565 | size_t lastPathSeparatorPos = archiveEntry.find("/", 0); |
| 566 | while (lastPathSeparatorPos != std::string::npos) |
| 567 | { |
| 568 | AddDirectory(archiveEntry.substr(0, lastPathSeparatorPos + 1)); |
| 569 | lastPathSeparatorPos = archiveEntry.find("/", lastPathSeparatorPos + 1); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | ZipArchive::AddDirectory(std::string const& dirName) |
no test coverage detected