Compress string (i tried to compress file but it didnt help) and save compress data to file:
#include <iostream>
#include <fstream>
#include <zlib.h>
int main() {
char a[50] = "Hello, world!";
char b[50];
uLong ucompSize = strlen(a)+1;
uLong compSize = compressBound(ucompSize);
compress((Bytef *)b, &compSize, (Bytef *)a, ucompSize);
std::ofstream f;
f.open("res.txt.gz", std::ios::binary);
f.write(b, compSize);
// Assertion
char c[50];
uncompress((Bytef *)c, &ucompSize, (Bytef *)b, compSize);;
assert(std::string(c) == std::string(a));
}
Next Im trying to unarchive it by gzip util and error appears:
$ gzip -d res.txt.gz
gzip: res.txt.gz: not in gzip format
Where is the compression error?