GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 77.9% 67 / 0 / 86
Functions: 71.4% 5 / 0 / 7
Branches: 67.6% 71 / 0 / 105

src/utilities/core/UnzipFile.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 "UnzipFile.hpp"
7 #include <minizip/unzip.h>
8
9 namespace openstudio {
10
11
2/2
✓ Branch 1 taken 9 times.
✓ Branch 5 taken 9 times.
9 UnzipFile::UnzipFile(const openstudio::path& filename) : m_unzFile(unzOpen(openstudio::toString(filename).c_str())), m_chunksize(32768) {
12
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 if (!m_unzFile) {
13
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (!openstudio::filesystem::exists(filename)) {
14
4/4
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 11 taken 1 time.
1 throw std::runtime_error("UnzipFile " + openstudio::toString(filename) + " does not exist, could not be opened");
15 } else {
16 ✗ throw std::runtime_error("UnzipFile " + openstudio::toString(filename) + " exists, could not be opened");
17 }
18 }
19 8 }
20
21 8 UnzipFile::~UnzipFile() {
22 8 unzClose(m_unzFile);
23 8 }
24
25 ✗ unsigned long UnzipFile::chunksize() const {
26 ✗ return m_chunksize;
27 }
28 ✗ void UnzipFile::setChunksize(unsigned long chunksize) {
29 ✗ m_chunksize = chunksize;
30 ✗ }
31
32 6 std::vector<openstudio::path> UnzipFile::extractAllFiles(const openstudio::path& outputPath) const {
33
34 // We do not call listFiles() then for each extractFile, for performance reasons (some of the work would be done twice). cf #4456
35
36
1/1
✓ Branch 1 taken 6 times.
6 bool cont_files = unzGoToFirstFile(m_unzFile) == UNZ_OK;
37
38 6 std::vector<openstudio::path> filesOnDisk;
39
40 // Start by creating the output directory itself if need be
41
1/1
✓ Branch 1 taken 6 times.
6 openstudio::filesystem::create_directories(outputPath);
42
43
1/1
✓ Branch 1 taken 6 times.
12 std::vector<char> buffer(m_chunksize);
44
1/1
✓ Branch 1 taken 6 times.
12 std::vector<char> filenamebuffer(300);
45
46
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 do {
47 unz_file_info file_info;
48
49
1/1
✓ Branch 3 taken 18 times.
18 unzGetCurrentFileInfo(m_unzFile, &file_info, &filenamebuffer.front(), filenamebuffer.size(), nullptr, 0, nullptr, 0);
50
51
1/1
✓ Branch 2 taken 18 times.
18 std::string fileName(&filenamebuffer.front(), file_info.size_filename);
52
53
1/1
✓ Branch 1 taken 18 times.
18 openstudio::path zippedFileRelPath = openstudio::toPath(fileName);
54
1/1
✓ Branch 1 taken 18 times.
36 openstudio::path createdFilePath = outputPath / zippedFileRelPath;
55
56
6/7
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 16 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 16 times.
✓ Branch 9 taken 2 times.
18 if ((fileName.back() == '/') || (fileName == ".")) {
57 // This is a directory - skip it
58 } else {
59 // Entry is a file, so extract it.
60 // First we do need to create the parent directory(ies). The order is not necessarilly good so we can't do it in the other branch of if above
61
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
16 openstudio::filesystem::create_directories(createdFilePath.parent_path());
62
63 // Open the zipped file
64
2/3
✓ Branch 1 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
16 if (unzOpenCurrentFile(m_unzFile) != UNZ_OK) {
65 ✗ throw std::runtime_error("Unable to open file in archive: " + openstudio::toString(zippedFileRelPath));
66 }
67
68 try {
69 16 bool cont_read = true;
70
71 // Open the target file on disk
72
1/1
✓ Branch 2 taken 16 times.
16 openstudio::filesystem::ofstream file(createdFilePath, std::ios_base::trunc | std::ios_base::binary);
73
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 16 times.
49 while (cont_read) {
74
75
1/1
✓ Branch 3 taken 33 times.
33 int bytesread = unzReadCurrentFile(m_unzFile, &buffer.front(), buffer.size());
76
77
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 17 times.
33 if (bytesread == 0) {
78 16 cont_read = false;
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 } else if (bytesread < 0) {
80 ✗ throw std::runtime_error("Unable to read from file: " + openstudio::toString(zippedFileRelPath));
81 } else {
82
1/1
✓ Branch 2 taken 17 times.
17 file.write(&buffer.front(), bytesread);
83
2/3
✓ Branch 1 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
17 if (!file.good()) {
84 ✗ throw std::runtime_error("Error writing to output file: " + toString(createdFilePath));
85 }
86 }
87 }
88
1/1
✓ Branch 1 taken 16 times.
16 file.close();
89
90
1/1
✓ Branch 1 taken 16 times.
16 filesOnDisk.push_back(createdFilePath);
91 16 } catch (...) {
92 ✗ unzCloseCurrentFile(m_unzFile);
93 ✗ throw;
94 ✗ }
95 }
96
97
1/1
✓ Branch 1 taken 18 times.
18 cont_files = unzGoToNextFile(m_unzFile) == UNZ_OK;
98 18 } while (cont_files);
99
100 12 return filesOnDisk;
101 6 }
102
103 3 openstudio::path UnzipFile::extractFile(const openstudio::path& filename, const openstudio::path& outputPath) const {
104
3/4
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
3 if (unzLocateFile(m_unzFile, openstudio::toString(filename).c_str(), 1) != UNZ_OK) {
105 ✗ throw std::runtime_error("File does not exist in archive: " + openstudio::toString(filename));
106 }
107
108
2/3
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (unzOpenCurrentFile(m_unzFile) != UNZ_OK) {
109 ✗ throw std::runtime_error("Unable to open file in archive: " + openstudio::toString(filename));
110 }
111
112
1/1
✓ Branch 1 taken 3 times.
3 std::vector<char> buffer(m_chunksize);
113
114 try {
115 3 bool cont = true;
116
117
1/1
✓ Branch 1 taken 3 times.
6 openstudio::path createdFile = outputPath / filename;
118
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 openstudio::filesystem::create_directories(createdFile.parent_path());
119
120
1/1
✓ Branch 2 taken 3 times.
3 openstudio::filesystem::ofstream file(createdFile, std::ios_base::trunc | std::ios_base::binary);
121
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 while (cont) {
122
1/1
✓ Branch 3 taken 8 times.
8 int bytesread = unzReadCurrentFile(m_unzFile, &buffer.front(), buffer.size());
123
124
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 if (bytesread == 0) {
125 3 cont = false;
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } else if (bytesread < 0) {
127 ✗ throw std::runtime_error("Unable to read from file");
128 } else {
129
1/1
✓ Branch 2 taken 5 times.
5 file.write(&buffer.front(), bytesread);
130
2/3
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
5 if (!file.good()) {
131 ✗ throw std::runtime_error("Error writing to output file: " + toString(createdFile));
132 }
133 }
134 }
135
1/1
✓ Branch 1 taken 3 times.
3 file.close();
136
137 3 return createdFile;
138 3 } catch (...) {
139 ✗ unzCloseCurrentFile(m_unzFile);
140 ✗ throw;
141 ✗ }
142
143 unzCloseCurrentFile(m_unzFile);
144 return openstudio::path{};
145 3 }
146
147 1 std::vector<openstudio::path> UnzipFile::listFiles() const {
148
1/1
✓ Branch 1 taken 1 time.
1 bool cont = unzGoToFirstFile(m_unzFile) == UNZ_OK;
149
150 1 std::vector<openstudio::path> paths;
151
152
1/1
✓ Branch 1 taken 1 time.
2 std::vector<char> filename(300);
153
154 6 do {
155 unz_file_info file_info;
156
157
1/1
✓ Branch 3 taken 6 times.
6 unzGetCurrentFileInfo(m_unzFile, &file_info, &filename.front(), filename.size(), nullptr, 0, nullptr, 0);
158
159
3/3
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 8 taken 6 times.
6 paths.push_back(openstudio::toPath(std::string(&filename.front(), file_info.size_filename)));
160
3/3
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 1 time.
6 cont = unzGoToNextFile(m_unzFile) == UNZ_OK;
161 } while (cont);
162
163 2 return paths;
164 1 }
165
166 } // namespace openstudio
167