How to decompress gzipstream with zlib

后端 未结 4 1078
走了就别回头了
走了就别回头了 2020-12-20 22:01

Can someone tell me which function I need to use in order to decompress a byte array that has been compressed with vb.net\'s gzipstream. I would like to use zlib.

I\

4条回答
  •  既然无缘
    2020-12-20 22:50

    Here is a C function that does the job with zlib:

    int gzip_inflate(char *compr, int comprLen, char *uncompr, int uncomprLen)
    {
        int err;
        z_stream d_stream; /* decompression stream */
    
        d_stream.zalloc = (alloc_func)0;
        d_stream.zfree = (free_func)0;
        d_stream.opaque = (voidpf)0;
    
        d_stream.next_in  = (unsigned char *)compr;
        d_stream.avail_in = comprLen;
    
        d_stream.next_out = (unsigned char *)uncompr;
        d_stream.avail_out = uncomprLen;
    
        err = inflateInit2(&d_stream, 16+MAX_WBITS);
        if (err != Z_OK) return err;
    
        while (err != Z_STREAM_END) err = inflate(&d_stream, Z_NO_FLUSH);
    
        err = inflateEnd(&d_stream);
        return err;
    }
    

    The uncompressed string is returned in uncompr. It's a null-terminated C string so you can do puts(uncompr). The function above only works if the output is text. I have tested it and it works.

提交回复
热议问题