小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

壓縮解壓縮的函數(shù)庫zlib的使用--gqlyxg1981的博客

 liujian6916 2007-01-07

                                    

zlib是一套公開源代碼的壓縮,解壓縮的函數(shù)庫,提供了很多文件操作的方法,但是他不是一套類庫,所以有興趣的人都可以把他進(jìn)行封裝,實(shí)現(xiàn)自己的類庫,和更高層的接口。
具體的介紹可以參考http://www./zlib/主頁,這里有詳細(xì)介紹。

    這里簡單實(shí)現(xiàn)了zlib的最簡單的用法,壓縮一個(gè)文件,通過使用文件映射來實(shí)現(xiàn)的。
    包含頭文件 zlib.h 和 zconf.h 和 zdll.lib

HANDLE hFile, hFileToWrite;

//打開要進(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è)文件映射的視圖用來作為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);
//因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD用來保存壓縮前的大小,
// 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到
dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);

//以下是創(chuàng)建一個(gè)文件,用來保存壓縮后的文件
hFileToWrite = CreateFile("demoFile.gz", // 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;
(*(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);

以上代碼通過測試,如果有疑問可以聯(lián)系djy@djypanda.com

 


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多