找回密码
 注册
搜索
查看: 14912|回复: 1

zlib用法简单说明

[复制链接]
发表于 2011-5-17 15:24:49 | 显示全部楼层 |阅读模式
本文的目的是: 简单说明如何把zlib加入到MFC程序中,提供内存压缩功能.# z2 |# g& x. L, C

- r7 Z9 k$ p# E" [( p/ j1. 如何获得zlib# F# l5 q) J# ?3 o! b4 P

2 u6 `# k  b8 G; [0 ^0 }) Azlib的主页是:http://www.zlib.net/
3 A4 d  m& B$ m4 `& t6 e
# [- r$ g% P+ y" f) y: u& `( M2. 用VC++6.0打开  {) Q2 d" W+ Q' K6 o6 p
7 c* x& c$ v5 o- \1 s+ c5 N# ^, Z
把下载的源代码解压打开,VC6.0的工程已经建好了,在\projects\visualc6. 双击zlib.dsw, 可以在VC++6.0中看到里面有3个工程: zlib 是库文件(编译设置选中 win32 lib debug / release), 工程example 是如何使用 zlib.lib 的示例, 工程minigzip 是如何用 zlib 提供的函数读写.gz文件的示例(*.gz的文件一般Linux下比较常用).) r( c! t0 G8 \+ b$ r# g/ b

7 C& o! F, |- Y7 A2 q+ l3. 如何加入到我的工程
0 ^5 r1 U- H# t1 r; H
3 f# Q: u. L+ M0 b$ b" S编译好 zlib.lib 后, 你就得到了调用一个静态库所需要的所有文件了(zlib.lib, zlib.h, zconf.h). 如何调用静态库不用我说了吧.8 R" v5 a: L- F% A7 }
# Z3 ^  v: W1 E# J
4. 用zlib能干什么
5 v1 `/ d" @8 t) }& J' }2 K$ d' x * ]' L2 O& W3 p# ]5 {6 t
先来看看 zlib 都提供了那些函数, 都在zlib.h中,看到一堆宏不要晕,其实都是为了兼容各种编译器和一些类型定义.死死抓住那些主要的函数的原型声明就不会受到这些东西的影响了.
% v  [$ K, h$ x/ l# i, D   ~' \1 Z# C: R% y2 n) [  b- N
关键的函数有那么几个:8 q  _9 g* |8 V

* R1 v& e$ S9 x7 `  E, U( z(1)int compress (Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen);
4 z/ s3 m% y: y- X7 H 9 j* m4 e/ Q, o  Z- G
把源缓冲压缩成目的缓冲, 就那么简单, 一个函数搞定
8 h$ e, T* s2 Y" z9 a6 Q  o: L8 L  x
# i( y/ E, s  X( `- l(2) int compress2 (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen,int level);+ q1 @1 i% K4 P* t; e9 s
/ v& F; I( i8 U* m6 u4 y8 @. R
功能和上一个函数一样,都一个参数可以指定压缩质量和压缩数度之间的关系(0-9)不敢肯定这个参数的话不用太在意它,明白一个道理就好了: 要想得到高的压缩比就要多花时间
4 H/ m* D2 N( Q1 [- h. T: z
/ W& H: z% D! J0 o! G$ ~: e(3) uLong compressBound (uLong sourceLen);7 e( L6 y& C8 x2 ^

& n7 L; b. h" L# |( ~' ]计算需要的缓冲区长度. 假设你在压缩之前就想知道你的产度为 sourcelen 的数据压缩后有多大, 可调用这个函数计算一下,这个函数并不能得到精确的结果,但是它可以保证实际输出长度肯定小于它计算出来的长度
5 I$ H# Y  [; b 2 l# U8 g" G, T( A% A5 o& F/ L! s6 b
(4) int uncompress (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen);
5 @5 I& D& f( |' V $ P0 c! o3 F: ]
解压缩(看名字就知道了:)
+ z8 ~! F; J8 a' f# a, r' L
6 p2 i% Z( Q% v& m, I! L' L(5) deflateInit() + deflate() + deflateEnd()
9 M% a1 r! N# a) _2 m: Q
, n3 z7 }% r- g! P% g8 K3个函数结合使用完成压缩功能,具体用法看 example.c 的 test_deflate()函数. 其实 compress() 函数内部就是用这3个函数实现的(工程 zlib 的 compress.c 文件)
# K- o6 V, T6 M+ a5 m5 U  p8 s ) a' N; `: G7 i9 {/ ^
(6) inflateInit() + inflate() + inflateEnd()
. |& o0 Z: t: s 6 @9 e% s* j0 ~5 J& H2 O8 l
和(5)类似,完成解压缩功能.
$ t$ B9 ^( z' I6 H/ ~0 s
1 ~+ M7 P; P+ b6 }! x. H$ l(7) gz开头的函数. 用来操作*.gz的文件,和文件stdio调用方式类似. 想知道怎么用的话看example.c 的 test_gzio() 函数,很easy.5 X. i0 ~6 D1 x, O+ G
4 u* i1 b. r0 G; J
(8) 其他诸如获得版本等函数就不说了. 3 n. U+ _: [4 v0 v5 @6 F% V
总结: 其实只要有了compress() 和uncompress() 两个函数,在大多数应用中就足够了.* k7 x( N9 w* N( {! m! k

6 ~' P  W' O$ i& G+ A9 {9 V2 [题外话: 我最初看到zlib的源代码时被好多宏吓倒了,呵呵,后来仔细看下去才发现原来接口那么简单. 至于那些英文说明也没想象中的那么难懂.只要有尝试的勇气,总能有些收获.
2 y, i; G& t/ V. s* b
& r3 O* f& m- r9 a1 {本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/querw/archive/2006/12/21/1452041.aspx! q) h+ B8 z% ~, C2 E0 e5 Y) c# |
 楼主| 发表于 2011-5-17 15:32:31 | 显示全部楼层

zlib 与 libpng 的配置与使用 part 2 zlib的安装

zlib 的安装
  libpng 是一套免费的、公开源代码的程序库,支持对 PNG 图形文件的创建、读写等操作。libpng 使用 zlib 程序库作为压缩引擎,zlib 也是著名的 gzip (GNU zip) 所采用的压缩引擎。
  我们首先安装zlib,从其官方网站下载最新的源程序,不妨假设文件名是zlib-1.1.4.tar.gz。网址:http://www.gzip.org/zlib/
   D:\ 建立 libpng 目录,将 zlib-1.1.4.tar.gz 释放到这个目录。尽管没有合适的makefile,我们仍然可以直接编译链接 zlib.lib
  1. D:\libpng\zlib-1.1.4>bcc32 -c -O2 -6 -w-8004 -w-8057 -w-8012 *.cBorland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borlandadler32.c:compress.c:crc32.c:deflate.c:example.c:gzio.c:infblock.c:infcodes.c:inffast.c:inflate.c:inftrees.c:infutil.c:maketree.c:minigzip.c:trees.c:uncompr.c:zutil.c: D:\libpng\zlib-1.1.4>tlib zlib.lib +adler32.obj +compress.obj +crc32.obj+deflate.obj +gzio.obj +infblock.obj +infcodes.obj 4 a: q8 o4 m" N  S* M7 [# K! D
  2. +inffast.obj +inflate.obj +inftrees.obj +infutil.obj +maketree.obj # A8 A/ f8 f  u
  3. +trees.obj+uncompr.obj +zutil.obj' F. ^1 w2 j% z6 v3 m6 b
  4. TLIB 4.5 Copyright (c) 1987, 1999 Inprise Corporation
复制代码
  注意,在 tlib 的命令行中,没有 example.obj minigzip.obj。接下来,测试 zlib.lib 是否编译成功,执行:
  1. D:\libpng\zlib-1.1.4>bcc32 minigzip.obj zlib.libBorland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 BorlandTurbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland D:\libpng\zlib-1.1.4>bcc32 example.obj zlib.libBorland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 BorlandTurbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland D:\libpng\zlib-1.1.4>exampleuncompress(): hello, hello!gzread(): hello, hello!gzgets() after gzseek: hello!inflate(): hello, hello!large_inflate(): OKafter inflateSync(): hello, hello!inflate with dictionary: hello, hello!
复制代码
执行 example.exe,看见“hello, hello!”,表明生成的 zlib.lib 是好的。
  zlib 是通用的压缩库,提供了一套 in-memory 压缩和解压函数,并能检测解压出来的数据的完整性(integrity)zlib 也支持读写 gzip (.gz) 格式的文件。下面介绍两个最有用的函数——compress uncompress
  1. int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
复制代码
  compress函数将 source 缓冲区中的内容压缩到 dest 缓冲区。 sourceLen 表示source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen表示 dest 缓冲区的大小,destLen > (sourceLen + 12)*100.1%。当函数退出后,destLen 表示压缩后缓冲区的实际大小。此时 destLen / sourceLen 正好是压缩率。
  compress 若成功,则返回 Z_OK;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR
  1. int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
复制代码
  uncompress 函数将 source 缓冲区的内容解压缩到 dest 缓冲区。sourceLen source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen 表示 dest 缓冲区的大小, dest 缓冲区要足以容下解压后的数据。在进行解压缩时,需要提前知道被压缩的数据解压出来会有多大。这就要求在进行压缩之前,保存原始数据的大小(也就是解压后的数据的大小)。这不是 zlib 函数库的功能,需要我们做额外的工作。当函数退出后, destLen 是解压出来的数据的实际大小。
  uncompress 若成功,则返回 Z_OK ;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR。若输入数据有误,则返回 Z_DATA_ERROR
  zlib 带的 example.c 是个很好的学习范例,值得一观。我们写个程序,验证 zlib 的压缩功能。所写的测试程序保存为 testzlib.cpp ,放在 zlib-1.1.4 目录下。程序源代码:
  1. // testzlib.cpp 简单测试 zlib 的压缩功能#include <cstring>#include <cstdlib>#include
    3 f" t$ f0 N& n+ Y
  2. <iostream>#include "zlib.h" using namespace
    3 c8 g% G4 P; S6 h9 H) K: L! V7 @
  3. std; int main(){int err;Byte compr[200], uncompr[200];// big enoughuLong comprLen, uncomprLen;const char* hello = "12345678901234567890123456789012345678901234567890";uLong len = strlen(hello) + 1;comprLen = sizeof(compr) / sizeof(compr[0]);err = compress(compr, &comprLen, (const Bytef*)hello, len);if (err != Z_OK) {cerr << "compess error: " << err << '\n';exit(1);}cout << "orignal size: " << len << " , compressed size : " << comprLen << '\n';strcpy((char*)uncompr, "garbage");err = uncompress(uncompr, &uncomprLen, compr, comprLen);if (err != Z_OK) {cerr << "uncompess error: " << err << '\n';exit(1);}cout << "orignal size: " << len << " , uncompressed size : " << uncomprLen << '\n';if (strcmp((char*)uncompr, hello)) {cerr << "BAD uncompress!!!\n";exit(1);} else {cout << "uncompress() succeed: \n" << (char *)uncompr;}}
复制代码
编译执行这个程序,输出应该是
  1. D:\libpng\zlib-1.1.4>bcc32 testzlib.cpp zlib.lib D:\libpng\zlib-1.1.4>testzliborignal size: 51 , compressed size : 22orignal size: 51 , uncompressed size : 51uncompress() succeed:12345678901234567890123456789012345678901234567890
复制代码
至此, zlib的安装任务算是完成了。为了以后使用方便,我将zlib.libzlib.hzconf.h拷贝到D:\mylibs\
- t" M* O1 I  c  h# `
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2026-1-16 03:40 , Processed in 0.017665 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表