• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            使用zlib壓縮解壓縮文件的詳細過程

            zlib是一套公開源代碼的壓縮,解壓縮的函數(shù)庫,提供了很多文件操作的方法,但是他不是一套類庫,所以有興趣的人都可以把他進行封裝,實現(xiàn)自己的類庫,和更高層的接口。
            具體的介紹可以參考http://www.gzip.org/zlib/主頁,這里有詳細介紹。
               
            這里簡單實現(xiàn)了zlib的最簡單的用法,壓縮一個文件,通過使用文件映射來實現(xiàn)的。
               
            包含頭文件 zlib.h zconf.h zlib.lib
            stdafx.h 中加入:
            #ifdef _DEBUG
            #pragma comment(lib,"zlibd.lib")
            #else
            #pragma comment(lib,"zlib.lib")
            #endif
            #include "zlib.h"
            #include "zconf.h"
            壓縮代碼:

             HANDLE hFile, hFileToWrite;
             CString strFilePath;
             m_ctrEdit.GetWindowText(strFilePath);
             
             //
            打開要進行壓縮的文件
             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;
             
             //
            創(chuàng)建一個文件映射
             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;
             
             //
            創(chuàng)建一個文件映射的視圖用來作為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;
             //
            因為壓縮函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個DWORD用來保存壓縮前的大小,
             //
            解壓縮的時候用,當然還可以保存更多的信息,這里用不到
             dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
             
             //
            以下是創(chuàng)建一個文件,用來保存壓縮后的文件
             hFileToWrite = CreateFile("demoFile.rar", // 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 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;
             (*(DWORD*)pBuf) = dwFileLength;
             pBuf = (DWORD*)pBuf + 1;
             
             
             //////////////////////////////////////////////////////////////////////
             
             //
            這里就是最重要的,zlib里面提供的一個方法,將源緩存的數(shù)據(jù)壓縮至目的緩存
             //
            原形如下:
             //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
             //
            參數(shù)destLen返回實際壓縮后的文件大小。
             compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);
             
             //////////////////////////////////////////////////////////////////////
             
             UnmapViewOfFile(lpMapAddress);
             CloseHandle(hMapFile);
             CloseHandle(hFile);
             
             UnmapViewOfFile(lpMapAddressToWrite);
             CloseHandle(hMapFileToWrite);
             //
            這里將文件大小重新設(shè)置一下
             SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN);
             SetEndOfFile(hFileToWrite);
             CloseHandle(hFileToWrite);

            解壓縮的方法其他地方都一樣,不同的就是使用方法是uncompress而不是compress
            原形如下:

            int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
            解壓縮代碼如下:

             HANDLE hFile, hFileToWrite;
             CString strFilePath;
             m_ctrEdit.GetWindowText(strFilePath);
             
             //
            打開要進行解壓縮的文件
             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;
             
             //
            創(chuàng)建一個文件映射
             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;
             
             //
            創(chuàng)建一個文件映射的視圖用來作為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) - sizeof(DWORD);
             //
            因為壓縮函數(shù)的輸出緩沖必須比輸入大0.1% + 12 然后一個DWORD用來保存壓縮前的大小,
             //
            解壓縮的時候用,當然還可以保存更多的信息,這里用不到
            // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);
             dwFileLengthToWrite = (*(DWORD*)lpMapAddress);

             LPVOID pSourceBuf = lpMapAddress;
             pSourceBuf = (DWORD*)pSourceBuf + 1;
             
             //
            以下是創(chuàng)建一個文件,用來保存壓縮后的文件
             hFileToWrite = CreateFile("demoFile.pdf", // 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, // 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 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里面提供的一個方法,將源緩存的數(shù)據(jù)壓縮至目的緩存
             //
            原形如下:
             //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
             //
            參數(shù)destLen返回實際壓縮后的文件大小。
             uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);
             
             //////////////////////////////////////////////////////////////////////
             
             UnmapViewOfFile(lpMapAddress);
             CloseHandle(hMapFile);
             CloseHandle(hFile);
             
             UnmapViewOfFile(lpMapAddressToWrite);
             CloseHandle(hMapFileToWrite);
             //
            這里將文件大小重新設(shè)置一下
             SetFilePointer(hFileToWrite,dwFileLengthToWrite ,NULL,FILE_BEGIN);
             SetEndOfFile(hFileToWrite);
             CloseHandle(hFileToWrite);

            以上代碼通過測試,如果有疑問可以聯(lián)系dawn2004cn@163.com

             

            posted on 2009-01-01 16:15 肥仔 閱讀(22955) 評論(9)  編輯 收藏 引用 所屬分類: 壓縮 & 解壓

            評論

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            收藏之~~謝謝~
            2009-01-02 15:25 | 小不點

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            大哥,別誤導(dǎo)新人,你這樣壓縮出來的RAR文件能用才怪,也只能讓你自己才能解壓縮吧
            2009-02-17 10:52 | 嘿哈

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            阿彌陀佛,,,,,不知不罪....
            2010-08-10 08:56 | 回復(fù)嘿哈的

            # re: 使用zlib壓縮解壓縮文件的詳細過程[未登錄]  回復(fù)  更多評論   

            “經(jīng)過測試”連這個都是復(fù)制的把,不說了,對新人是一種磨練

            2010-08-12 14:40 | yy

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            這樣搞出來的文件rar真的是不能識別呢。
            2011-11-30 23:19 | williamshy

            # re: 使用zlib壓縮解壓縮文件的詳細過程[未登錄]  回復(fù)  更多評論   

            無法解壓啊
            2011-11-30 23:20 | xxx

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            誤人子弟啊
            2011-12-16 17:58 | 盟XX

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            如果要壓縮成 rar 文件的話,還要寫入 gzip 數(shù)據(jù)頭
            直接用 compress 壓縮的數(shù)據(jù)單純是壓縮數(shù)據(jù)而已,沒有任何附加信息
            2012-06-14 00:21 | 法克魷

            # re: 使用zlib壓縮解壓縮文件的詳細過程  回復(fù)  更多評論   

            我只能參考前5行
            2015-02-12 15:25 | Fack
            超级碰久久免费公开视频| 午夜精品久久久久久久| 亚洲AV日韩精品久久久久久久| 久久精品国产WWW456C0M| 国产精品久久久久影院嫩草| 久久久婷婷五月亚洲97号色| 午夜精品久久久久久影视riav| 日韩精品国产自在久久现线拍| 久久永久免费人妻精品下载| 亚洲精品国产字幕久久不卡| 久久久久久精品成人免费图片| 亚洲精品无码专区久久同性男| 国产精品成人精品久久久| 热久久国产精品| 日韩欧美亚洲综合久久影院d3| 久久婷婷五月综合色高清| 亚洲AV无码1区2区久久| 久久久免费精品re6| 99久久久精品免费观看国产| 国产精品福利一区二区久久| 久久精品国产亚洲网站| 99久久亚洲综合精品成人| 国产精品成人久久久久三级午夜电影 | 99精品国产综合久久久久五月天| 久久精品亚洲欧美日韩久久| 久久中文精品无码中文字幕| 伊人 久久 精品| 久久婷婷五月综合色奶水99啪| 久久国产成人精品麻豆| 久久青青草原精品国产软件| 热99RE久久精品这里都是精品免费 | 亚洲AV无码一区东京热久久| 亚洲αv久久久噜噜噜噜噜| 69SEX久久精品国产麻豆| 国内精品欧美久久精品| 少妇熟女久久综合网色欲| 少妇内射兰兰久久| 久久亚洲高清观看| 久久人人爽人人爽人人爽| 99久久免费国产特黄| 一极黄色视频久久网站|