#include <zlib.h>
#include <string>
#include "Exception.hxx"
#include "LOG.hxx"
Go to the source code of this file.
◆ compressString()
Compress a given string with zlib.
Compress the given string. Returns an empty string if something went wrong
- Parameters
-
stringToCompress | String that should be compressed |
- Returns
- std::string compressed data
- Exceptions
-
CompressionError | if Zlib stream initialization fails or there was an error during the compression process |
Definition at line 17 of file compression.hxx.
21 int deflateResult = Z_OK;
25 zstream.zalloc = Z_NULL;
26 zstream.zfree = Z_NULL;
27 zstream.opaque = Z_NULL;
29 if (deflateInit(&zstream, Z_BEST_COMPRESSION) != Z_OK)
36 zstream.avail_in =
static_cast<unsigned int>(stringToCompress.size());
37 zstream.next_in = (Bytef *)stringToCompress.data();
42 zstream.next_out =
reinterpret_cast<Bytef *
>(writeBuffer);
43 zstream.avail_out =
sizeof(writeBuffer);
45 deflateResult = deflate(&zstream, Z_FINISH);
47 if (compressedString.size() < zstream.total_out)
49 compressedString.append(writeBuffer, zstream.total_out - compressedString.size());
51 }
while (zstream.avail_out == 0);
55 if (deflateResult != Z_STREAM_END)
62 return compressedString;
◆ decompressString()
decompresses given string
- Parameters
-
compressedString | The String that should be decompressed |
- Returns
- std::string Uncompressed string
- Exceptions
-
CompressionError | if Zlib stream initialization fails or there was an error during the decompression process |
Definition at line 71 of file compression.hxx.
79 zstream.zalloc = Z_NULL;
80 zstream.zfree = Z_NULL;
81 zstream.opaque = Z_NULL;
83 if (inflateInit(&zstream) != Z_OK)
90 zstream.next_in = (Bytef *)compressedString.data();
91 zstream.avail_in =
static_cast<unsigned int>(compressedString.size());
96 zstream.next_out =
reinterpret_cast<Bytef *
>(readBuffer);
97 zstream.avail_out =
sizeof(readBuffer);
99 inflateResult = inflate(&zstream, 0);
101 if (uncompressedString.size() < zstream.total_out)
103 uncompressedString.append(readBuffer, zstream.total_out - uncompressedString.size());
106 }
while (inflateResult == Z_OK);
108 inflateEnd(&zstream);
110 if (inflateResult != Z_STREAM_END)
117 return uncompressedString;
◆ CHUNK_SIZE
constexpr int CHUNK_SIZE = 65536 |
|
constexpr |