GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 80.6% 29 / 0 / 36
Functions: 100.0% 4 / 0 / 4
Branches: 60.4% 32 / 0 / 53

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 144607 times.
✓ Branch 3 taken 144147 times.
✓ Branch 4 taken 460 times.
144607 while (!ifs.eof()) {
39
1/1
✓ Branch 1 taken 144147 times.
144147 std::vector<char> buffer(1024);
40
1/1
✓ Branch 3 taken 144147 times.
144147 ifs.read(&buffer.front(), buffer.size());
41 144147 std::streamsize bytesread = ifs.gcount();
42
43
6/8
✓ Branch 1 taken 144147 times.
✓ Branch 3 taken 460 times.
✓ Branch 4 taken 143687 times.
✓ Branch 6 taken 460 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 460 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 144147 times.
144147 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 144147 times.
144147 zipWriteInFileInZip(m_zipFile, &buffer.front(), static_cast<unsigned int>(bytesread));
48 144147 }
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