| 101 | } |
| 102 | |
| 103 | openstudio::path UnzipFile::extractFile(const openstudio::path& filename, const openstudio::path& outputPath) const { |
| 104 | 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 | if (unzOpenCurrentFile(m_unzFile) != UNZ_OK) { |
| 109 | throw std::runtime_error("Unable to open file in archive: " + openstudio::toString(filename)); |
| 110 | } |
| 111 | |
| 112 | std::vector<char> buffer(m_chunksize); |
| 113 | |
| 114 | try { |
| 115 | bool cont = true; |
| 116 | |
| 117 | openstudio::path createdFile = outputPath / filename; |
| 118 | openstudio::filesystem::create_directories(createdFile.parent_path()); |
| 119 | |
| 120 | openstudio::filesystem::ofstream file(createdFile, std::ios_base::trunc | std::ios_base::binary); |
| 121 | while (cont) { |
| 122 | int bytesread = unzReadCurrentFile(m_unzFile, &buffer.front(), buffer.size()); |
| 123 | |
| 124 | if (bytesread == 0) { |
| 125 | cont = false; |
| 126 | } else if (bytesread < 0) { |
| 127 | throw std::runtime_error("Unable to read from file"); |
| 128 | } else { |
| 129 | file.write(&buffer.front(), bytesread); |
| 130 | if (!file.good()) { |
| 131 | throw std::runtime_error("Error writing to output file: " + toString(createdFile)); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | file.close(); |
| 136 | |
| 137 | return createdFile; |
| 138 | } catch (...) { |
| 139 | unzCloseCurrentFile(m_unzFile); |
| 140 | throw; |
| 141 | } |
| 142 | |
| 143 | unzCloseCurrentFile(m_unzFile); |
| 144 | return openstudio::path{}; |
| 145 | } |
| 146 | |
| 147 | std::vector<openstudio::path> UnzipFile::listFiles() const { |
| 148 | bool cont = unzGoToFirstFile(m_unzFile) == UNZ_OK; |