src/utilities/core/ZipFile.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "ZipFile.hpp" | ||
| 7 | #include "FilesystemHelpers.hpp" | ||
| 8 | |||
| 9 | #include <minizip/zip.h> | ||
| 10 | |||
| 11 | namespace openstudio { | ||
| 12 | |||
| 13 | 34 | ZipFile::ZipFile(const openstudio::path& filename, bool add) | |
| 14 |
4/4✓ Branch 0 taken 1 time.
✓ Branch 1 taken 33 times.
✓ Branch 3 taken 34 times.
✓ Branch 7 taken 34 times.
|
34 | : m_zipFile(zipOpen(openstudio::toString(filename).c_str(), add ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE)) { |
| 15 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | if (!m_zipFile) { |
| 16 | ✗ | throw std::runtime_error("ZipFile " + openstudio::toString(filename) + " could not be opened"); | |
| 17 | } | ||
| 18 | 34 | } | |
| 19 | |||
| 20 | 34 | ZipFile::~ZipFile() { | |
| 21 | 34 | zipClose(m_zipFile, nullptr); | |
| 22 | 34 | } | |
| 23 | |||
| 24 | 460 | void ZipFile::addFile(const openstudio::path& localPath, const openstudio::path& destinationPath) { | |
| 25 |
3/4✓ Branch 1 taken 460 times.
✓ Branch 5 taken 460 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 460 times.
|
920 | if (zipOpenNewFileInZip(m_zipFile, openstudio::toString(destinationPath).c_str(), nullptr, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, |
| 26 | Z_DEFAULT_COMPRESSION) | ||
| 27 | 460 | != ZIP_OK) { | |
| 28 | ✗ | throw std::runtime_error("Unable to create new file in archive: " + openstudio::toString(destinationPath)); | |
| 29 | } | ||
| 30 | |||
| 31 | try { | ||
| 32 |
2/2✓ Branch 2 taken 460 times.
✓ Branch 5 taken 460 times.
|
460 | std::ifstream ifs(openstudio::toSystemFilename(localPath), std::ios_base::in | std::ios_base::binary); |
| 33 | |||
| 34 |
4/7✓ Branch 1 taken 460 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 460 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 460 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 460 times.
|
460 | if (!ifs.is_open() || ifs.fail()) { |
| 35 | ✗ | throw std::runtime_error("Unable to open local file: " + openstudio::toString(localPath)); | |
| 36 | } | ||
| 37 | |||
| 38 |
3/3✓ Branch 1 taken 144499 times.
✓ Branch 3 taken 144039 times.
✓ Branch 4 taken 460 times.
|
144499 | while (!ifs.eof()) { |
| 39 |
1/1✓ Branch 1 taken 144039 times.
|
144039 | std::vector<char> buffer(1024); |
| 40 |
1/1✓ Branch 3 taken 144039 times.
|
144039 | ifs.read(&buffer.front(), buffer.size()); |
| 41 | 144039 | std::streamsize bytesread = ifs.gcount(); | |
| 42 | |||
| 43 |
6/8✓ Branch 1 taken 144039 times.
✓ Branch 3 taken 460 times.
✓ Branch 4 taken 143579 times.
✓ Branch 6 taken 460 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 460 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 144039 times.
|
144039 | if (ifs.fail() && !ifs.eof()) { |
| 44 | ✗ | throw std::runtime_error("Error reading from local file: " + openstudio::toString(localPath)); | |
| 45 | } | ||
| 46 | |||
| 47 |
1/1✓ Branch 2 taken 144039 times.
|
144039 | zipWriteInFileInZip(m_zipFile, &buffer.front(), static_cast<unsigned int>(bytesread)); |
| 48 | 144039 | } | |
| 49 | 460 | } catch (...) { | |
| 50 | ✗ | zipCloseFileInZip(m_zipFile); | |
| 51 | ✗ | throw; | |
| 52 | ✗ | } | |
| 53 | |||
| 54 | 460 | zipCloseFileInZip(m_zipFile); | |
| 55 | 460 | } | |
| 56 | |||
| 57 | 9 | void ZipFile::addDirectory(const openstudio::path& localDir, const openstudio::path& destinationDir) { | |
| 58 | // following conventions in openstudio::copyDirectory | ||
| 59 | |||
| 60 |
3/3✓ Branch 1 taken 9 times.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 9 times.
|
17 | for (const auto& file : openstudio::filesystem::recursive_directory_files(localDir)) { |
| 61 |
1/1✓ Branch 1 taken 8 times.
|
16 | const auto srcItemPath = localDir / file; |
| 62 |
1/1✓ Branch 1 taken 8 times.
|
16 | const auto dstItemPath = destinationDir / file; |
| 63 |
1/1✓ Branch 1 taken 8 times.
|
8 | addFile(srcItemPath, dstItemPath); |
| 64 | 17 | } | |
| 65 | 9 | } | |
| 66 | |||
| 67 | } // namespace openstudio | ||
| 68 |