一、壓縮文件
我們的程序要用到了zip壓縮,就需要自己將幾個(gè)zip相關(guān)文件加入到工程中
zlib.h zconf.h zlib.lib 這些可以自己上網(wǎng)下載 http://d.download.csdn.net/down/2344459/mryeze
在程序中要將 兩個(gè).h文件 add to project。然后聲明引入lib
- #include "zlib.h"http://壓縮文件相關(guān)
- #include "zconf.h"
- #pragma comment(lib,"zlib.lib")
這些工作只是為了使程序中的關(guān)鍵函數(shù)compress 能夠使用
之后就寫我們的代碼了,源代碼是網(wǎng)上找的,自己在做的時(shí)候,對其做了一點(diǎn)修改,使其能直接運(yùn)行。
- HANDLE hFile, hFileToWrite;
-
- CString strFilePath = "C:/aaa.txt";
-
-
-
- hFile = CreateFile(strFilePath,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
- if (hFile == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("Could not open file to read");
- return;
- }
-
- HANDLE hMapFile, hMapFileToWrite;
-
- hMapFile = CreateFileMapping(hFile,
- NULL,
- PAGE_READONLY,
- 0,
- 0,
- "ZipTestMappingObjectForRead"
- );
- if (hMapFile == NULL)
- {
- AfxMessageBox("Could not create file mapping object");
- return;
- }
-
-
- LPVOID lpMapAddress, lpMapAddressToWrite;
-
- lpMapAddress = MapViewOfFile(hMapFile,
- FILE_MAP_READ,
- 0,
- 0,
- 0);
- if (lpMapAddress == NULL)
- {
- AfxMessageBox("Could not map view of file");
- return;
- }
-
-
- DWORD dwFileLength,dwFileLengthToWrite;
- dwFileLength = GetFileSize(hFile, NULL);
-
- / m_dwSourceFileLength = dwFileLength;
-
-
-
- dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);
-
-
-
- hFileToWrite = CreateFile("C:/demoFile.txt",
- GENERIC_WRITE|GENERIC_READ,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL ,
- NULL);
- if (hFileToWrite == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("Could not open file to write");
- return;
- }
-
-
- hMapFileToWrite = CreateFileMapping(hFileToWrite,
- NULL,
- PAGE_READWRITE,
- 0,
- dwFileLengthToWrite,
- "ZipTestMappingObjectForWrite");
- if (hMapFileToWrite == NULL)
- {
- AfxMessageBox("Could not create file mapping object for write");
- return;
- }
-
-
- lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite,
- FILE_MAP_WRITE,
- 0,
- 0,
- 0
- );
- if (lpMapAddressToWrite == NULL)
- {
- AfxMessageBox("Could not map view of file");
- return;
- }
-
-
- LPVOID pBuf = lpMapAddressToWrite;
- (*(DWORD*)pBuf) = dwFileLength;
- pBuf = (DWORD*)pBuf + 1;
-
-
-
-
-
- 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);
運(yùn)行程序后,將aaa.txt文件壓縮成功了。

二、解壓文件
解壓文件的思路和壓縮差不多,其中的區(qū)別就是
1. 輸出文件緩沖區(qū)大小的設(shè)定
zip為:
-
-
- dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);
upzip為:
- dwFileLengthToWrite = (*(DWORD*)lpMapAddress);
2 compress 與 uncompress函數(shù)
這個(gè)是必須的了!
- compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);
- uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);
還是提供源代碼
- HANDLE hFile, hFileToWrite;
- CString strFilePath="C:/demoFile.txt";
-
- hFile = CreateFile(strFilePath,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
- if (hFile == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("Could not open file to read");
- return;
- }
- HANDLE hMapFile, hMapFileToWrite;
-
- hMapFile = CreateFileMapping(hFile,
- NULL,
- PAGE_READONLY,
- 0,
- 0,
- "ZipTestMappingObjectForRead");
- if (hMapFile == NULL)
- {
- AfxMessageBox("Could not create file mapping object");
- return;
- }
- LPVOID lpMapAddress, lpMapAddressToWrite;
-
- lpMapAddress = MapViewOfFile(hMapFile,
- FILE_MAP_READ,
- 0,
- 0,
- 0);
- if (lpMapAddress == NULL)
- {
- AfxMessageBox("Could not map view of file");
- return;
- }
- DWORD dwFileLength,dwFileLengthToWrite;
- dwFileLength = GetFileSize(hFile, NULL) - sizeof(DWORD);
-
-
-
- dwFileLengthToWrite = (*(DWORD*)lpMapAddress);
- LPVOID pSourceBuf = lpMapAddress;
- pSourceBuf = (DWORD*)pSourceBuf + 1;
-
- hFileToWrite = CreateFile("C:/UnZipFile.txt",
- GENERIC_WRITE|GENERIC_READ,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL ,
- NULL
- );
- if (hFileToWrite == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("Could not open file to write");
- return;
- }
- hMapFileToWrite = CreateFileMapping(hFileToWrite,
- NULL,
- PAGE_READWRITE,
- 0,
- dwFileLengthToWrite,
- "ZipTestMappingObjectForWrite");
- if (hMapFileToWrite == NULL)
- {
- AfxMessageBox("Could not create file mapping object for write");
- return;
- }
- lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite,
- FILE_MAP_WRITE,
- 0,
- 0,
- 0
- );
- if (lpMapAddressToWrite == NULL)
- {
- AfxMessageBox("Could not map view of file");
- return;
- }
-
- LPVOID pBuf = lpMapAddressToWrite;
-
-
-
-
- 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);
運(yùn)行效果如下:

|