logo

Navigation:First_Community->Software->Linux development Goto:New TopicSettingSearch
Linux程式设计- 7.zlib的运用
Posted by: imac Date: March 29, 2008 02:00AM
gzip(*.gz)档案格式几乎是Linux下的标准格式了,有人认为bzip2的压缩率比gzip来得高。一般来说,这个说法大致正确,不过根据我个人的经验,有一半以上的档案,bzip2没有比gzip的压缩率来得高,有少数状况下,gzip压缩率反而比bzip2来的高。 

CopyRight owned by the original author.--(www.MegaEntry.com)

zlib是个支援gzip档案格式的函数库,它使得gz档的存取就犹如开档关档一样地容易,您可以很容易地为您的程式加入gz档的支援。  -------------------------------------------------------------------------使用例 : showgz.c

CopyRight owned by the original author.--(www.MegaEntry.com)

#include   #include   #include   void main(int argc,char **argv)  {    gzFile zip; 

MegaEntry - Social networking and discussion site!

  int c;    if (argc<2) return;    zip = gzopen(argv[1], "rb ");    while ((c=gzgetc(zip))!=EOF) putchar(c); 

MegaEntry - Social networking and discussion site!

  gzclose(zip);  }  编译 gcc -o showgz showgz.c -lz  检验

CopyRight owned by the original author.--(www.MegaEntry.com)

gzip -9 < showgz.c > showgz.c.gz  ./showgz showgz.c.gz  将会把这个程式内容显示出来,showgz的作用可说等於gzip -dc。 

CopyRight owned by the original author.--(www.MegaEntry.com)

-------------------------------------------------------------------------函数宣告 gzFile gzopen  (const char *path, const char *mode);  开启一个gzip(*.gz)档。  mode参数可为 "rb "或 "wb "。  另外也可包含压缩程度如 "wb9 "。 

CopyRight owned by the original author.--(www.MegaEntry.com)

用 'f '作为过滤资料,如 "wb6f "。  用 'h '可指定Huffman only压缩,如 "wb1h "  gzopen亦可用於读取非压缩的gzip档案格式,在这种状况下,gzread会直接读取,而不进行解压缩。     int gzread (gzFile file, voidp buf, unsigned len);  与read的用法相同。 

MegaEntry - Social networking and discussion site!

int gzwrite (gzFile file, const voidp buf, unsigned len);  与write用法相同。  int gzprintf (gzFile file, const char *format, ...);  与fprintf用法相同。 

CopyRight owned by the original author.--(www.MegaEntry.com)

char * gzgets (gzFile file, char *buf, int len);  与fgets用法相同。  int gzputc (gzFile file, int c);  与fputc用法相同。 

MegaEntry - Social networking and discussion site!

int gzgetc (gzFile file);  与fgetc用法相同。  int gzflush (gzFile file, int flush);  与fflush作用相同。 

MegaEntry - Social networking and discussion site!

z_off_t gzseek (gzFile file, z_off_t offset, int whence);  whence不支援SEEK_END  如果档案是开启为 "读取 ",则SEEK_SET及SEEK_CUR,向前及向後均支援,不过很慢就是了。  如果档案是开启为 "写入 ",仅支援向前SEEK。 

CopyRight owned by the original author.--(www.MegaEntry.com)

int gzrewind (gzFile file);  与gzseek(file, 0L, SEEK_SET)相同作用,仅在读取时有效。  z_off_t gztell (gzFile file);  返回值 : 目前档案位置(解压缩後的位置) 

CopyRight owned by the original author.--(www.MegaEntry.com)

int gzeof (gzFile file);  返回值 : 1 - EOF, 0 - not EOF  int gzclose (gzFile file);  关闭档案  返回值 : zlib error number 

MegaEntry - Social networking and discussion site!



Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect