Zip压缩文件 与 解压 (MFC 文件操作 四 )

 

一、压缩文件

 

我们的程序要用到了zip压缩,就需要自己将几个zip相关文件加入到工程中

 

zlib.h  zconf.h  zlib.lib  这些可以自己上网下载  http://d.download.csdn.net/down/2344459/mryeze

 

在程序中要将 两个.h文件 add to project。然后声明引入lib

  1. #include "zlib.h"//压缩文件相关  
  2. #include "zconf.h"  
  3. #pragma comment(lib,"zlib.lib")  

 

这些工作只是为了使程序中的关键函数compress 能够使用

之后就写我们的代码了,源代码是网上找的,自己在做的时候,对其做了一点修改,使其能直接运行。

 

  1. HANDLE hFile, hFileToWrite;  
  2.   
  3. CString strFilePath = "C:/aaa.txt"//要压缩的文件的路径   
  4.                                                  // 下载的源代码中后缀名为rar,经测试无法打开   
  5.   
  6. //打开要进行压缩的文件   
  7. hFile = CreateFile(strFilePath, // file name   
  8.     GENERIC_READ, // open for reading   
  9.     FILE_SHARE_READ, // share for reading   
  10.     NULL, // no security   
  11.     OPEN_EXISTING, // existing file only   
  12.     FILE_ATTRIBUTE_NORMAL, // normal file   
  13.     NULL  
  14.     ); // no attr. template   
  15. if (hFile == INVALID_HANDLE_VALUE)  
  16. {  
  17.     AfxMessageBox("Could not open file to read"); // process error   
  18.     return;  
  19. }  
  20.   
  21. HANDLE hMapFile, hMapFileToWrite;  
  22. //创建一个文件映射   
  23. hMapFile = CreateFileMapping(hFile, // Current file handle.   
  24.     NULL, // Default security.   
  25.     PAGE_READONLY, // Read/write permission.   
  26.     0, // Max. object size.   
  27.     0, // Size of hFile.   
  28.     "ZipTestMappingObjectForRead"  
  29.     ); // Name of mapping object.   
  30. if (hMapFile == NULL)  
  31. {  
  32.     AfxMessageBox("Could not create file mapping object");  
  33.     return;  
  34. }  
  35.   
  36.   
  37. LPVOID lpMapAddress, lpMapAddressToWrite;  
  38. //创建一个文件映射的视图用来作为source   
  39. lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.   
  40.     FILE_MAP_READ, // Read/write permission   
  41.     0, // Max. object size.   
  42.     0, // Size of hFile.   
  43.     0); // Map entire file.   
  44. if (lpMapAddress == NULL)  
  45. {  
  46.     AfxMessageBox("Could not map view of file");  
  47.     return;  
  48. }  
  49.   
  50.   
  51. DWORD dwFileLength,dwFileLengthToWrite;  
  52. dwFileLength = GetFileSize(hFile, NULL);    //获取文件的大小   
  53.   
  54. /   m_dwSourceFileLength = dwFileLength;  
  55.   
  56. //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小,   
  57.     // 解压缩的时候用,当然还可以保存更多的信息,这里用不到   
  58. dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);  
  59.   
  60.   
  61. //以下是创建一个文件,用来保存压缩后的文件   
  62. hFileToWrite = CreateFile("C:/demoFile.txt"// demoFile.rar   
  63.     GENERIC_WRITE|GENERIC_READ, // open for writing   
  64.     0, // do not share   
  65.     NULL, // no security   
  66.     CREATE_ALWAYS, // overwrite existing   
  67.     FILE_ATTRIBUTE_NORMAL , // normal file   
  68.     NULL); // no attr. template   
  69. if (hFileToWrite == INVALID_HANDLE_VALUE)  
  70. {  
  71.     AfxMessageBox("Could not open file to write"); // process error   
  72.     return;  
  73. }  
  74.   
  75. //为输出文件  创建一个文件映射   
  76. hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle.   
  77.     NULL, // Default security.   
  78.     PAGE_READWRITE, // Read/write permission.   
  79.     0, // Max. object size.   
  80.     dwFileLengthToWrite, // Size of hFile.   
  81.     "ZipTestMappingObjectForWrite"); // Name of mapping object.   
  82. if (hMapFileToWrite == NULL)  
  83. {  
  84.     AfxMessageBox("Could not create file mapping object for write");  
  85.     return;  
  86. }  
  87.   
  88. //为输出文件 创建一个文件映射视图   
  89. lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, //Handle to mapping   
  90.     FILE_MAP_WRITE, // Read/write permission 0, // Max. object size.   
  91.     0, // Size of hFile.   
  92.     0,  
  93.     0  
  94.     ); // Map entire file.   
  95. if (lpMapAddressToWrite == NULL)  
  96. {  
  97.     AfxMessageBox("Could not map view of file");  
  98.     return;  
  99. }  
  100.   
  101. //这里是将压缩前的大小保存在文件的第一个DWORD 里面   
  102. LPVOID pBuf = lpMapAddressToWrite;  
  103. (*(DWORD*)pBuf) = dwFileLength;  
  104. pBuf = (DWORD*)pBuf + 1;  
  105.   
  106. //这里就是最重要的,zlib 里面提供的一个方法,将源缓存的数据压缩至目的缓存   
  107. //原形如下:   
  108. //int compress (Bytef *dest, uLongf *destLen, const Bytef*source, uLong sourceLen);   
  109. //参数destLen 返回实际压缩后的文件大小。   
  110. compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);  
  111.   
  112. UnmapViewOfFile(lpMapAddress);  
  113. CloseHandle(hMapFile);  
  114. CloseHandle(hFile);  
  115. UnmapViewOfFile(lpMapAddressToWrite);  
  116. CloseHandle(hMapFileToWrite);  
  117.   
  118. //这里将文件大小重新设置一下   
  119. SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN);  
  120. SetEndOfFile(hFileToWrite);  
  121. CloseHandle(hFileToWrite);  

 

运行程序后,将aaa.txt文件压缩成功了。

 

 

 

二、解压文件

 

解压文件的思路和压缩差不多,其中的区别就是

1. 输出文件缓冲区大小的设定

zip为:

  1. //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小,   
  2. // 解压缩的时候用,当然还可以保存更多的信息,这里用不到   
  3. dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);  

 

upzip为:

  1. dwFileLengthToWrite = (*(DWORD*)lpMapAddress);  

 

2 compress 与 uncompress函数

这个是必须的了!

  1. compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);  

 

  1. uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);  

 

还是提供源代码

  1. HANDLE hFile, hFileToWrite;  
  2. CString strFilePath="C:/demoFile.txt";  
  3. //打开要进行解压缩的文件   
  4. hFile = CreateFile(strFilePath, // file name   
  5.     GENERIC_READ, // open for reading   
  6.     FILE_SHARE_READ, // share for reading   
  7.     NULL, // no security   
  8.     OPEN_EXISTING, // existing file only   
  9.     FILE_ATTRIBUTE_NORMAL, // normal file   
  10.     NULL  
  11.     ); // no attr. template   
  12. if (hFile == INVALID_HANDLE_VALUE)  
  13. {  
  14.     AfxMessageBox("Could not open file to read"); // process error   
  15.     return;  
  16. }  
  17. HANDLE hMapFile, hMapFileToWrite;  
  18. //创建一个文件映射   
  19. hMapFile = CreateFileMapping(hFile, // Current file handle.   
  20.     NULL, // Default security.   
  21.     PAGE_READONLY, // Read/write permission.   
  22.     0, // Max. object size.   
  23.     0, // Size of hFile.   
  24.     "ZipTestMappingObjectForRead"); // Name of mapping object.   
  25. if (hMapFile == NULL)  
  26. {  
  27.     AfxMessageBox("Could not create file mapping object");  
  28.     return;  
  29. }  
  30. LPVOID lpMapAddress, lpMapAddressToWrite;  
  31. //创建一个文件映射的视图用来作为source   
  32. lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping   
  33.     FILE_MAP_READ, // Read/write permission   
  34.     0, // Max. object size.   
  35.     0, // Size of hFile.   
  36.     0); // Map entire file.   
  37. if (lpMapAddress == NULL)  
  38. {  
  39.     AfxMessageBox("Could not map view of file");  
  40.     return;  
  41. }  
  42. DWORD dwFileLength,dwFileLengthToWrite;  
  43. dwFileLength = GetFileSize(hFile, NULL) - sizeof(DWORD);  
  44. //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD 用来保存压缩前的大小,   
  45.     // 解压缩的时候用,当然还可以保存更多的信息,这里用不到   
  46.     // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);   
  47.     dwFileLengthToWrite = (*(DWORD*)lpMapAddress);  
  48. LPVOID pSourceBuf = lpMapAddress;  
  49. pSourceBuf = (DWORD*)pSourceBuf + 1;  
  50. //以下是创建一个文件,用来保存压缩后的文件   
  51. hFileToWrite = CreateFile("C:/UnZipFile.txt"// create demo.gz   
  52.     GENERIC_WRITE|GENERIC_READ, // open for writing   
  53.     0, // do not share   
  54.     NULL, // no security   
  55.     CREATE_ALWAYS, // overwrite existing   
  56.     FILE_ATTRIBUTE_NORMAL , // normal file   
  57.     NULL  
  58.     ); // no attr. template   
  59. if (hFileToWrite == INVALID_HANDLE_VALUE)  
  60. {  
  61.     AfxMessageBox("Could not open file to write"); //process error   
  62.     return;  
  63. }  
  64. hMapFileToWrite = CreateFileMapping(hFileToWrite, // Currentfile handle.   
  65.     NULL, // Default security.   
  66.     PAGE_READWRITE, // Read/write permission.   
  67.     0, // Max. object size.   
  68.     dwFileLengthToWrite, // Size of hFile.   
  69.     "ZipTestMappingObjectForWrite"); // Name of mapping object.   
  70. if (hMapFileToWrite == NULL)  
  71. {  
  72.     AfxMessageBox("Could not create file mapping object for write");  
  73.     return;  
  74. }  
  75. lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, //Handle to mapping object.   
  76.     FILE_MAP_WRITE, // Read/write permission   
  77.     0, // Max. object size.   
  78.     0, // Size of hFile.   
  79.     0  
  80.     ); // Map entire file.   
  81. if (lpMapAddressToWrite == NULL)  
  82. {  
  83.     AfxMessageBox("Could not map view of file");  
  84.     return;  
  85. }  
  86. //这里是将压缩前的大小保存在文件的第一个DWORD 里面   
  87. LPVOID pBuf = lpMapAddressToWrite;  
  88. //这里就是最重要的,zlib 里面提供的一个方法,将源缓存的数据压缩至目的缓存   
  89. //原形如下:   
  90. //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);   
  91. //参数destLen 返回实际压缩后的文件大小。   
  92. uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);  
  93. UnmapViewOfFile(lpMapAddress);  
  94. CloseHandle(hMapFile);  
  95. CloseHandle(hFile);  
  96. UnmapViewOfFile(lpMapAddressToWrite);  
  97. CloseHandle(hMapFileToWrite);  
  98. //这里将文件大小重新设置一下   
  99. SetFilePointer(hFileToWrite,dwFileLengthToWrite,NULL,FILE_BEGIN);  
  100. SetEndOfFile(hFileToWrite);  
  101. CloseHandle(hFileToWrite);  

 

运行效果如下:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值