byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
GzipFileWriter.hpp
Go to the documentation of this file.
1#ifndef BYTEME_GZIP_FILE_WRITER_HPP
2#define BYTEME_GZIP_FILE_WRITER_HPP
3
4#include <stdexcept>
5#include <vector>
6#include <string>
7#include "Writer.hpp"
8
15namespace byteme {
16
22class GzipFileWriter : public Writer {
23public:
29 GzipFileWriter(const char* path, int compression_level = 6, size_t buffer_size = 65536) : my_gzfile(path, "wb") {
30 if (gzbuffer(my_gzfile.handle, buffer_size)) {
31 throw std::runtime_error("failed to set the Gzip compression buffer");
32 }
33 if (gzsetparams(my_gzfile.handle, compression_level, Z_DEFAULT_STRATEGY) != Z_OK) {
34 throw std::runtime_error("failed to set the Gzip compression parameters");
35 }
36 }
37
43 GzipFileWriter(const std::string& path, int compression_level = 6, size_t buffer_size = 65536) : GzipFileWriter(path.c_str(), compression_level, buffer_size) {}
44
45public:
46 using Writer::write;
47
48 void write(const unsigned char* buffer, size_t n) {
49 if (n) {
50 size_t ok = gzwrite(my_gzfile.handle, buffer, n);
51 if (ok != n) {
52 throw std::runtime_error("failed to write to the Gzip-compressed file");
53 }
54 }
55 }
56
57 void finish() {
58 my_gzfile.closed = true;
59 if (gzclose(my_gzfile.handle) != Z_OK) {
60 throw std::runtime_error("failed to close the Gzip-compressed file after writing");
61 }
62 }
63
64private:
65 SelfClosingGzFile my_gzfile;
66};
67
68}
69
70#endif
Write to an output sink.
Write uncompressed bytes to a Gzip-compressed file.
Definition GzipFileWriter.hpp:22
GzipFileWriter(const std::string &path, int compression_level=6, size_t buffer_size=65536)
Definition GzipFileWriter.hpp:43
void write(const unsigned char *buffer, size_t n)
Definition GzipFileWriter.hpp:48
GzipFileWriter(const char *path, int compression_level=6, size_t buffer_size=65536)
Definition GzipFileWriter.hpp:29
void finish()
Definition GzipFileWriter.hpp:57
Virtual class for writing bytes to a sink.
Definition Writer.hpp:18
virtual void write(const unsigned char *buffer, size_t n)=0
Simple byte readers and writers.