Linux 版 (精华区)
发信人: xian (我想用心来点亮希望), 信区: Linux
标 题: Linux程式设计入门 - zlib的运用
发信站: 紫 丁 香 (Sun May 2 16:32:31 1999), 转信
网络工作室--知识库:编程技术:Unix编程:Linux程式设计入门:
Linux程式设计入门-zlib的运用
Linux程式设计入门-zlib的运用
gzip(*.gz)档案格式几乎是Linux下的标准格式了,有人认为bzip2的压缩率比
gzip来得高。一般来说,这个说法大致正确,不过根据我个人的经验,有一半
以上的档案,bzip2没有比gzip的压缩率来得高,有少数状况下,gzip压缩率反
而比bzip2来的高。
zlib是个支援gzip档案格式的函数库,它使得gz档的存取就犹如开档关档一样
地容易,您可以很容易地为您的程式加入gz档的支援。
使用范例:showgz.c
#include<stdio.h>
#include<stdlib.h>
#include<zlib.h>
voidmain(intargc,char**argv)
{
gzFilezip;
intc;
if(argc<2)return;
zip=gzopen(argv[1],"rb");
while((c=gzgetc(zip))!=EOF)putchar(c);
gzclose(zip);
}
编译
gcc-oshowgzshowgz.c-lz
检验
gzip-9<showgz.c>showgz.c.gz
./showgzshowgz.c.gz
将会把这个程式内容显示出来,showgz的作用可说等於gzip-dc。
函数宣告
gzFilegzopen(constchar*path,constchar*mode);
开启一个gzip(*.gz)档。
mode叁数可为"rb"或"wb"。
另外也可包含压缩程度如"wb9"。
用'f'作为过滤资料,如"wb6f"。
用'h'可指定Huffmanonly压缩,如"wb1h"
gzopen亦可用於读取非压缩的gzip档案格式,在这种状况下,gzread会直接读
取,而不进行解压缩。
intgzread(gzFilefile,voidpbuf,unsignedlen);
与read的用法相同。
intgzwrite(gzFilefile,constvoidpbuf,unsignedlen);
与write用法相同。
intgzprintf(gzFilefile,constchar*format,...);
与fprintf用法相同。
char*gzgets(gzFilefile,char*buf,intlen);
与fgets用法相同。
intgzputc(gzFilefile,intc);
与fputc用法相同。
intgzgetc(gzFilefile);
与fgetc用法相同。
intgzflush(gzFilefile,intflush);
与fflush作用相同。
z_off_tgzseek(gzFilefile,z_off_toffset,intwhence);
whence不支援SEEK_END
如果档案是开启为"读取",则SEEK_SET及SEEK_CUR,向前及向後均支援,不过
很慢就是了。
如果档案是开启为"写入",仅支援向前SEEK。
intgzrewind(gzFilefile);
与gzseek(file,0L,SEEK_SET)相同作用,仅在读取时有效。
z_off_tgztell(gzFilefile);
返回值:目前档案位置(解压缩後的位置)
intgzeof(gzFilefile);
返回值:1-EOF,0-notEOF
intgzclose(gzFilefile);
关闭档案
返回值:zliberrornumber
OKSTATION,Webmaster,BrianLin
admin@studio.openunix.org
--
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 202.118.239.115]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:6.986毫秒