当前位置: 技术问答>linux和unix
gzip flate实现?
来源: 互联网 发布时间:2017-01-31
本文导语: 下面的代码是抄来的然后做了些修改,压缩出来的数据,用gzip解压时,提示not in gzip format,请问有谁知道我哪弄错了吗?对这个不了解,也不知道参考什么。 int gzcompress(Bytef *zdata, uLong *nzdata,Bytef *data, uLong nda...
下面的代码是抄来的然后做了些修改,压缩出来的数据,用gzip解压时,提示not in gzip format,请问有谁知道我哪弄错了吗?对这个不了解,也不知道参考什么。
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 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; memLevel=9 uses maximum memory
00514 for optimal speed. The default value is 8. See zconf.h for total memory
00515 usage as a function of windowBits and memLevel.
00516
00517 The strategy parameter is used to tune the compression algorithm. Use the
00518 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
00519 filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
00520 string match), or Z_RLE to limit match distances to one (run-length
00521 encoding). Filtered data consists mostly of small values with a somewhat
00522 random distribution. In this case, the compression algorithm is tuned to
00523 compress them better. The effect of Z_FILTERED is to force more Huffman
00524 coding and less string matching; it is somewhat intermediate between
00525 Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
00526 Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
00527 parameter only affects the compression ratio but not the correctness of the
00528 compressed output even if it is not set appropriately. Z_FIXED prevents the
00529 use of dynamic Huffman codes, allowing for a simpler decoder for special
00530 applications.
00531
00532 deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00533 memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
00534 method). msg is set to null if there is no error message. deflateInit2 does
00535 not perform any compression: this will be done by deflate().
00536 */