一、压缩文件
我们的程序要用到了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"; //要压缩的文件的路径 // 下载的源代码中后缀名为rar,经测试无法打开 //打开要进行压缩的文件 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; //创建一个文件映射 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; //创建一个文件映射的视图用来作为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; //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小, // 解压缩的时候用,当然还可以保存更多的信息,这里用不到 dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD); //以下是创建一个文件,用来保存压缩后的文件 hFileToWrite = CreateFile("C:/demoFile.txt", // 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 FILE_MAP_WRITE, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0, 0 ); // Map entire file. if (lpMapAddressToWrite == NULL) { AfxMessageBox("Could not map view of file"); return; } //这里是将压缩前的大小保存在文件的第一个DWORD 里面 LPVOID pBuf = lpMapAddressToWrite; (*(DWORD*)pBuf) = dwFileLength; pBuf = (DWORD*)pBuf + 1; //这里就是最重要的,zlib 里面提供的一个方法,将源缓存的数据压缩至目的缓存 //原形如下: //int compress (Bytef *dest, uLongf *destLen, const Bytef*source, uLong sourceLen); //参数destLen 返回实际压缩后的文件大小。 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为:
//因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小, // 解压缩的时候用,当然还可以保存更多的信息,这里用不到 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, // 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; //创建一个文件映射 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; //创建一个文件映射的视图用来作为source lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping 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); //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小, // 解压缩的时候用,当然还可以保存更多的信息,这里用不到 // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD); dwFileLengthToWrite = (*(DWORD*)lpMapAddress); LPVOID pSourceBuf = lpMapAddress; pSourceBuf = (DWORD*)pSourceBuf + 1; //以下是创建一个文件,用来保存压缩后的文件 hFileToWrite = CreateFile("C:/UnZipFile.txt", // 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, // Currentfile 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; } //这里是将压缩前的大小保存在文件的第一个DWORD 里面 LPVOID pBuf = lpMapAddressToWrite; //这里就是最重要的,zlib 里面提供的一个方法,将源缓存的数据压缩至目的缓存 //原形如下: //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); //参数destLen 返回实际压缩后的文件大小。 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);
运行效果如下: