1. 如何获得zlib

zlib的主页是:http://www.zlib.net/

2. 用VC++6.0打开

把 下载的源代码解压打开,VC6.0的工程已经建好了,在\projects\visualc6. 双击zlib.dsw, 可以在VC++6.0中看到里面有3个工程: zlib 是库文件(编译设置选中 win32 lib debug / release), 工程example 是如何使用 zlib.lib 的示例, 工程minigzip是如何用 zlib 提供的函数读写.gz文件的示例(*.gz的文件一般Linux下比较常用).

3. 如何加入到我的工程

编译好 zlib.lib 后, 你就得到了调用一个静态库所需要的所有文件了(zlib.lib, zlib.h, zconf.h). 如何调用静态库不用我说了吧.

4. 用zlib能干什么

先来看看 zlib 都提供了那些函数, 都在zlib.h中,看到一堆宏不要晕,其实都是为了兼容各种编译器和一些类型定义.死死抓住那些主要的函数的原型声明就不会受到这些东西的影响了.

关键的函数有那么几个:

(1)int compress (Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen);

把源缓冲压缩成目的缓冲, 就那么简单, 一个函数搞定

(2) int compress2 (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen,int level);

功能和上一个函数一样,都一个参数可以指定压缩质量和压缩数度之间的关系(0-9)不敢肯定这个参数的话不用太在意它,明白一个道理就好了:要想得到高的压缩比就要多花时间

(3) uLong compressBound (uLong sourceLen);

计算需要的缓冲区长度. 假设你在压缩之前就想知道你的产度为sourcelen 的数据压缩后有多大, 可调用这个函数计算一下,这个函数并不能得到精确的结果,但是它可以保证实际输出长度肯定小于它计算出来的长度

(4) int uncompress (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen);

解压缩(看名字就知道了:)

(5) deflateInit() + deflate() + deflateEnd()

3个函数结合使用完成压缩功能,具体用法看 example.c 的test_deflate()函数. 其实 compress() 函数内部就是用这3个函数实现的(工程 zlib 的 compress.c 文件)

(6) inflateInit() + inflate() + inflateEnd()

和(5)类似,完成解压缩功能.

(7) gz开头的函数. 用来操作*.gz的文件,和文件stdio调用方式类似.想知道怎么用的话看example.c 的 test_gzio() 函数,很easy.

(8) 其他诸如获得版本等函数就不说了.

总结: 其实只要有了compress() 和uncompress() 两个函数,在大多数应用中就足够了.

题外话: 我最初看到zlib的源代码时被好多宏吓倒了,呵呵,后来仔细看下去才发现原来接口那么简单. 至于那些英文说明也没想象中的那么难懂.只要有尝试的勇气,总能有些收获.

#include <iostream>
#include <stdlib.h>
#include "zlib.h"

using namespace std;

#define MaxBufferSize 1024*10

void main()
{
    int i;

FILE* File_src;
     FILE* File_tmp;
     FILE* File_dest;

unsigned long   len_src;
     unsigned long len_tmp;
     unsigned long len_dest;

unsigned char *buffer_src  = new unsignedchar[MaxBufferSize];
     unsigned char *buffer_tmp  = new unsignedchar[MaxBufferSize];
     unsigned char *buffer_dest = new unsignedchar[MaxBufferSize];

File_src = fopen("src.txt","r");
     len_src = fread(buffer_src,sizeof(char),MaxBufferSize-1,File_src);

for(i = 0 ; i < len_src ; ++i)
    {
         cout<<buffer_src[i];
     }
     cout<<endl;
     compress(buffer_tmp,&len_tmp,buffer_src,len_src);

File_tmp = fopen("tmp.txt","w");
     fwrite(buffer_tmp,sizeof(char),len_tmp,File_tmp);

for(i = 0 ; i < len_tmp ; ++i)
    {
         cout<<buffer_tmp[i];
     }
     cout<<endl;

uncompress(buffer_dest,&len_dest,buffer_tmp,len_tmp);

File_tmp = fopen("tmp.txt","r");
     File_dest = fopen("dest.txt","w");
     fwrite(buffer_dest,sizeof(char),len_dest,File_dest);

for(i = 0 ; i < len_dest ; ++i)
    {
         cout<<buffer_dest[i];
     }
     cout<<endl;

}

Write to file :

char * pchData = "xxx..." ;    
gzFile fData = gzopen(pchFile,"wb");    
gzwrite(fData,pchData,strlen(pchData));    
gzclose(fData);

read from file :

char pchData[1024];    
gzFile fData = gzopen(pchFile,"rb");    
int n = gzread(fData,pchData,1024);    
gzclose(fData);

----------------------------------------------------------------------------------------------------------------

使用zlib压缩解压缩文件的详细过程

zlib是一套公开源代码的压缩,解压缩的函数库,提供了很多文件操作的方法,但是他不是一套类库,所以有兴趣的人都可以把他进行封装,实现自己的类库,和更高层的接口。
具体的介绍可以参考http://www.gzip.org/zlib/主页,这里有详细介绍。
    这里简单实现了zlib的最简单的用法,压缩一个文件,通过使用文件映射来实现的。
    包含头文件 zlib.h 和 zconf.h 和 zlib.lib
在stdafx.h 中加入:
#ifdef _DEBUG
#pragma comment(lib,"zlibd.lib")
#else
#pragma comment(lib,"zlib.lib")
#endif
#include "zlib.h"
#include "zconf.h"
压缩代码:

HANDLE hFile, hFileToWrite;
 CString strFilePath;
 m_ctrEdit.GetWindowText(strFilePath);
 
 //打开要进行压缩的文件
 hFile = CreateFile(strFilePath, // file name
  GENERIC_READ, // open for reading
  FILE_SHARE_READ, // share for reading
  NULL, // no security
  OPEN_EXISTING, // existing file only
  FILE_ATTRIBUTE_NORMAL, // normal file
  NULL); // no attr. template
 
 if (hFile == INVALID_HANDLE_VALUE)
 {
  AfxMessageBox("Could not open file to read"); // process error
  return;
 }
 
 HANDLE hMapFile, hMapFileToWrite;
 
 //创建一个文件映射
 hMapFile = CreateFileMapping(hFile, // Current file handle.
  NULL, // Default security.
  PAGE_READONLY, // Read/write permission.
  0, // Max. object size.
  0, // Size of hFile.
  "ZipTestMappingObjectForRead"); // Name of mapping object.
 
 if (hMapFile == NULL)
 {
  AfxMessageBox("Could not create file mapping object");
  return;
 }
 
 LPVOID lpMapAddress, lpMapAddressToWrite;
 
 //创建一个文件映射的视图用来作为source
 lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
  FILE_MAP_READ, // Read/write permission
  0, // Max. object size.
  0, // Size of hFile.
  0); // Map entire file.
 
 if (lpMapAddress == NULL)
 {
  AfxMessageBox("Could not map view of file");
  return;
 }
 
 //////////////////////////////////////////////////////////////////////////////////
 DWORD dwFileLength,dwFileLengthToWrite;
 dwFileLength = GetFileSize(hFile, NULL);
 m_dwSourceFileLength = dwFileLength;
 //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小,
 // 解压缩的时候用,当然还可以保存更多的信息,这里用不到
 dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
 
 //以下是创建一个文件,用来保存压缩后的文件
 hFileToWrite = CreateFile("demoFile.rar", // demoFile.rar
  GENERIC_WRITE|GENERIC_READ, // open for writing
  0, // do not share
  NULL, // no security
  CREATE_ALWAYS, // overwrite existing
  FILE_ATTRIBUTE_NORMAL , // normal file
  NULL); // no attr. template
 
 if (hFileToWrite == INVALID_HANDLE_VALUE)
 {
  AfxMessageBox("Could not open file to write"); // process error
  return;
 }
 
 hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle.
  NULL, // Default security.
  PAGE_READWRITE, // Read/write permission.
  0, // Max. object size.
  dwFileLengthToWrite, // Size of hFile.
  "ZipTestMappingObjectForWrite"); // Name of mapping object.
 
 if (hMapFileToWrite == NULL)
 {
  AfxMessageBox("Could not create file mapping object for write");
  return;
 }
 
 lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object.
  FILE_MAP_WRITE, // Read/write permission
  0, // Max. object size.
  0, // Size of hFile.
  0); // Map entire file.
 
 if (lpMapAddressToWrite == NULL)
 {
  AfxMessageBox("Could not map view of file");
  return;
 }
 
 //这里是将压缩前的大小保存在文件的第一个DWORD里面
 LPVOID pBuf = lpMapAddressToWrite;
 (*(DWORD*)pBuf) = dwFileLength;
 pBuf = (DWORD*)pBuf + 1;
 
 
 //////////////////////////////////////////////////////////////////////
 
 //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存
 //原形如下:
 //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
 //参数destLen返回实际压缩后的文件大小。
 compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);
 
 //////////////////////////////////////////////////////////////////////
 
 UnmapViewOfFile(lpMapAddress);
 CloseHandle(hMapFile);
 CloseHandle(hFile);
 
 UnmapViewOfFile(lpMapAddressToWrite);
 CloseHandle(hMapFileToWrite);
 //这里将文件大小重新设置一下
 SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN);
 SetEndOfFile(hFileToWrite);
 CloseHandle(hFileToWrite);

解压缩的方法其他地方都一样,不同的就是使用方法是uncompress而不是compress
原形如下:

int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
解压缩代码如下:

HANDLE hFile, hFileToWrite;
 CString strFilePath;
 m_ctrEdit.GetWindowText(strFilePath);
 
 //打开要进行解压缩的文件
 hFile = CreateFile(strFilePath, // file name
  GENERIC_READ, // open for reading
  FILE_SHARE_READ, // share for reading
  NULL, // no security
  OPEN_EXISTING, // existing file only
  FILE_ATTRIBUTE_NORMAL, // normal file
  NULL); // no attr. template
 
 if (hFile == INVALID_HANDLE_VALUE)
 {
  AfxMessageBox("Could not open file to read"); // process error
  return;
 }
 
 HANDLE hMapFile, hMapFileToWrite;
 
 //创建一个文件映射
 hMapFile = CreateFileMapping(hFile, // Current file handle.
  NULL, // Default security.
  PAGE_READONLY, // Read/write permission.
  0, // Max. object size.
  0, // Size of hFile.
  "ZipTestMappingObjectForRead"); // Name of mapping object.
 
 if (hMapFile == NULL)
 {
  AfxMessageBox("Could not create file mapping object");
  return;
 }
 
 LPVOID lpMapAddress, lpMapAddressToWrite;
 
 //创建一个文件映射的视图用来作为source
 lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
  FILE_MAP_READ, // Read/write permission
  0, // Max. object size.
  0, // Size of hFile.
  0); // Map entire file.
 
 if (lpMapAddress == NULL)
 {
  AfxMessageBox("Could not map view of file");
  return;
 }
 
 //////////////////////////////////////////////////////////////////////////////////
 DWORD dwFileLength,dwFileLengthToWrite;
 dwFileLength = GetFileSize(hFile, NULL) - sizeof(DWORD);
 //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小,
 // 解压缩的时候用,当然还可以保存更多的信息,这里用不到
// dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
 dwFileLengthToWrite = (*(DWORD*)lpMapAddress);

LPVOID pSourceBuf = lpMapAddress;
 pSourceBuf = (DWORD*)pSourceBuf + 1;
 
 //以下是创建一个文件,用来保存压缩后的文件
 hFileToWrite = CreateFile("demoFile.pdf", // create demo.gz
  GENERIC_WRITE|GENERIC_READ, // open for writing
  0, // do not share
  NULL, // no security
  CREATE_ALWAYS, // overwrite existing
  FILE_ATTRIBUTE_NORMAL , // normal file
  NULL); // no attr. template
 
 if (hFileToWrite == INVALID_HANDLE_VALUE)
 {
  AfxMessageBox("Could not open file to write"); // process error
  return;
 }
 
 hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle.
  NULL, // Default security.
  PAGE_READWRITE, // Read/write permission.
  0, // Max. object size.
  dwFileLengthToWrite, // Size of hFile.
  "ZipTestMappingObjectForWrite"); // Name of mapping object.
 
 if (hMapFileToWrite == NULL)
 {
  AfxMessageBox("Could not create file mapping object for write");
  return;
 }
 
 lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object.
  FILE_MAP_WRITE, // Read/write permission
  0, // Max. object size.
  0, // Size of hFile.
  0); // Map entire file.
 
 if (lpMapAddressToWrite == NULL)
 {
  AfxMessageBox("Could not map view of file");
  return;
 }
 
 //这里是将压缩前的大小保存在文件的第一个DWORD里面
 LPVOID pBuf = lpMapAddressToWrite;
 
 
 //////////////////////////////////////////////////////////////////////
 
 //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存
 //原形如下:
 //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
 //参数destLen返回实际压缩后的文件大小。
 uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);
 
 //////////////////////////////////////////////////////////////////////
 
 UnmapViewOfFile(lpMapAddress);
 CloseHandle(hMapFile);
 CloseHandle(hFile);
 
 UnmapViewOfFile(lpMapAddressToWrite);
 CloseHandle(hMapFileToWrite);
 //这里将文件大小重新设置一下
 SetFilePointer(hFileToWrite,dwFileLengthToWrite ,NULL,FILE_BEGIN);
 SetEndOfFile(hFileToWrite);
 CloseHandle(hFileToWrite);

zlib用法说明的更多相关文章

  1. SqlServer与MySql的一些常用用法的差别

    最近学习了一下mySql,总结一下SqlServer不同一些用法: 操作符优先级以下列表显示了操作符优先级的由低到高的顺序.排列在同一行的操作符具有相同的优先级.:=||, OR, XOR&& ...

  2. 【转】 C++使用zlib库(-)

    来自:  http://blog.chinaunix.net/uid-24607609-id-2118143.html   今天看到一个gzopen函数,搜了一下他的系列函数,及相关用法   C++使 ...

  3. [Linux] yum和apt-get用法及区别

    一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...

  4. Node基础:资源压缩之zlib

    概览 做过web性能优化的同学,对性能优化大杀器gzip应该不陌生.浏览器向服务器发起资源请求,比如下载一个js文件,服务器先对资源进行压缩,再返回给浏览器,以此节省流量,加快访问速度. 浏览器通过H ...

  5. Linux中yum和apt-get用法及区别

    Linux中yum和apt-get用法及区别   一般来说著名的linux系统基本上分两大类:   1.RedHat系列:Redhat.Centos.Fedora等   2.Debian系列:Debi ...

  6. 了解PHP中Stream(流)的概念与用法(转)

    Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...

  7. saltstack:使用教程之二高级模块用法Grains、Pillar

    1.grains用法: 在客户端服务启动的时候收集客户的基础信息,在配置发生变化后也可以通过master重新同步 显示一个客户端的所有项目: [root@node5 ~]# salt "no ...

  8. Node.js ZLIB

    Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...

  9. shell 函数用法

    近期在学习shell编程方面的知识,写的不怎么好,请大家多多指点,下面给大家分享一下shell函数的用法. 我们为什么要用shell函数? 简单的说,函数的作用就是把程序多次调用相同的代码部分定义成一 ...

随机推荐

  1. JDBC 学习笔记(二)—— 大数据+存储过程+批处理+事务管理

    本文目录:       1.使用JDBC处理大数据        2.使用JDBC处理大文本        3.使用JDBC处理二进制数据        4.Oracle中大数据处理        5 ...

  2. 编写一段程序,从标准输入读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止。使用while循环一次读取一个单词,当一个单词连续出现两次是使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有人任何单词是重复出现的。

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  3. ASP.NET MVC 中CSS JS压缩合并 功能的使用方法

    通过压缩合并js文件和css文件,可以减少 服务器的响应 次数和 流量,可以大大减小服务器的压力,对网站优化有比较明显的帮助!压缩合并 css 文件和js文件是网站优化的一个 比较常用的方法. ASP ...

  4. d3d11 effect state and default value tables

    Blend state State Default ValueAlphaToCoverage Enable FALSEIndependentBlend Enable FALSERenderTarget ...

  5. Tower of Hanoi问题

    [问题描述] 有A, B, C三个塔座,A上套有n个直径不同的圆 盘,按直径从小到大叠放,形如宝塔,编号1, 2, 3 … n. 要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循 下列原则: w ...

  6. Castle 开发系统文章

    转: http://www.cnblogs.com/Jebel/archive/2008/06/24/1228766.html

  7. Lambda Action Func练习

    namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] a ...

  8. nginx的健康检查功能将挂掉的Tomcat舍弃

    1.Ngninx自带健康检查功能,能将挂掉的服务器舍弃,不在访问路径里 2.Nginx还有URL重写功能,能将接收到的请求,进行改写,再将新的URL分发到后端服务器上

  9. 87. Scramble String

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  10. Servlet编写登录界面

    package com.mhb; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servlet ...