Takes ownership of pre-deflated entries and registers them in the archive in order. Entries stay alive for the AutoCloseZip's lifetime, so libzip's later zip_close (either explicit or from the destructor) still sees valid memory when it drains the sources.
| 188 | /// Entries stay alive for the AutoCloseZip's lifetime, so libzip's later zip_close (either |
| 189 | /// explicit or from the destructor) still sees valid memory when it drains the sources. |
| 190 | Expected<void> addPreDeflatedEntries( |
| 191 | std::vector<DeflatedEntry> entries, |
| 192 | const std::vector<std::pair<std::filesystem::path, std::string>>& files, |
| 193 | int level, |
| 194 | const std::string& password ) |
| 195 | { |
| 196 | assert( entries.size() == files.size() ); |
| 197 | entries_ = std::move( entries ); |
| 198 | const zip_uint16_t gpbFlags = deflateGpbLevelFlags( level ); |
| 199 | for ( size_t i = 0; i < entries_.size(); ++i ) |
| 200 | { |
| 201 | auto& entry = entries_[i]; |
| 202 | entry.gpbFlags = gpbFlags; |
| 203 | const auto& archiveFilePath = files[i].second; |
| 204 | |
| 205 | zip_source_t* src = zip_source_function( handle_, deflatedSourceCallback, &entry ); |
| 206 | if ( !src ) |
| 207 | return unexpected( "Cannot create source for " + archiveFilePath ); |
| 208 | |
| 209 | const auto index = zip_file_add( handle_, archiveFilePath.c_str(), src, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8 ); |
| 210 | if ( index < 0 ) |
| 211 | { |
| 212 | zip_source_free( src ); |
| 213 | return unexpected( "Cannot add file " + archiveFilePath + " to archive" ); |
| 214 | } |
| 215 | |
| 216 | zip_set_file_compression( handle_, index, ZIP_CM_DEFLATE, zip_uint32_t( level ) ); |
| 217 | |
| 218 | if ( !password.empty() ) |
| 219 | { |
| 220 | if ( zip_file_set_encryption( handle_, index, ZIP_EM_AES_256, password.c_str() ) ) |
| 221 | return unexpected( "Cannot encrypt file " + archiveFilePath + " in archive" ); |
| 222 | } |
| 223 | } |
| 224 | return {}; |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | zip_t * handle_ = nullptr; |
no test coverage detected