日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Zip壓縮文件與解壓(MFC文件操作四)

 寂靜如河 2012-08-06

一、壓縮文件

 

我們的程序要用到了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

[c-sharp] view plaincopy?
  1. #include "zlib.h"http://壓縮文件相關(guān)  
  2. #include "zconf.h"  
  3. #pragma comment(lib,"zlib.lib")  

 

這些工作只是為了使程序中的關(guān)鍵函數(shù)compress 能夠使用

之后就寫我們的代碼了,源代碼是網(wǎng)上找的,自己在做的時(shí)候,對其做了一點(diǎn)修改,使其能直接運(yùn)行。

 

  1. HANDLE hFile, hFileToWrite;  
  2.   
  3. CString strFilePath = "C:/aaa.txt"//要壓縮的文件的路徑   
  4.                                                  // 下載的源代碼中后綴名為rar,經(jīng)測試無法打開   
  5.   
  6. //打開要進(jìn)行壓縮的文件   
  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. //創(chuàng)建一個(gè)文件映射   
  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. //創(chuàng)建一個(gè)文件映射的視圖用來作為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. //因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD 用來保存壓縮前的大小,   
  57.     // 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到   
  58. dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);  
  59.   
  60.   
  61. //以下是創(chuàng)建一個(gè)文件,用來保存壓縮后的文件   
  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. //為輸出文件  創(chuàng)建一個(gè)文件映射   
  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. //為輸出文件 創(chuàng)建一個(gè)文件映射視圖   
  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. //這里是將壓縮前的大小保存在文件的第一個(gè)DWORD 里面   
  102. LPVOID pBuf = lpMapAddressToWrite;  
  103. (*(DWORD*)pBuf) = dwFileLength;  
  104. pBuf = (DWORD*)pBuf + 1;  
  105.   
  106. //這里就是最重要的,zlib 里面提供的一個(gè)方法,將源緩存的數(shù)據(jù)壓縮至目的緩存   
  107. //原形如下:   
  108. //int compress (Bytef *dest, uLongf *destLen, const Bytef*source, uLong sourceLen);   
  109. //參數(shù)destLen 返回實(shí)際壓縮后的文件大小。   
  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. //這里將文件大小重新設(shè)置一下   
  119. SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN);  
  120. SetEndOfFile(hFileToWrite);  
  121. CloseHandle(hFileToWrite);  

 

運(yùn)行程序后,將aaa.txt文件壓縮成功了。

 

 

 

二、解壓文件

 

解壓文件的思路和壓縮差不多,其中的區(qū)別就是

1. 輸出文件緩沖區(qū)大小的設(shè)定

zip為:

[c-sharp] view plaincopy?
  1. //因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD 用來保存壓縮前的大小,   
  2. // 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到   
  3. dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);  

 

upzip為:

[c-sharp] view plaincopy?
  1. dwFileLengthToWrite = (*(DWORD*)lpMapAddress);  

 

2 compress 與 uncompress函數(shù)

這個(gè)是必須的了!

[c-sharp] view plaincopy?
  1. compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);  

 

[c-sharp] view plaincopy?
  1. uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);  

 

還是提供源代碼

[c-sharp] view plaincopy?
  1. HANDLE hFile, hFileToWrite;  
  2. CString strFilePath="C:/demoFile.txt";  
  3. //打開要進(jìn)行解壓縮的文件   
  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. //創(chuàng)建一個(gè)文件映射   
  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. //創(chuàng)建一個(gè)文件映射的視圖用來作為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. //因?yàn)閴嚎s函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個(gè)DWORD 用來保存壓縮前的大小,   
  45.     // 解壓縮的時(shí)候用,當(dāng)然還可以保存更多的信息,這里用不到   
  46.     // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);   
  47.     dwFileLengthToWrite = (*(DWORD*)lpMapAddress);  
  48. LPVOID pSourceBuf = lpMapAddress;  
  49. pSourceBuf = (DWORD*)pSourceBuf + 1;  
  50. //以下是創(chuàng)建一個(gè)文件,用來保存壓縮后的文件   
  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. //這里是將壓縮前的大小保存在文件的第一個(gè)DWORD 里面   
  87. LPVOID pBuf = lpMapAddressToWrite;  
  88. //這里就是最重要的,zlib 里面提供的一個(gè)方法,將源緩存的數(shù)據(jù)壓縮至目的緩存   
  89. //原形如下:   
  90. //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);   
  91. //參數(shù)destLen 返回實(shí)際壓縮后的文件大小。   
  92. uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);  
  93. UnmapViewOfFile(lpMapAddress);  
  94. CloseHandle(hMapFile);  
  95. CloseHandle(hFile);  
  96. UnmapViewOfFile(lpMapAddressToWrite);  
  97. CloseHandle(hMapFileToWrite);  
  98. //這里將文件大小重新設(shè)置一下   
  99. SetFilePointer(hFileToWrite,dwFileLengthToWrite,NULL,FILE_BEGIN);  
  100. SetEndOfFile(hFileToWrite);  
  101. CloseHandle(hFileToWrite);  

 

運(yùn)行效果如下:

 

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(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ā)表

    請遵守用戶 評論公約

    類似文章 更多