日期:2014-05-16  浏览次数:20945 次

gzip flate实现?
下面的代码是抄来的然后做了些修改,压缩出来的数据,用gzip解压时,提示not in gzip format,请问有谁知道我哪弄错了吗?对这个不了解,也不知道参考什么。
C/C++ code

int gzcompress(Bytef *zdata, uLong *nzdata,Bytef *data, uLong ndata)
{
    gz_header_s gz_header;
    z_stream c_stream;
    int err = 0;
 
    if(data && ndata > 0)
    {
        c_stream.zalloc = (alloc_func)0;
        c_stream.zfree = (free_func)0;
        c_stream.opaque = (voidpf)0;
        if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,  
                    -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
        c_stream.next_in  = data;
        c_stream.avail_in  = ndata;
        c_stream.next_out = zdata;
        c_stream.avail_out  = *nzdata;
        
        gz_header.text = 0;
        gz_header.time = time(NULL);
        gz_header.xflags = 0;
        gz_header.os = 3;
        gz_header.extra = Z_NULL;
        gz_header.extra_len = 0;
        gz_header.extra_max = 0;
        gz_header.name = Z_NULL;
        gz_header.name_max = 0;
        gz_header.comment = Z_NULL;
        gz_header.comm_max = 0;
        gz_header.hcrc = 1;
        gz_header.done = 1;
        deflateSetHeader(&c_stream, &gz_header);
        
        while (c_stream.avail_in != 0 && c_stream.total_out < *nzdata)  
        {
            if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
        }
        if(c_stream.avail_in != 0) return c_stream.avail_in;
        for (;;) {
            if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
            if(err != Z_OK) return -1;
        }
        if(deflateEnd(&c_stream) != Z_OK) return -1;
        *nzdata = c_stream.total_out;
        return 0;
    }
    return -1;
} 



------解决方案--------------------
C/C++ code

/*
00480 ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
00481                                      int  level,
00482                                      int  method,
00483                                      int  windowBits,
00484                                      int  memLevel,
00485                                      int  strategy));
00486 
00487      This is another version of deflateInit with more compression options. The
00488    fields next_in, zalloc, zfree and opaque must be initialized before by
00489    the caller.
00490 
00491      The method parameter is the compression method. It must be Z_DEFLATED in
00492    this version of the library.
00493 
00494      The windowBits parameter is the base two logarithm of the window size
00495    (the size of the history buffer). It should be in the range 8..15 for this
00496    version of the library. Larger values of this parameter result in better
00497    compression at the expense of memory usage. The default value is 15 if
00498    deflateInit is used instead.
00499 
00500      windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
00501    determines the window size. deflate() will then generate raw deflate data
00502    with no zlib header or trailer, and will not compute an adler32 check value.
00503 
00504      windowBits can also be greater than 15 for optional gzip encoding. Add
00505    16 to windowBits to write a simple gzip header and trailer around the
00506    compressed data instead of a zlib wrapper. The gzip header will have no
00507    file name, no extra data, no comment, no modification time (set to zero),
00508    no header crc, and the operating system will be set to 255 (unknown).  If a
00509    gzip stream is being written, strm->adler is a crc32 instead of an adler32.
00510 
00511      The memLevel parameter specifies how much memory should be allocated
00512    for the internal compression state. memLevel=1 uses minimum memory but
00513    is slow and reduces compression ratio; mem