| 22 | } |
| 23 | |
| 24 | void ZipFile::addFile(const openstudio::path& localPath, const openstudio::path& destinationPath) { |
| 25 | if (zipOpenNewFileInZip(m_zipFile, openstudio::toString(destinationPath).c_str(), nullptr, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, |
| 26 | Z_DEFAULT_COMPRESSION) |
| 27 | != ZIP_OK) { |
| 28 | throw std::runtime_error("Unable to create new file in archive: " + openstudio::toString(destinationPath)); |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | std::ifstream ifs(openstudio::toSystemFilename(localPath), std::ios_base::in | std::ios_base::binary); |
| 33 | |
| 34 | if (!ifs.is_open() || ifs.fail()) { |
| 35 | throw std::runtime_error("Unable to open local file: " + openstudio::toString(localPath)); |
| 36 | } |
| 37 | |
| 38 | while (!ifs.eof()) { |
| 39 | std::vector<char> buffer(1024); |
| 40 | ifs.read(&buffer.front(), buffer.size()); |
| 41 | std::streamsize bytesread = ifs.gcount(); |
| 42 | |
| 43 | if (ifs.fail() && !ifs.eof()) { |
| 44 | throw std::runtime_error("Error reading from local file: " + openstudio::toString(localPath)); |
| 45 | } |
| 46 | |
| 47 | zipWriteInFileInZip(m_zipFile, &buffer.front(), static_cast<unsigned int>(bytesread)); |
| 48 | } |
| 49 | } catch (...) { |
| 50 | zipCloseFileInZip(m_zipFile); |
| 51 | throw; |
| 52 | } |
| 53 | |
| 54 | zipCloseFileInZip(m_zipFile); |
| 55 | } |
| 56 | |
| 57 | void ZipFile::addDirectory(const openstudio::path& localDir, const openstudio::path& destinationDir) { |
| 58 | // following conventions in openstudio::copyDirectory |