boost庫編譯及gzip + boost::iostream使用html
一.boost庫編譯ios
1.下載源碼 http://www.boost.org/users/history/version_1_58_0.html 2.解壓文件到本地目錄[D:\download\boost_1_58_0],進入目錄並運行bootstrap.bat,生成boost相關的構建工具 3.cmd命令編譯庫 動態庫: bjam install stage --toolset=msvc-12.0 --stagedir="C:\Boost\boost_vc_120" link=shared runtime-link=shared threading=multi debug release 靜態庫: bjam install stage --toolset=msvc-12.0 --stagedir="C:\Boost\boost_vc_120" link=static runtime-link=static threading=multi debug release
注意點:bootstrap
[1].bjam涉及相關toolset設置,須要時也可修改目錄下的project-config.jam,好比添加 using mpi ;[注意mpi後面留有一個空格,而後纔是分號] [2].bjam相關參數說明 --build-dir=<builddir> 編譯的臨時文件會放在builddir裏(這樣比較好管理,編譯完就能夠把它刪除了) --stagedir=<stagedir> 存放編譯後庫文件的路徑,默認是stage --build-type=complete 編譯全部版本,否則只會編譯一小部分版本(確切地說是至關於:variant=release, threading=multi;link=shared|static;runtime-link=shared) link=static|shared 決定使用靜態庫仍是動態庫。 threading=single|multi 決定使用單線程仍是多線程庫。 runtime-link=static|shared 決定是靜態仍是動態連接C/C++標準庫。 --with-<library> 只編譯指定的庫,如輸入--with-regex就只編譯regex庫了。 --show-libraries 顯示須要編譯的庫名稱
二.編譯zlib庫服務器
1.下載源碼 http://www.zlib.net/fossils/ 2.解壓文件到本地目錄[C:\zlib-1.2.8] 3.cmd命令編譯庫 設置編譯庫源碼路徑 set ZLIB_SOURCE="C:\zlib-1.2.8" 編譯zlib庫 bjam install stage --toolset=msvc-12.0 --build-type=complete --stagedir="C:\Boost\boost_vc_120" threading=multi debug release
三.boost庫gzip + boost::iostream使用多線程
情景:客戶端網頁請求http頭增長了"Accept-Encoding: gzip, deflate"屬性時,表明客戶端能夠接收壓縮格式的數據,服務器會根據這標識在處理時就將大文件壓縮再發回客戶端,這時客戶端接收到的數據爲壓縮數據,顯示爲亂碼,須要進行解壓才能使用。工具
代碼以下: #include <boost/iostreams/filtering_stream.hpp> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/gzip.hpp> #include <boost/iostreams/device/back_inserter.hpp> #include <vector> #include <string> #include <iostream> #include <sstream> std::string strResponse = "boost_gzip_string_test"; 對原數據壓縮: std::string strCompressed;//壓縮後數據 { filtering_ostream fos; fos.push(gzip_compressor(gzip_params(gzip::best_compression))); fos.push(boost::iostreams::back_inserter(compressedString)); fos << strResponse; boost::iostreams::close(fos); } 對壓縮數據進行解壓: std::string decompressedString; //解壓後數據 { filtering_ostream fos; fos.push(gzip_decompressor()); fos.push(boost::iostreams::back_inserter(decompressedString)); fos << strCompressed; //把壓縮數據按容器長度寫入流 fos << std::flush; }
注意點:
服務端返回gzip數據,前兩個字節標識爲0x1f8b,可經過該標識檢測返回的數據是否壓縮?ui
bool IsDataGzipped(std::string strData) const { bool bgziped = false; if (strData.size() >= 2 && static_cast<unsigned char>(strData[0]) == 0x1f && static_cast<unsigned char>(strData[1]) == 0x8b) { bgziped = true; } return bgziped; }