Skip to content
Snippets Groups Projects
Commit 94022741 authored by Adrien Béraud's avatar Adrien Béraud Committed by Guillaume Roguez
Browse files

archiver: add Gzip support


Add methods for Gzip file compression/decompression.

Change-Id: I81d52c09a98b835c32d7a07ab2cb9a22aca19e8e
Reviewed-by: default avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent b9f2042f
No related branches found
No related tags found
No related merge requests found
...@@ -223,6 +223,33 @@ compress(const std::string& str) ...@@ -223,6 +223,33 @@ compress(const std::string& str)
return outbuffer; 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> std::vector<uint8_t>
decompress(const std::vector<uint8_t>& str) decompress(const std::vector<uint8_t>& str)
{ {
......
...@@ -64,6 +64,16 @@ std::vector<uint8_t> compress(const std::string& str); ...@@ -64,6 +64,16 @@ std::vector<uint8_t> compress(const std::string& str);
*/ */
std::vector<uint8_t> decompress(const std::vector<uint8_t>& dat); 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 } // namespace ring
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment