landuochong 发表于 2011-12-21 08:44

使用libz和libbz2的API在项目中集成gzip和bzip2功能

<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">转载:http://blog.csdn.net/yui/archive/2010/07/01/5707842.aspx<br></span></span></p><p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">提供了一套与</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzip</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">有关的</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">API</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">提供了一套与</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">bzip2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">有关的</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">API</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">。我们可以利用其中几个常用的函数,在自己的项目中实现压缩、解压缩功能。这两个库文件一般在</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">linux</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">系统中都会有,如果没有,可以分别到以下网址下载其源代码:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">gzip: </span><a href="http://www.gzip.org/"><span style="font-size: small;">http://www.gzip.org/</span></a></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">bzip2: </span><a href="http://www.bzip.org/index.html"><span style="font-size: small;">http://www.bzip.org/index.html</span></a></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">最有用的函数有四个:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">gzFile gzopen(const char *path, const char *mode);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int gzclose(gzFile file);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int gzread(gzFile file, void *buf, unsigned len);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int gzwrite(gzFile file, const void *buf, unsigned len);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">追踪其源代码的话,可以发现,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzFile</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">也就是</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">void *</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">最有用的函数也有四个:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">BZFILE *BZ2_bzopen(const char *path, const char *mode);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">void BZ2_bzclose(BZFILE *file);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int BZ2_bzread(BZFILE *file, void *buf, int len);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int BZ2_bzwrite(BZFILE *file, void *buf, int len);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">追踪其源代码的话,可以发现,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZFILE</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">也就是</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">void</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">所以说,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的四个函数与</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的四个函数,无论从名字上看,还是从参数上看,都是如出一辙的。只不过,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzopen()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzread()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">可以打开和读取任何文件,而</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzopen()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzread()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">只能打开和读取</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">bzip2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">压缩的文件。</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">事实上,它们与普通文件的打开、关闭、读取、写入的四个函数,基本上是对应的:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">FILE *fopen(const char *path, const char *mode);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int fclose(FILE *fp);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int fread(void *buf, int size_of_element, int len, FILE *fp);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">int fwrite(void *buf, int size_of_element, int len, FILE *fp);</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">要打开一个打算读取的二进制文件,三个打开函数的调用分别是:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">gzopen("filename", "r");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">BZ2_bzopen("filename", "r");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">fopen("filename", "rb");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">要打开一个打算写入的二进制文件,三个打开函数的调用分别是:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">gzopen("filename", "w");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">BZ2_bzopen("filename", "w");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;; color: blue;" lang="EN-US"><span style="font-size: small;">fopen("filename", "w");</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">可以说参数的使用是基本一样的,不过</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzopen()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzopen()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">mode</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">参数一般没有</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">"rb"</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">而只有</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">"r"</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">,因为它们处理的基本上都是二进制文件,不需要特别指明。此外,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">mode</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">参数</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">还有其它用法,比如制定压缩率等,具体可以查看源代码。</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">不同的地方之一,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">FILE</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">是一个关于文件信息的结构体,而不是</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">void</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">类型,之二,</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzread()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">、</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzwrite()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">、</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzread()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzwrite()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的参数位置与</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fread()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fwrite()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">不一样,同时也省略了</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">size_of_element</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">参数</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">。</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">有了这两套</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">API</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">,我们就可以很方便地写出程序对文件进行压缩、解压缩操作,更多的是,把压缩、解压缩功能集成到自己的项目中去,使得项目支持压缩格式。举例如下:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<ol style="margin-top: 0cm;" type="1"><li class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-list: l1 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">如果使用</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">API</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">,压缩就是对源文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fopen()/fread()/fclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">操作,对目标文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzopen()/gzwrite()/gzclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">操作;解压缩就是对源文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">gzopen()/gzread()/gzclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">操作,对目标文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fopen()/fwrite()/fclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">操作</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></li><li class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-list: l1 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">如果使用</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">的</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">API</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">,压缩就是对源文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fopen()/fread()/fclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">操作,对目标文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzopen()/BZ2_bzwrite()/BZ2_bzclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">操作;解压缩就是对源文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BZ2_bzopen()/BZ2_bzread()/BZ2_bzclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">操作,对目标文件进行</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">fopen()/fwrite()/fclose()</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">操作</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></li></ol>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">如果要直接使用</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">,很简单,只需要做到三件事:</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="text-indent: -21.75pt; margin: 0cm 0cm 0pt 39.75pt; mso-list: l0 level1 lfo2; tab-stops: list 39.75pt;"><span style="font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Courier New';" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: small;">1.</span><span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">include</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">头文件。把</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">zlib.h</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">bzlib.h</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">包含到项目源程序中</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="text-indent: -21.75pt; margin: 0cm 0cm 0pt 39.75pt; mso-list: l0 level1 lfo2; tab-stops: list 39.75pt;"><span style="font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Courier New';" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: small;">2.</span><span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">链接库文件。如果由于权限问题不能安装库文件,需要在编译时指定库文件的路径</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="text-indent: -21.75pt; margin: 0cm 0cm 0pt 39.75pt; mso-list: l0 level1 lfo2; tab-stops: list 39.75pt;"><span style="font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Courier New';" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: small;">3.</span><span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">如果库文件没有安装在系统默认的搜索路径,运行前还要修改</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">LD_LIBRARY_PATH</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">环境变量,使得运行时能够找到库文件</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"><span style="font-size: small;">&nbsp;</span></span></p>
<span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">值得注意的是,以上列出的只是</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">和</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libbz2</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">里面最常用、比较高级的函数,其实,这两个库文件里还有其它底层的函数,利用这些底层函数,甚至可以解压</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">.Z</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">结尾的压缩文件。具体做法就要慢慢参透</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">libz</span><span style="font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New';">的全部源代码了。</span></span>
               
               
               
               
页: [1]
查看完整版本: 使用libz和libbz2的API在项目中集成gzip和bzip2功能