decompressing with zlib

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
1.6 kB
5
Indexable
Never
```
void DecompressFile(const char* compressedData, size_t compressedSize, size_t& decompressedSize, char** output) {
    z_stream stream;
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;
    stream.avail_in = static_cast<uInt>(compressedSize);
    stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(compressedData));

    if (inflateInit(&stream) != Z_OK) {
        return;
    }

    const size_t bufferSize = 1024;
    char buffer[bufferSize];
    std::string result;

    do {
        stream.avail_out = bufferSize;
        stream.next_out = reinterpret_cast<Bytef*>(buffer);

        int ret = inflate(&stream, Z_NO_FLUSH);

        if (ret == Z_STREAM_ERROR) {
            inflateEnd(&stream);
            return;
        }

        result.append(buffer, bufferSize - stream.avail_out);
    } while (stream.avail_out == 0);

    inflateEnd(&stream);

    decompressedSize = result.size();

    *output = _strdup(result.c_str()); // this line is the problem / part of the problem
}
```

i want the same behaviour as
```
import zlib

def extract_zlib(input_file, output_file):
    try:
        with open(input_file, 'rb') as f_in:
            compressed_data = f_in.read()
            decompressed_data = zlib.decompress(compressed_data)

        with open(output_file, 'wb') as f_out:
            f_out.write(decompressed_data)

        print(f"Data extracted and saved to {output_file}")
    except Exception as e:
        print(f"Error: {str(e)}")
        
extract_zlib(input_file, output_file)
```
with data instead of files :/
Leave a Comment