crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Gzip.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Gzip.hpp
24  *
25  * Namespace for functions for gzip compression.
26  *
27  * Created on: May 2, 2019
28  * Author: ans
29  */
30 
31 #ifndef DATA_GZIP_HPP_
32 #define DATA_GZIP_HPP_
33 
34 #include <boost/iostreams/copy.hpp>
35 #include <boost/iostreams/filter/gzip.hpp>
36 #include <boost/iostreams/filtering_streambuf.hpp>
37 
38 #include <sstream> // std::stringstream
39 #include <string> // std::string
40 
43 
44  /*
45  * DECLARATION
46  */
47 
50 
51  std::string compress(const std::string& content);
52  std::string decompress(const std::string& compressedContent);
53 
57 
59  inline constexpr auto compressionLevel{9};
60 
62 
63  /*
64  * IMPLEMENTATION
65  */
66 
68 
80  inline std::string compress(const std::string& content) {
81  if(content.empty()) {
82  return "";
83  }
84 
85  std::stringstream compressed;
86  std::stringstream origin(content);
87 
88  boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
89 
90  out.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(compressionLevel)));
91  out.push(origin);
92 
93  boost::iostreams::copy(out, compressed);
94 
95  // return compressed content
96  return compressed.str();
97  }
98 
100 
111  inline std::string decompress(const std::string& compressedContent) {
112  if(compressedContent.empty()) {
113  return "";
114  }
115 
116  std::stringstream compressed(compressedContent);
117  std::stringstream decompressed;
118 
119  boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
120 
121  out.push(boost::iostreams::gzip_decompressor());
122  out.push(compressed);
123 
124  boost::iostreams::copy(out, decompressed);
125 
126  // return decompressed content
127  return decompressed.str();
128  }
129 
130 } /* namespace crawlservpp::Data::Compression::Gzip */
131 
132 #endif /* DATA_GZIP_HPP_ */
Namespace for compressing and decompressing gzip.
Definition: Gzip.hpp:42
std::string decompress(const std::string &compressedContent)
Decompresses gzip-compressed content.
Definition: Gzip.hpp:111
constexpr auto compressionLevel
The compression level used when compressing with gzip.
Definition: Gzip.hpp:59
std::string compress(const std::string &content)
Compresses content using gzip.
Definition: Gzip.hpp:80