IamEasy_Man

Filled With Confidence , And Never Say Give Up !

【转载 】zlib数据压缩库的使用

  【转自】http://blog.csdn.net/pknife/archive/2009/04/30/4138918.aspx

  zlib是用于数据压缩的函数库,在windowslinux上都被广泛应用。当然,windows mobile上也可以顺利地使用该函数库。

  先进入到下面的地址去下载一个包,是专门针对wince系统的:http://www.tenik.co.jp/~adachi/wince/zlibce/index.html。其中Download有三项,如果你并不想深入源码的话,选择第三项zlib for WindowsCE Ver.1.1.4(with binaries)

  下载到本地后解开,在zlibce目录下,有我们需要的头文件zconf.hzlib.h;至于库文件则在zlibce\WCE400\ARMV4I下,zlibce.lib

  VS2005(ECV4.0当然也可以)下新建一测试工程,把上述头文件和库文件加入工程。

  同时,在你需要的地方添加以下代码:

#include "zlib.h"
#pragma comment(lib , "zlibce.lib")

  好了,我们现在可以来使用zlib库了。

  假如,现在有一段数据pBuf要压缩,数据长度为nLen,使用下面的代码:

gzFile zipFile = gzopen("\\Program Files\\test.gz""wb");
gzwrite(zipFile, (voidp)pBuf, nLen);
gzclose(zipFile);

  貌似很简单的说。注意,gzopen的第一个参数为const char*型,跟wince下文件路径参数类型不一致。如果pBuf类型为char*nLen千万不要使用strlen()什么的,至于原因,就...

  压缩成功了,再看看如何把test.gz文件解压缩:

gzFile Unzip = gzopen("\\Program Files\\test.gz""rb");

HANDLE  hFile = CreateFile( L"\\Program Files\\test.dat",
          GENERIC_WRITE,
          
0,
          NULL,
          OPEN_ALWAYS,
          FILE_ATTRIBUTE_NORMAL,
          NULL
          );

if( hFile != INVALID_HANDLE_VALUE )
{
    DWORD dw;
    BYTE pBuf[1000];
    
int nLen;
    
while(true)
    {
        nLen = gzread(Unzip, (voidp)pBuf, 1000);
        
if(nLen == 0)
        {
            
break;
        }
        WriteFile(hFile, pBuf, nLen, &dw, NULL);
    }

   gzclose(Unzip);
   CloseHandle(hFile);

}

  其他函数的用法,可以参考zlib.h

  附上一段使用的代码:

代码
  1 #include "UniBase.h"
  2 #include "ZLib/ZLib.h"
  3 #pragma comment(lib, "ZLib/ZLib.lib")
  4 
  5 // Values used in typeflag field
  6 #define REGTYPE       '0'       // regular file
  7 #define AREGTYPE      '\0'      // regular file
  8 #define LNKTYPE       '1'       // link
  9 #define SYMTYPE       '2'       // reserved
 10 #define CHRTYPE       '3'       // character special
 11 #define BLKTYPE       '4'       // block special
 12 #define DIRTYPE       '5'       // directory
 13 #define FIFOTYPE      '6'       // FIFO special
 14 #define CONTTYPE      '7'       // reserved
 15 
 16 //  GNU tar extensions
 17 #define GNUTYPE_DUMPDIR   'D'       // file names from dumped directory
 18 #define GNUTYPE_LONGLINK  'K'       // long link name
 19 #define GNUTYPE_LONGNAME  'L'       // long file name
 20 #define GNUTYPE_MULTIVOL  'M'       // continuation of file from another volume
 21 #define GNUTYPE_NAMES   'N'       // file name that does not fit into main hdr
 22 #define GNUTYPE_SPARSE    'S'       // sparse file
 23 #define GNUTYPE_VOLHDR    'V'       // tape/volume hdr
 24 
 25 
 26 #define MAX_BLOCK 512
 27 #define MAX_FNAME 100
 28 
 29 struct TARHDR
 30 {
 31  CHAR name[100];               //   0
 32  CHAR mode[8];                 // 100
 33  CHAR uid[8];                  // 108
 34  CHAR gid[8];                  // 116
 35  CHAR size[12];                // 124
 36  CHAR mtime[12];               // 136
 37  CHAR chksum[8];               // 148
 38  CHAR typeflag;                // 156
 39  CHAR linkname[100];           // 157
 40  CHAR magic[6];                // 257
 41  CHAR version[2];              // 263
 42  CHAR uname[32];               // 265
 43  CHAR gname[32];               // 297
 44  CHAR devmajor[8];             // 329
 45  CHAR devminor[8];             // 337
 46  CHAR prefix[155];             // 345
 47 };
 48 
 49 typedef union _TARBUF
 50 {
 51  TARHDR hdr;
 52  CHAR buf[MAX_BLOCK];
 53 }
 54 TARBUF, *PTARBUF;
 55 
 56 
 57 // Convert octal digits to INT
 58 INT GetOct(PSTR p,INT iWidth)
 59 {
 60  INT iResult = 0;
 61  while (iWidth--)
 62  {
 63    CHAR c = *p++;
 64    if (c == 0)
 65    {
 66      break;
 67    }
 68    if (c == ' ')
 69    {
 70      continue;
 71    }
 72    if (c < '0' || c > '7')
 73    {
 74      return -1;
 75    }
 76    iResult = iResult * 8 + (c - '0');
 77  }
 78  return iResult;
 79 }
 80 
 81 INT XTar(PTSTR ptzCmd)
 82 {
 83  PTSTR ptzOutDir = ptzCmd;
 84  for (; *ptzOutDir; ptzOutDir++)
 85  {
 86    if (*ptzOutDir == ',')
 87    {
 88      *ptzOutDir++ = 0;
 89      break;
 90    }
 91  }
 92 
 93  CHAR szPath[MAX_BLOCK];
 94  UStrToAStr(szPath, ptzCmd, MAX_PATH);
 95  gzFile gz = gzopen(szPath,"rb");
 96  if (!gz)
 97  {
 98    return -1;
 99  }
100 
101  INT iGetHeader = 1;
102  INT iRemaining = 0;
103  BOOL bFail = FALSE;
104  HANDLE hFile = NULL;
105  TCHAR tzName[MAX_PATH];
106  do
107  {
108    TARBUF buf;
109    INT iLen = gzread(gz, &buf, MAX_BLOCK);
110    if (iLen < 0)
111    {
112      return -2;
113    }
114 
115    // Always expect complete blocks to process the tar information.
116    if (iLen != MAX_BLOCK)
117    {
118      bFail = TRUE;
119      iRemaining = 0;
120    }
121 
122    // If we have to get a tar header
123    if (iGetHeader >= 1)
124    {
125      // if we met the end of the tar or the end-of-tar block, we are done
126      if (iLen == 0 || buf.hdr.name[0== 0)
127      {
128        break;
129      }
130 
131      if (iGetHeader == 1)
132      {
133        UAStrToStr(tzName, buf.hdr.name, MAX_PATH);
134        UStrRep(tzName, '/''\\');
135      }
136      else
137      {
138        iGetHeader = 1;
139      }
140 
141      // Act according to the type flag
142      switch (buf.hdr.typeflag)
143      {
144      case DIRTYPE:
145        TCHAR tzPath[MAX_PATH];
146        UStrPrint(tzPath, TEXT("%s\\%s\\"), ptzOutDir, tzName);
147        UDirCreate(tzPath);
148        break;
149 
150      case REGTYPE:
151      case AREGTYPE:
152        iRemaining = GetOct(buf.hdr.size, 12);
153        if (iRemaining == -1)
154        {
155          bFail = TRUE;
156          break;
157        }
158        else
159        {
160          UStrPrint(tzPath, TEXT("%s\\%s"), ptzOutDir, tzName);
161          UDirCreate(tzPath);
162          hFile = UFileOpen(tzPath, UFILE_WRITE);
163        }
164        iGetHeader = 0;
165        break;
166 
167      case GNUTYPE_LONGLINK:
168      case GNUTYPE_LONGNAME:
169        iRemaining = GetOct(buf.hdr.size, 12);
170        if (iRemaining < 0 || iRemaining >= MAX_BLOCK)
171        {
172          bFail = TRUE;
173          break;
174        }
175        iLen = gzread(gz, szPath, MAX_BLOCK);
176        if (iLen < 0)
177        {
178          return -4;
179        }
180        if (szPath[MAX_BLOCK - 1!= 0 || (INT) strlen(szPath) > iRemaining)
181        {
182          bFail = TRUE;
183          break;
184        }
185        iGetHeader = 2;
186        break;
187      }
188    }
189    else
190    {
191      UINT uSize = (iRemaining > MAX_BLOCK) ? MAX_BLOCK : iRemaining;
192      if (hFile)
193      {
194        UFileWrite(hFile, &buf, uSize);
195      }
196      iRemaining -= uSize;
197    }
198 
199    if (iRemaining == 0)
200    {
201      iGetHeader = 1;
202      if (hFile)
203      {
204        UFileClose(hFile);
205        hFile = NULL;
206      }
207    }
208  }
209  while (!bFail);
210 
211  gzclose(gz);
212  return 0;
213 }

 

 

posted on 2009-12-14 19:14  IamEasy_Man  阅读(1243)  评论(0)    收藏  举报

导航