-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathZLibCompressor.cpp
46 lines (38 loc) · 1.13 KB
/
ZLibCompressor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* ZLibCompressor.cpp
*
* Created on: 03 mag 2018
* Author: Stefano ceccherini (stefano.ceccherini@gmail.com)
*/
#include "ZLibCompressor.h"
#include <iostream>
#include <zlib.h>
/*static */
bool
ZLibCompressor::Compress(const char* source, size_t sourceLength, char*& destination, size_t& destLength)
{
destLength = compressBound(sourceLength);
destination = new char[destLength];
if (int compressStatus = compress((Bytef*)destination, (uLongf*)&destLength,
(const Bytef*)source, (uLong)sourceLength) != Z_OK) {
std::cerr << "Compress returned error: " << zError(compressStatus) << std::endl;
delete[] destination;
return false;
}
return true;
}
/* static */
bool
ZLibCompressor::Uncompress(const char* source, size_t sourceLen, char*& destination, size_t& destLength)
{
destLength = 32768;
destination = new char[destLength];
if (int status = uncompress((Bytef*)destination, (uLongf*)&destLength,
(const Bytef*)source, (uLong)sourceLen) != Z_OK) {
std::cerr << "UncompressXml: Failed to decompress XML: ";
std::cerr << zError(status) << std::endl;
delete[] destination;
return false;
}
return true;
}