zlib是一套公開(kāi)源代碼的壓縮,解壓縮的函數(shù)庫(kù),提供了很多文件操作的方法,但是他不是一套類(lèi)庫(kù),所以有興趣的人都可以把他進(jìn)行封裝,實(shí)現(xiàn)自己的類(lèi)庫(kù),和更高層的接口。
具體的介紹可以參考http://www.gzip.org/zlib/主頁(yè),這里有詳細(xì)介紹。
這里簡(jiǎn)單實(shí)現(xiàn)了zlib的最簡(jiǎn)單的用法,壓縮一個(gè)文件,通過(guò)使用文件映射來(lái)實(shí)現(xiàn)的。
包含頭文件 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);
//打開(kāi)要進(jìn)行壓縮的文件
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;
//創(chuàng)建一個(gè)文件映射
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;
//創(chuàng)建一個(gè)文件映射的視圖用來(lái)作為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;
//因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD用來(lái)保存壓縮前的大小,
// 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到
dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
//以下是創(chuàng)建一個(gè)文件,用來(lái)保存壓縮后的文件
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;
}
//這里是將壓縮前的大小保存在文件的第一個(gè)DWORD里面
LPVOID pBuf = lpMapAddressToWrite;
(*(DWORD*)pBuf) = dwFileLength;
pBuf = (DWORD*)pBuf + 1;
//////////////////////////////////////////////////////////////////////
//這里就是最重要的,zlib里面提供的一個(gè)方法,將源緩存的數(shù)據(jù)壓縮至目的緩存
//原形如下:
//int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
//參數(shù)destLen返回實(shí)際壓縮后的文件大小。
compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);
//////////////////////////////////////////////////////////////////////
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);
CloseHandle(hFile);
UnmapViewOfFile(lpMapAddressToWrite);
CloseHandle(hMapFileToWrite);
//這里將文件大小重新設(shè)置一下
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);
//打開(kāi)要進(jìn)行解壓縮的文件
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;
//創(chuàng)建一個(gè)文件映射
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;
//創(chuàng)建一個(gè)文件映射的視圖用來(lái)作為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);
//因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD用來(lái)保存壓縮前的大小,
// 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到
// dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
dwFileLengthToWrite = (*(DWORD*)lpMapAddress);
LPVOID pSourceBuf = lpMapAddress;
pSourceBuf = (DWORD*)pSourceBuf + 1;
//以下是創(chuàng)建一個(gè)文件,用來(lái)保存壓縮后的文件
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;
}
//這里是將壓縮前的大小保存在文件的第一個(gè)DWORD里面
LPVOID pBuf = lpMapAddressToWrite;
//////////////////////////////////////////////////////////////////////
//這里就是最重要的,zlib里面提供的一個(gè)方法,將源緩存的數(shù)據(jù)壓縮至目的緩存
//原形如下:
//int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
//參數(shù)destLen返回實(shí)際壓縮后的文件大小。
uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);
//////////////////////////////////////////////////////////////////////
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);
CloseHandle(hFile);
UnmapViewOfFile(lpMapAddressToWrite);
CloseHandle(hMapFileToWrite);
//這里將文件大小重新設(shè)置一下
SetFilePointer(hFileToWrite,dwFileLengthToWrite ,NULL,FILE_BEGIN);
SetEndOfFile(hFileToWrite);
CloseHandle(hFileToWrite);
以上代碼通過(guò)測(cè)試,如果有疑問(wèn)可以聯(lián)系dawn2004cn@163.com