Compression.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__COMPRESSION
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__COMPRESSION
19 
21 #include <Balau/Resource/File.hpp>
22 #include <Balau/Util/DateTime.hpp>
23 #include <Balau/Util/Files.hpp>
24 
25 #include <algorithm>
26 #include <chrono>
27 #include <iosfwd>
28 #include <sstream>
29 
30 #include <boost/iostreams/filtering_stream.hpp>
31 
32 namespace Balau {
33 
34 namespace Util {
35 
39 struct GZip final {
46  static void gzip(const Resource::File & input, const Resource::File & output) {
47  boost::filesystem::ofstream out(output.getEntry(), std::ios::binary);
48  boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
49  filter.push(boost::iostreams::gzip_compressor());
50  filter.push(out);
51  boost::filesystem::ifstream in(input.getEntry(), std::ios::binary);
52  boost::iostreams::copy(in, filter);
53  }
54 
61  static void gzip(const std::string & input, const Resource::File & output) {
62  boost::filesystem::ofstream out(output.getEntry(), std::ios::binary);
63  boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
64  filter.push(boost::iostreams::gzip_compressor());
65  filter.push(out);
66  std::istringstream in(input, std::ios::binary);
67  boost::iostreams::copy(in, filter);
68  }
69 
76  static void gzip(std::istream & input, const Resource::File & output) {
77  boost::filesystem::ofstream out(output.getEntry(), std::ios::binary);
78  boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
79  filter.push(boost::iostreams::gzip_compressor());
80  filter.push(out);
81  boost::iostreams::copy(input, filter);
82  }
83 
90  static void gunzip(const Resource::File & input, const Resource::File & output) {
91  boost::filesystem::ifstream in(input.getEntry(), std::ios::binary);
92  boost::iostreams::filtering_streambuf<boost::iostreams::input> filter;
93  filter.push(boost::iostreams::gzip_decompressor());
94  filter.push(in);
95  boost::filesystem::ofstream out(output.getEntry(), std::ios::binary);
96  boost::iostreams::copy(filter, out);
97  }
98 
108  static void gunzip(const Resource::File & input, std::string & output) {
109  boost::filesystem::ifstream in(input.getEntry(), std::ios::binary);
110  boost::iostreams::filtering_streambuf<boost::iostreams::input> filter;
111  filter.push(boost::iostreams::gzip_decompressor());
112  filter.push(in);
113  std::ostringstream out(std::ios::binary);
114  boost::iostreams::copy(filter, out);
115  output = out.str();
116  }
117 
124  static void gunzip(const Resource::File & input, std::unique_ptr<std::istream> & stream) {
125  boost::filesystem::ifstream inputStream(input.getEntry(), std::ios::binary);
126  stream = std::make_unique<boost::iostreams::filtering_istream>();
127  auto * filterStream = static_cast<boost::iostreams::filtering_istream *>(stream.get());
128  filterStream->push(boost::iostreams::gzip_decompressor());
129  filterStream->push(inputStream);
130  }
131 
133 
134  GZip() = delete;
135  GZip(const GZip &) = delete;
136  GZip & operator = (const GZip &) = delete;
137 };
138 
139 } // namespace Util
140 
141 } // namespace Balau
142 
143 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__COMPRESSION
static void gunzip(const Resource::File &input, std::unique_ptr< std::istream > &stream)
Get an input stream of the uncompressed contents of the specified gzipped input file.
Definition: Compression.hpp:124
static void gunzip(const Resource::File &input, std::string &output)
Gunzip the specified file to a string.
Definition: Compression.hpp:108
Utilities for files.
A file on the local file system.
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
static void gzip(std::istream &input, const Resource::File &output)
Gzip the supplied input stream to the specified output file.
Definition: Compression.hpp:76
static void gunzip(const Resource::File &input, const Resource::File &output)
Gunzip the specified input file to the specified output file.
Definition: Compression.hpp:90
boost::filesystem::directory_entry getEntry() const
Get the underlying directory entry for this file URI.
Definition: File.hpp:400
static void gzip(const Resource::File &input, const Resource::File &output)
Gzip the specified input file to the specified output file.
Definition: Compression.hpp:46
static void gzip(const std::string &input, const Resource::File &output)
Gzip the supplied string to the specified output file.
Definition: Compression.hpp:61
A file on the local file system.
Definition: File.hpp:35
GZip compression utilities.
Definition: Compression.hpp:39
Balau exceptions for resources.
Date and time utilities.