参考网址:
开源代码:http://www.zlib.net
zlib使用手册:http://www.zlib.net/manual.html
zlib wince版:http://www.tenik.co.jp/~adachi/wince/
在这里,你可以查看基于各种操作系统平台的压缩与解缩代码实现。
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
以下是经过测试的 WinCE 及 WinXP 下的代码
<<<<<<<<
第一步: 首先到www.zlib.net下载个ZLIB,
WinXP: 解压缩后打开zlib-1.2.3/projects/visualc6/zlib.dsw,选择Win32 LIB Release 按F7编绎生成zlib.lib, zlib.dll.
WinCE: 下载一个for Windows CE 版的包,里面针对各种平台(ARM4, ARM4I, MIPS, X86)有对应的zlibce.dll zlibce.lib.
<<<<<<<<
第二步: 建立EVC 或者 VC6 的对话框工程.
在工程中添加以下文件:zlib.h, zconf.h, zlib.lib, zlib.dll (或者 zlibce.dll);
这3个文件就在刚才从www.zlib.net下载的软件包中.
<<<<<<<<
第三步: 包含头文件
#include "zlib.h"
#include "StorageDeal.h"
其中CStorageDeal是我写的文件处理类,功能类似于CFile类。它的类定义如下:
class CStorageDeal : public CStatic
{
// Construction
public:
CStorageDeal();
virtual ~CStorageDeal();
// Attributes
public:
CFile myFile;
DWORD myFileLength;
BOOL FileOpenStaus;
public:
BOOL OpenFile(CString pFileName);
BOOL CloseFile();
DWORD ReadBlockData(BYTE *pData, DWORD blockIndex, DWORD idIndex);
DWORD WriteBlockData(BYTE *pData, DWORD blockIndex, DWORD idIndex);
DWORD GetFileLength();
DWORD SetFileLength(DWORD length);
DWORD ReadData(BYTE *pData, DWORD position, DWORD length);
DWORD WriteData(BYTE *pData, DWORD position, DWORD length);
static BOOL DeleteFile(CString pFileName);
static BOOL RenameFile(CString pOldName, CString pNewName);
};
<<<<<<<<
第四步: 添加压缩与解压缩代码
压缩代码:
BOOL CMyZip_evcDlg::zip(CString srcFileName, CString dstFileName)
{
#ifdef NO_GZCOMPRESS
MessageBox(_T("NO_GZCOMPRESS -- gz* functions cannot compress"));
return FALSE;
#endif
// 打开待压缩的文件
CStorageDeal unzipFile;
unzipFile.OpenFile(srcFileName);
int fileLen = unzipFile.GetFileLength();
// 创建压缩文件
gzFile zipFile;
CString zipFileName = srcFileName +_T(".gz");
int zipFileNameLen = zipFileName.GetLength();
#ifdef ARM // WinCE下代码
char nameBuff[100]={0};
WideCharToMultiByte(CP_ACP,0,zipFileName,zipFileNameLen,nameBuff,zipFileNameLen,NULL,NULL);
zipFile = gzopen(nameBuff, "wb");
#else // WinXP下大妈
zipFile = gzopen(zipFileName, "wb");
#endif
z_off_t pos;
// 临时变量定义
BYTE buff[10000]={0};
int buffLen;
int BlockSize = sizeof(buff);
int BlockLeft = fileLen%BlockSize;
int BlockNum = fileLen/BlockSize;
if(BlockLeft != 0)
{
BlockNum++;
}
// 开始压缩
for(int BlockIndex=0;BlockIndex<BlockNum;BlockIndex++)
{
if(BlockLeft != 0)
{
if(BlockIndex < BlockNum - 1)
buffLen = BlockSize;
else
buffLen = BlockLeft;
}
else
{
buffLen = BlockSize;
}
unzipFile.ReadData(buff,BlockIndex*BlockSize,buffLen);
if (zipFile == NULL)
{
MessageBox(_T("gzopen error/n"));
return FALSE;
}
#ifdef ARM
gzwrite(zipFile,(voidp)buff,buffLen);
#else
gzwrite(zipFile,(voidpc)buff,buffLen);
#endif
}
gzclose(zipFile);
unzipFile.CloseFile();
// dest file name is not processed.
// CFile::Rename(zipFileName,dstFileName);
}
解压缩代码:
BOOL CMyZip_evcDlg::unzip(CString srcFileName, CString dstFileName)
{
// 打开待解压的文件
gzFile zipFile;
CString zipFileName = srcFileName;
int zipFileNameLen = zipFileName.GetLength();
#ifdef ARM
char nameBuff[100]={0};
WideCharToMultiByte(CP_ACP,0,zipFileName,zipFileNameLen,nameBuff,zipFileNameLen,NULL,NULL);
zipFile = gzopen(nameBuff, "rb");
#else
zipFile = gzopen(zipFileName, "rb");
#endif
if (zipFile == NULL) {
MessageBox(_T("gzopen error"));
return FALSE;
}
// 创建文件
CStorageDeal unzipFile;
dstFileName = srcFileName;
dstFileName.TrimRight(_T(".gz"));
unzipFile.OpenFile(dstFileName);
unzipFile.SetFileLength(0);
// 临时变量
BYTE buff[1000]={0};
int buffLen = sizeof(buff);
// 开始解压
int len;
int pos = 0;
while(1)
{
len = gzread(zipFile, (voidp)buff,buffLen);
if(len==0) break;
unzipFile.WriteData(buff,pos,len);
pos += len;
}
gzclose(zipFile);
unzipFile.CloseFile();
}
<<<<<<<<
第五步: 调用压缩与解压缩代码
void CMyZIP_vc6Dlg::OnButton1()
{
zip(_T("d://aa.doc"),_T("aa.doc.gz"));
}
void CMyZIP_vc6Dlg::OnButton2()
{
unzip(_T("d://aa.doc.gz"),_T("aa.doc"));
}
<<<<<<<<