一、压缩文件
我们的程序要用到了zip压缩,就需要自己将几个zip相关文件加入到工程中
zlib.h zconf.h zlib.lib 这些可以自己上网下载 http://d.download.csdn.net/down/2344459/mryeze
在程序中要将 两个.h文件 add to project。然后声明引入lib
- #include "zlib.h"//压缩文件相关
- #include "zconf.h"
- #pragma comment(lib,"zlib.lib")
这些工作只是为了使程序中的关键函数compress 能够使用
之后就写我们的代码了,源代码是网上找的,自己在做的时候,对其做了一点修改,使其能直接运行。
- 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);
运行程序后,将aaa.txt文件压缩成功了。

二、解压文件
解压文件的思路和压缩差不多,其中的区别就是
1. 输出文件缓冲区大小的设定
zip为:
-
-
- dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);
upzip为:
- dwFileLengthToWrite = (*(DWORD*)lpMapAddress);
2 compress 与 uncompress函数
这个是必须的了!
- 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);
运行效果如下:

|