0

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?

Vlad
  • 87
  • 1
  • 1
  • 6

1 Answers1

1

It says it's not in gzip format because it's not in gzip format. compress() compresses to the zlib format. If you want zlib to produce gzip streams, you either need to use deflate() to do it in memory, or the gz* functions if you're writing to a file.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158