DevCpp中Zlib包的使用

英文版本:http://www.gzip.org/zlib/manual.html#compress

“zlib" 通用压缩程序库 版本1.1.4,2002年3月11日

版本所有?(C) 1995-2002 Jean-loup Gailly and Mark Adler

zlib库所使用的数据格式参见1950-1952的RFCS文档:??

rfc1950.txt (zlib 格式), rfc1951.txt (deflate 格式) and rfc1952.txt (gzip 格式).

 

二、常用支持的函数列表:

下列的函数是基于 basic stream-oriented functions的基础上实现的,仅提供了一般的接口。函数参数使用了特定的选项(例如:压缩级别、内存使用、标准的内存分配函数),如果需要特定的版本,可以对源码进行修改。

  • int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
  • int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
  • int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
  • typedef voidp gzFile;
  • gzFile gzopen (const char *path, const char *mode);
  • gzFile gzdopen (int fd, const char *mode);
  • int gzsetparams (gzFile file, int level, int strategy);
  • int gzread (gzFile file, voidp buf, unsigned len);
  • int gzwrite (gzFile file, const voidp buf, unsigned len);
  • int VA gzprintf (gzFile file, const char *format, ...);
  • int gzputs (gzFile file, const char *s);
  • char * gzgets (gzFile file, char *buf, int len);
  • int gzputc (gzFile file, int c);
  • int gzgetc (gzFile file);
  • int gzflush (gzFile file, int flush);
  • z_off_t gzseek (gzFile file, z_off_t offset, int whence);
  • z_off_t gztell (gzFile file);
  • int gzrewind (gzFile file);
  • int gzeof (gzFile file);
  • int gzclose (gzFile file);
  • const char * gzerror (gzFile file, int *errnum);

函数描述:

int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
将source缓冲区的数据压缩后拷贝至dest缓冲区。
sourceLen 是source 缓冲区的字节数.
?Upon entry, destLen is the total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.

This function can be used to compress a whole file at once if the input file is mmap'ed.

compress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer.

int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
Compresses the source buffer into the destination buffer. The level parameter has the same meaning as in deflateInit. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.

compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, Z_STREAM_ERROR if the level parameter is invalid.

int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
Decompresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be large enough to hold the entire uncompressed data. (The size of the uncompressed data must have been saved previously by the compressor and transmitted to the decompressor by some mechanism outside the scope of this compression library.) Upon exit, destLen is the actual size of the compressed buffer.

This function can be used to decompress a whole file at once if the input file is mmap'ed.

uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, or Z_DATA_ERROR if the input data was corrupted.

typedef voidp gzFile;

gzFile gzopen (const char *path, const char *mode);
Opens a gzip (.gz) file for reading or writing. The mode parameter is as in fopen ("rb" or "wb") but can also include a compression level ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman only compression as in "wb1h". (See the description of deflateInit2 for more information about the strategy parameter.)

gzopen can be used to read a file which is not in gzip format ; in this case gzread will directly read from the file without decompression.

gzopen returns NULL if the file could not be opened or if there was insufficient memory to allocate the (de)compression state ; errno can be checked to distinguish the two cases (if errno is zero, the zlib error is Z_MEM_ERROR).

gzFile gzdopen (int fd, const char *mode);
gzdopen() associates a gzFile with the file descriptor fd. File descriptors are obtained from calls like open, dup, creat, pipe or fileno (in the file has been previously opened with fopen). The mode parameter is as in gzopen.

The next call of gzclose on the returned gzFile will also close the file descriptor fd, just like fclose(fdopen(fd), mode) closes the file descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).

gzdopen returns NULL if there was insufficient memory to allocate the (de)compression state.

int gzsetparams (gzFile file, int level, int strategy);
Dynamically update the compression level or strategy. See the description of deflateInit2 for the meaning of these parameters.

gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not opened for writing.

int gzread (gzFile file, voidp buf, unsigned len);
Reads the given number of uncompressed bytes from the compressed file. If the input file was not in gzip format, gzread copies the given number of bytes into the buffer.

gzread returns the number of uncompressed bytes actually read (0 for end of file, -1 for error).

int gzwrite (gzFile file, const voidp buf, unsigned len);
Writes the given number of uncompressed bytes into the compressed file. gzwrite returns the number of uncompressed bytes actually written (0 in case of error).

int VA gzprintf (gzFile file, const char *format, ...);
Converts, formats, and writes the args to the compressed file under control of the format string, as in fprintf. gzprintf returns the number of uncompressed bytes actually written (0 in case of error).

int gzputs (gzFile file, const char *s);
Writes the given null-terminated string to the compressed file, excluding the terminating null character.

gzputs returns the number of characters written, or -1 in case of error.

char * gzgets (gzFile file, char *buf, int len);
Reads bytes from the compressed file until len-1 characters are read, or a newline character is read and transferred to buf, or an end-of-file condition is encountered. The string is then terminated with a null character.

gzgets returns buf, or Z_NULL in case of error.

int gzputc (gzFile file, int c);
Writes c, converted to an unsigned char, into the compressed file. gzputc returns the value that was written, or -1 in case of error.

int gzgetc (gzFile file);
Reads one byte from the compressed file. gzgetc returns this byte or -1 in case of end of file or error.

int gzflush (gzFile file, int flush);
Flushes all pending output into the compressed file. The parameter flush is as in the deflate() function. The return value is the zlib error number (see function gzerror below). gzflush returns Z_OK if the flush parameter is Z_FINISH and all output could be flushed.

gzflush should be called only when strictly necessary because it can degrade compression.

z_off_t gzseek (gzFile file, z_off_t offset, int whence);
Sets the starting position for the next gzread or gzwrite on the given compressed file. The offset represents a number of bytes in the uncompressed data stream. The whence parameter is defined as in lseek(2); the value SEEK_END is not supported.

If the file is opened for reading, this function is emulated but can be extremely slow. If the file is opened for writing, only forward seeks are supported ; gzseek then compresses a sequence of zeroes up to the new starting position.

gzseek returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position would be before the current position.

int gzrewind (gzFile file);
Rewinds the given file. This function is supported only for reading.

gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)

z_off_t gztell (gzFile file);
Returns the starting position for the next gzread or gzwrite on the given compressed file. This position represents a number of bytes in the uncompressed data stream.

gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)

int gzeof (gzFile file);
Returns 1 when EOF has previously been detected reading the given input stream, otherwise zero.

int gzclose (gzFile file);
Flushes all pending output if necessary, closes the compressed file and deallocates all the (de)compression state. The return value is the zlib error number (see function gzerror below).

const char * gzerror (gzFile file, int *errnum);
Returns the error message for the last error which occurred on the given compressed file. errnum is set to zlib error number. If an error occurred in the file system and not in the compression library, errnum is set to Z_ERRNO and the application may consult errno to get the exact error code.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值