diff --git a/src/archiver.cpp b/src/archiver.cpp index b90500538c20fab50d06fe1a266df6393a22d2f1..4c3c8631a6421e41014f99088291f0468e51ed6d 100644 --- a/src/archiver.cpp +++ b/src/archiver.cpp @@ -223,6 +223,33 @@ compress(const std::string& str) return outbuffer; } +void +compressGzip(const std::string& str, const std::string& path) +{ + auto fi = gzopen(path.c_str(), "wb"); + gzwrite(fi, str.data(), str.size()); + gzclose(fi); +} + +std::vector<uint8_t> +decompressGzip(const std::string& path) +{ + std::vector<uint8_t> out; + auto fi = gzopen(path.c_str(),"rb"); + gzrewind(fi); + while (not gzeof(fi)) { + std::array<uint8_t, 32768> outbuffer; + int len = gzread(fi, outbuffer.data(), outbuffer.size()); + if (len == -1) { + gzclose(fi); + throw std::runtime_error("Exception during gzip decompression"); + } + out.insert(out.end(), outbuffer.begin(), outbuffer.begin() + len); + } + gzclose(fi); + return out; +} + std::vector<uint8_t> decompress(const std::vector<uint8_t>& str) { diff --git a/src/archiver.h b/src/archiver.h index cfdf6131725470f185841e6aa03c66bd6bd3097e..f7b637d04161a0dc8f4d39188ac3013340f05b8b 100644 --- a/src/archiver.h +++ b/src/archiver.h @@ -64,6 +64,16 @@ std::vector<uint8_t> compress(const std::string& str); */ std::vector<uint8_t> decompress(const std::vector<uint8_t>& dat); +/** + * Compress string to a Gzip file + */ +void compressGzip(const std::string& str, const std::string& path); + +/** + * Decompress Gzip file to bytes + */ +std::vector<uint8_t> decompressGzip(const std::string& path); + } } // namespace ring